WCF获取有意义的通道异常

WCF获取有意义的通道异常,wcf,exception,Wcf,Exception,我有一个简单的WCF服务,有一个方法: [ServiceContract] public interface TestServiceContract { [OperationContract] int[] Test(); } public class TestService:TestServiceContract { public int[] Test() { return new int[1000000]; } } 当我在客户端打电话

我有一个简单的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" />

但这没用

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

在创建服务终结点时,使用“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();
        }
    }
}
名称空间测试
{
[服务合同]
公共接口TestServiceContract
{
[经营合同]
int[]测试();
}
公共类TestService:TestServiceContract
{
公共int[]测试()
{
返回新整数[1000000];
}
}
班级计划
{
静态void Main(字符串[]参数)
{
Uri baseAddress=新Uri(“http://localhost:8000/GettingStarted/");
ServiceHost selfHost=新的ServiceHost(typeof(TestService),baseAddress);
尝试
{
AddServiceEndpoint(typeof(TestServiceContract),新的WSHttpBinding(),“Test”);
ServiceMetadataBehavior smb=新ServiceMetadataBehavior();
smb.HttpGetEnabled=true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
WriteLine(“服务准备就绪”);
Console.WriteLine(“按以终止服务”);
Console.WriteLine();
Console.ReadLine();
}
捕获(通信异常ce)
{
WriteLine(“发生异常:{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();

    }
static void Main(字符串[]args)
{
WSHttpBinding myBinding=新的WSHttpBinding();
EndpointAddress myEndpoint=新的EndpointAddress(“http://localhost:8000/GettingStarted/Test");
ChannelFactory myChannelFactory=新的ChannelFactory(myBinding,myEndpoint);
TestServiceContract wcfClient1=myChannelFactory.CreateChannel();
wcfClient1.Test();
}
这是客户端代码。我创建了一个通道工厂来调用服务。您还可以使用Svcutil生成代理类来调用服务