提问人:AndrewR 提问时间:6/9/2020 更新时间:6/9/2020 访问量:31
WCF 获取有意义的通道异常
WCF getting meaningful channel exceptions
问:
我有一个简单的WCF服务,只有一种方法:
[ServiceContract]
public interface TestServiceContract
{
[OperationContract]
int[] Test();
}
public class TestService:TestServiceContract
{
public int[] Test()
{
return new int[1000000];
}
}
在客户端时,我调用
client.Test();
它失败了,显然是因为我传递的对象太大了。
但
我得到的不是有意义的描述,而是完全无用的描述
通信对象 System.ServiceModel.Channels.ServiceChannel 不能用于通信 因为它处于“故障”状态。
我尝试启用
<serviceDebug includeExceptionDetailInFaults="true" />
但这无济于事。
是否有可能获得有意义的错误描述?
答:
1赞
Ding Peng
6/9/2020
#1
使用“try catch”在创建服务终结点时捕获异常。根据你的描述,我做了一个测试,发现如果传递的对象太大,就会有异常。这是我得到的例外:
这是我的演示:
namespace Test
{
[ServiceContract]
public interface TestServiceContract
{
[OperationContract]
int[] Test();
}
public class TestService : TestServiceContract
{
public int[] Test()
{
return new int[1000000];
}
}
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/");
ServiceHost selfHost = new ServiceHost(typeof(TestService), baseAddress);
try
{
selfHost.AddServiceEndpoint(typeof(TestServiceContract), new WSHttpBinding(), "Test");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <Enter> to terminate the service.");
Console.WriteLine();
Console.ReadLine();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
这是服务器端代码。
static void Main(string[] args)
{
WSHttpBinding myBinding = new WSHttpBinding();
EndpointAddress myEndpoint = new EndpointAddress("http://localhost:8000/GettingStarted/Test");
ChannelFactory<TestServiceContract> myChannelFactory = new ChannelFactory<TestServiceContract>(myBinding, myEndpoint);
TestServiceContract wcfClient1 = myChannelFactory.CreateChannel();
wcfClient1.Test();
}
这是客户端代码。我创建了一个通道工厂来调用该服务。还可以使用 Svcutil 生成代理类来调用服务。
评论