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();

它失败了,显然是因为我传递的对象太大了。

但是

我得到的不是有意义的描述,而是完全没用的

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

我尝试启用

<serviceDebug includeExceptionDetailInFaults="true" />

但没用。

是否可以获得有意义的错误描述?

创建服务时使用"try catch"捕获异常endpoints.According根据你的描述,我测试了一下,如果传入的对象太大,会出现异常。这是我得到的异常:

这是我的演示:

    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();

    }

这是客户端code.I创建一个通道工厂来调用服务。也可以使用Svcutil生成代理类调用服务