一些远程站点通过WCF发送二进制数据的问题

本文关键字:WCF 二进制 数据 问题 程站点 站点 | 更新日期: 2023-09-27 18:11:59

我遇到的这个问题并不总是发生,但是对于特定的WCF客户端,我记录了以下异常:

System.ServiceModel.Security.MessageSecurityException

从另一方接收到不安全或不正确安全的故障聚会,派对故障码和详细信息请参见内部FaultException。

System.ServiceModel.Security。MessageSecurityException:一个不安全的或接收到对方错误保护的错误。处理步骤看到inner FaultException表示故障代码和详细信息。--->System.ServiceModel.FaultException:验证时发生错误消息的安全性。——内部异常堆栈跟踪结束——服务器栈跟踪:在System.ServiceModel.Security.SecuritySessionClientSettings 1.SecurityRequestSessionChannel.ProcessReply(Message reply, TimeSpan timeout, SecurityProtocolCorrelationState correlationState) at System.ServiceModel.Security.SecuritySessionClientSettings . securityrequestsessionchannel . request (Message消息,TimeSpan超时)System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan超时)System.ServiceModel.Channels.ServiceChannel。调用(字符串操作,单向布尔,ProxyOperationRuntime操作,对象[]Object[] out, TimeSpan timeout)System.ServiceModel.Channels.ServiceChannel。调用(字符串操作,单向布尔,ProxyOperationRuntime操作,对象[]Object[] out) atSystem.ServiceModel.Channels.ServiceChannelProxy.InvokeService (IMethodCallMessage方法调用,ProxyOperationRuntime操作)System.ServiceModel.Channels.ServiceChannelProxy.Invoke (IMessage[0]: atSystem.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage (IMessagereqMsg; IMessageSystem.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke (MessageData&msgData, Int32类型)Proxy.ExecutionResultsUpdateService.IExecutionResultsUpdateService.SaveBinaryResults (SaveBinaryResultsRequest请求)Proxy.ExecutionResultsUpdateService.ExecutionResultsUpdateServiceClient。> Proxy.ExecutionResultsUpdateService.IExecutionResultsUpdateService.SaveBinaryResults (SaveBinaryResultsRequest请求)LoggingExecutionResultsUpdateServiceClient。SaveBinaryResults (SaveBinaryResultsRequest请求)在ClientServices.ResultsController.Send(IEnumerable ' 1startautomaticagent () atMainApplication.Start ()

客户端正在抛出异常。

SaveBinaryResults的服务代码:

[ServiceContract(Name="IExecutionResultsUpdateService")]
public interface IExecutionResultsUpdateService_v1_0_1 : IExecutionResultsUpdateService
{
    [OperationContract(Name="SaveBinaryResults")]
    void SaveSQLResults(byte[] data, string serverXml, Guid clientInstanceId, int moduleId, Guid batchIdentifier);
}
   public void SaveSQLResults(byte[] data, string serverXml, Guid clientInstanceId, int moduleId, Guid batchIdentifier)
    {
        DataSet dataSet = (DataSet)new BinaryFormatter().Deserialize(new MemoryStream(data));
        try
        {
            XmlDocument serverDocument = new XmlDocument();
            serverDocument.LoadXml(serverXml);
            executionResultsRepository.Save(dataSet, serverDocument);
            executionHistoryRepository.SaveModuleExecution(clientInstanceId, moduleId, true, null, batchIdentifier);
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            executionHistoryRepository.SaveModuleExecution(clientInstanceId, moduleId, false, ex.ToString(), batchIdentifier);
            throw new FaultException<System.Data.SqlClient.SqlException>(ex, new FaultReason(ex.Message));
        }
    }

IExecutionResultsUpdateService的客户端代理配置为:

<binding name="WSHttpBinding_IExecutionResultsUpdateService"
  closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"
  sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false"
  hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288"
  maxReceivedMessageSize="65536" messageEncoding="Mtom" textEncoding="utf-8"
  useDefaultWebProxy="true" allowCookies="false">
  <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
  <reliableSession ordered="true" inactivityTimeout="00:10:00"
    enabled="false" />
  <security mode="Message">
    <transport clientCredentialType="Windows" proxyCredentialType="None"
      realm="" />
    <message clientCredentialType="UserName" negotiateServiceCredential="true"
      algorithmSuite="Default" establishSecurityContext="true" />
  </security>
</binding>

业务配置为

<service behaviorConfiguration="ServiceBehavior" 
    name="Services.ExecutionResultsUpdateService">
    <endpoint binding="wsHttpBinding" 
      bindingConfiguration="MtomWsHttpBindingForBigArrays" 
      contract="Services.IExecutionResultsUpdateService_v1_0_1" 
      address="/ExecutionResults"/>
    <endpoint binding="wsHttpBinding" 
      bindingConfiguration="wsHttpBinding" 
      contract="Services.IExecutionResultsUpdateService_v1_0_1"/>
    <host>
      <baseAddresses>
      <add baseAddress="http://[ipaddress]/ExecutionResultsUpdateService.svc"/>
      </baseAddresses>
    </host>
</service>
   <binding name="MtomWsHttpBindingForBigArrays" 
         maxReceivedMessageSize="2147483647" 
         messageEncoding="Mtom" 
         receiveTimeout="5"              
         useDefaultWebProxy="false">
          <readerQuotas maxDepth="2147483647" 
             maxStringContentLength="2147483647" 
             maxArrayLength="2147483647" 
             maxBytesPerRead="2147483647" 
             maxNameTableCharCount="2147483647" />
          <security mode="Message">
            <message clientCredentialType="UserName" />
          </security>
    </binding>

客户端代理的一部分看起来像这样:

public interface IExecutionResultsUpdateService
{
    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IExecutionResultsUpdateService/ReportPackageCompletion", ReplyAction="http://tempuri.org/IExecutionResultsUpdateService/ReportPackageCompletionResponse" +
        "")]
    [System.ServiceModel.XmlSerializerFormatAttribute()]
    void ReportPackageCompletion(string clientInstanceId, int packageId);
    // CODEGEN: Parameter 'data' requires additional schema information that cannot be captured using the parameter mode. The specific attribute is 'System.Xml.Serialization.XmlElementAttribute'.
    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IExecutionResultsUpdateService/SaveBinaryResults", ReplyAction="http://tempuri.org/IExecutionResultsUpdateService/SaveBinaryResultsResponse")]
    [System.ServiceModel.XmlSerializerFormatAttribute()]
    Proxy.ExecutionResultsUpdateService.SaveBinaryResultsResponse SaveBinaryResults(Proxy.ExecutionResultsUpdateService.SaveBinaryResultsRequest request);
}

注意:两个端点是因为我必须支持这个服务的两个版本。绑定"MtomWsHttpBindingForBigArrays"的服务是导致我的问题的原因。(另一个做同样的事情,但接受一个数据集作为参数,除了一个字节[],我很快发现这是低效和不好的做法。)

有没有人知道我是否可以控制这个异常?我可以对客户端或服务配置文件进行任何配置更改吗?

如果您还需要我提供更多的信息,请告诉我。

一些远程站点通过WCF发送二进制数据的问题

每次客户端调用时都抛出错误吗?如果是,请检查客户端的时钟/时区/日期