【问题标题】:WCF getting meaningful channel exceptionsWCF 获得有意义的通道异常
【发布时间】:2020-09-27 21:45:11
【问题描述】:

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

但没用。

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

【问题讨论】:

    标签: wcf exception


    【解决方案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 生成代理类来调用服务。

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 2011-02-02
      • 2011-01-12
      • 2011-08-13
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多