WCF:IErrorHandler实现不提供指定的泛型错误

时间:2017-01-09 11:10:39

标签: c# wcf ierrorhandler

我已经实现了ProvideFault接口的IErrorHandler方法,如下所示:

public void ProvideFault(Exception ex, MessageVersion version, ref Message fault)
{
    ResponseModel responseModel = new ResponseModel();
    responseModel.EndDate = DateTime.Now;

    FaultException<ResponseModel> faultEx;

    if (ex is FaultException<ResponseModel>)
    {
        return;
    }
    ServerCoreException coreException = ex as ServerCoreException;
    if (coreException != null)
    {
        ServerCoreException serverCoreException = coreException;
        faultEx = new FaultException<ResponseModel>(responseModel,
        new FaultReason(serverCoreException.Message),
        new FaultCode(serverCoreException.ErrorCode.ToString()));
    }
    else
    {
        faultEx = new FaultException<ResponseModel>(responseModel,
        new FaultReason(ex.Message),
        new FaultCode("-1"));
    }
    MessageFault msgFault = faultEx.CreateMessageFault();
    fault = Message.CreateMessage(version, msgFault, faultEx.Action);
}

然后,我将此实现添加到名为ChannelDispatcherBase的{​​{1}}的另一个实现中的每个IServiceBehavior。我的服务实现使用此属性进行修饰,并且GlobalErrorBehaviorAttribute为:

FaultContractAttribute

但是对服务实现中的方法的测试总是失败抱怨所收到的错误不是[FaultContract(typeof(ResponseModel), Name = "ServiceFaultException", Namespace = "http://ws.net.core/base/model")] 类型而是FaultException<ResponseModel>而是:

FaultException

为什么我在客户端收到Assert.That(() => client.CreateVersion(request), Throws.TypeOf<FaultException<ResponseModel>>()); 而不是FaultException

0 个答案:

没有答案