在单个应用程序中托管服务和客户端时捕获WCF错误

时间:2012-12-20 16:25:47

标签: wcf service client faultexception

我正在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.

在使用()结束时我得到

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

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

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

0 个答案:

没有答案