从基于声明的(WIF)WCF服务引发自定义错误时出现问题 - 现时无效或重播

时间:2013-11-27 16:32:29

标签: c# wcf web-services wif

场景:基于IIS 7.5,MVC 4声明的客户端和基于WCF声明的(WIF)服务.net框架4.5。我正在尝试从WCF服务抛出自定义错误并在客户端捕获它。

服务端的自定义故障是:

[DataContractAttribute]
public class AppModelFault
{

    private string message;

    public AppModelFault(string message)
    {
        this.message = message;
    }

    [DataMember]
    public string Message
    {
        get { return this.message; }
        set { this.message = value; }
    }
}

我在服务方法

的接口定义中有这个
[OperationContract]
[FaultContractAttribute(typeof(AppModelFault))]
Int64 SaveApplication(Application app);

这就是我在服务方面抛出错误的方法:

var errorMsg = "Custom error message";
throw new FaultException<AppModelFault>(new AppModelFault(errorMsg), new FaultReason(errorMsg));

在客户端我正在捕捉这样的自定义错误:

catch (FaultException<AppModelFault> fault)
{
    return Json(new JsonMessage(Response, false, fault.Detail.Message, JsonMessageType.Error));
}

这在本地测试时工作正常,然后AppModelFault异常无问题地流向客户端。但是,当我将它发布到开发机器时,这不起作用。当我添加wcf跟踪日志记录时,在将消息发送到客户端之前,我在服务端收到以下错误。

<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error"><TraceIdentifier>http://msdn.microsoft.com/is-IS/library/System.ServiceModel.Diagnostics.ThrowingException.aspx</TraceIdentifier><Description>Throwing an exception.</Description><AppDomain>/LM/W3SVC/2/ROOT/Isb.ApplicationModel.Service-10-130300371813153037</AppDomain><Exception><ExceptionType>System.ServiceModel.Security.MessageSecurityException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>The nonce is invalid or replayed.</Message><StackTrace>   at System.ServiceModel.Security.ReceiveSecurityHeader.ProcessPrimarySignature(SignedXml signedXml, Boolean isFromDecryptedSource)
   at System.ServiceModel.Security.ReceiveSecurityHeader.ProcessEncryptedData(EncryptedData encryptedData, TimeSpan timeout, Int32 position, Boolean eagerMode, Boolean&amp;amp; primarySignatureFound)
   at System.ServiceModel.Security.ReceiveSecurityHeader.ExecuteFullPass(XmlDictionaryReader reader)
   at System.ServiceModel.Security.ReceiveSecurityHeader.Process(TimeSpan timeout, ChannelBinding channelBinding, ExtendedProtectionPolicy extendedProtectionPolicy)
   at System.ServiceModel.Security.MessageSecurityProtocol.ProcessSecurityHeader(ReceiveSecurityHeader securityHeader, Message&amp;amp; message, SecurityToken requiredSigningToken, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationStates)
   at System.ServiceModel.Security.SymmetricSecurityProtocol.VerifyIncomingMessageCore(Message&amp;amp; message, String actor, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationStates)
   at System.ServiceModel.Security.MessageSecurityProtocol.VerifyIncomingMessage(Message&amp;amp; message, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationStates)
   at System.ServiceModel.Channels.SecurityChannelListener`1.ServerSecurityChannel`1.VerifyIncomingMessage(Message&amp;amp; message, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationState)
   at System.ServiceModel.Channels.SecurityChannelListener`1.SecurityReplyChannel.ProcessReceivedRequest(RequestContext requestContext, TimeSpan timeout)
   at System.ServiceModel.Channels.SecurityChannelListener`1.ReceiveItemAndVerifySecurityAsyncResult`2.OnInnerReceiveDone()
   at System.ServiceModel.Channels.SecurityChannelListener`1.ReceiveItemAndVerifySecurityAsyncResult`2.InnerTryReceiveCompletedCallback(IAsyncResult result)
   at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
   at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
   at System.Runtime.InputQueue`1.AsyncQueueReader.Set(Item item)
   at System.Runtime.InputQueue`1.EnqueueAndDispatch(Item item, Boolean canDispatchOnThisThread)
   at System.Runtime.InputQueue`1.EnqueueAndDispatch(T item, Action dequeuedCallback, Boolean canDispatchOnThisThread)
   at System.ServiceModel.Channels.SingletonChannelAcceptor`3.Enqueue(QueueItemType item, Action dequeuedCallback, Boolean canDispatchOnThisThread)
   at System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.CompleteParseAndEnqueue(IAsyncResult result)
   at System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.HandleParseIncomingMessage(IAsyncResult result)
   at System.Runtime.AsyncResult.AsyncCompletionWrapperCallback(IAsyncResult result)
   at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
   at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
   at System.ServiceModel.Channels.HttpInput.ParseMessageAsyncResult.OnRead(IAsyncResult result)
   at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
   at System.Web.Hosting.ReadAsyncResult.Complete(Int32 bytesRead, Int32 hresult, IntPtr pbAsyncReceiveBuffer, Boolean synchronous)
   at System.Web.Hosting.IIS7WorkerRequest.OnAsyncCompletion(Int32 bytesCompleted, Int32 hresult, IntPtr pAsyncCompletionContext)
   at System.Web.Hosting.PipelineRuntime.AsyncCompletionHandler(IntPtr rootedObjectsPointer, Int32 bytesCompleted, Int32 hresult, IntPtr pAsyncCompletionContext)
</StackTrace><ExceptionString>System.ServiceModel.Security.MessageSecurityException: The nonce is invalid or replayed.</ExceptionString></Exception></TraceRecord>

客户端收到“从另一方收到的不安全或错误安全的故障System.ServiceModel.FaultException:验证邮件的安全性时发生错误”

有人可以帮忙吗?

0 个答案:

没有答案