1

我正在 Visual Studio 单元测试中试验 WCF 服务。客户端和服务都以编程方式配置。

目前我的代码如下所示:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests
{
    public abstract class EntityBase
    {
    }

    public class TestEntity : EntityBase
    {
        public string Name { get; set; }
    }

    [ServiceContract]
    [ServiceKnownType("GetKnownTypes", typeof(ServiceKnownTypesDiscoveryHelper))]
    public interface ITestService
    {
        [OperationContract]
        EntityBase GetEntity(string entityName);
    }

    public class TestService : ITestService
    {
        public EntityBase GetEntity(string entityName)
        {
            Type t = Type.GetType(entityName);
            return (EntityBase)Activator.CreateInstance(t);
        }
    }

    [TestClass]
    public class ServiceTests
    {
        private static ServiceHost ServiceHost { get; set; }

        [ClassInitialize]
        public static void ClassInitialize(TestContext testContext)
        {        
            ServiceHost = new ServiceHost(typeof(TestService));
            NetTcpBinding wsBinding = new NetTcpBinding();

            ServiceHost.AddServiceEndpoint(typeof(ITestService), wsBinding,
                "net.tcp://localhost:8011/TestService");

// trying to turn on debugging here
            var behavior = ServiceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
            behavior.IncludeExceptionDetailInFaults = true;

            ServiceHost.Open();
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            ServiceHost.Close();
        }

        [TestMethod]
        public void TestSomething()
        {
            var binding = new NetTcpBinding();
            var endpoint = new EndpointAddress("net.tcp://localhost:8011/TestService");

            using (ChannelFactory<ITestService> testServiceFactory =
                                            new ChannelFactory<ITestService>(binding, endpoint))
            {
                var proxy = testServiceFactory.CreateChannel();
                using (proxy as IDisposable)
                {
                    try
                    {
                        var entity = proxy.GetEntity(typeof(TestEntity).FullName);
                        Assert.IsInstanceOfType(entity, typeof(TestEntity));
                    }
                    catch (FaultException ex)
                    {
// copied this from MSDN example
                        string msg = "FaultException: " + ex.Message;
                        MessageFault fault = ex.CreateMessageFault();
                        if (fault.HasDetail == true)
                        {
                            var reader = fault.GetReaderAtDetailContents();
                            if (reader.Name == "ExceptionDetail")
                            {
                                ExceptionDetail detail = fault.GetDetail<ExceptionDetail>();
                                msg += "\n\nStack Trace: " + detail.StackTrace;
                            }
                        }
                        System.Diagnostics.Trace.WriteLine(msg);
                    }
                }
            }
        }
    }
}

如果我的 ServiceKnownTypesDiscoveryHelper 没有返回已知类型,我知道我的服务和客户端应该在 .NET 服务模型代码的深处抛出一些与序列化相关的东西(如果我修改它以返回我的 TestEntity 那么当然一切正常,没有任何问题)。

但是目前如果服务失败,我只会收到一些模糊的异常消息,例如:

The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue.

在 using() 结束时,我得到

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

(这也很奇怪 - 如果 ServiceChannel 处于故障状态,为什么我什至不能处理它......)

如何捕捉导致服务或客户端失败的实际故障,而不是那些模糊的异常消息?

4

0 回答 0