通过 namedPipe WCF 的字节 []/流出现错误

Byte[]/stream through namedPipe WCF is faulted

我正在开发命名管道双工 WCF 服务。

对于基本的事情,客户端和服务器通信没有问题。服务器可以回调客户端发送给他的字符串,并且它没有任何问题。 但是,当我希望它发送带有字节 [] 或流的位图时,一切都出错了。请注意,我尝试使用流,因为 byte[] 不工作...... 在服务器端,字节[]/流的生成没有问题。 但是当服务器向客户端发送 byte[]/stream 时,如果 byte[]/stream 为空,它会通过,但是当它有数据时,它就会出错。

我已经检查了我所有的配置,并尝试设置一个大的 buffer/message/poolsize/stringcontent/arraylenght/byteperread/whatever size/lenght 因为我知道这是 WCF 中的一个经典问题。

这是我的 WCF 配置文件主要部分的 C/P。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <netNamedPipeBinding>
        <binding name="NamedPipeBinding_ICameraService" closeTimeout="00:05:00" openTimeout="00:20:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" maxConnections="10" maxBufferPoolSize="500000000" maxBufferSize="500000000" maxReceivedMessageSize="500000000">
          <readerQuotas maxDepth="32" maxStringContentLength="500000000" maxArrayLength="500000000" maxBytesPerRead="500000000" maxNameTableCharCount="500000000"/>
          <security mode="Transport"/>
        </binding>
      </netNamedPipeBinding>
    </bindings>
    <services>
      <service name="EOSDigital.CameraService" behaviorConfiguration="MEX">
        <endpoint address="net.pipe://localhost/EOSDigital/CameraService" binding="netNamedPipeBinding" bindingConfiguration="NamedPipeBinding_ICameraService" contract="EOSDigital.ICameraService"/>

        <endpoint address="net.pipe://localhost/EOSDigital/CameraService/mex" binding="mexNamedPipeBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MEX">
          <serviceMetadata httpGetEnabled="False"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

这里是服务回调契约

[ServiceContract]
public interface ICameraServiceCallBack
{
    [OperationContract(IsOneWay = true)]
    void CallBackFunction(string str);

    [OperationContract(IsOneWay = true)]
    void LiveviewUpdated(byte[] img);
}

这是我的服务合同声明。

[ServiceContract(CallbackContract = typeof(ICameraServiceCallBack))]
public interface ICameraService

我不会放所有东西,这太大了。

这就是我的使用方式

private void CurrentCamera_LiveViewUpdated(object sender, Stream img)
{
    MemoryStream data = new MemoryStream();
    img.CopyTo(data);

    _callback = OperationContext.Current.GetCallbackChannel<ICameraServiceCallBack>();

    _callback.CallBackFunction("this is a test"); // ok

    _callback.LiveviewUpdated(data.ToArray()); //Faulted
}

我从佳能数码相机获取流,它大约是字节[146242]。当我发送一个字节 [10] 时,它起作用了。 这一定是大小问题,我想我在配置文件中遗漏了一些东西......

我还尝试生成并查看我的服务的 scvclog 文件,以查看发生的故障异常的一些详细信息。 但是,嗯……一行不少于 50k 个字符。这是不可读的。

谢谢。

检查您的客户端 WCF 配置。 您的客户端配置必须与您的主机具有相同的 netNamedPipeBinding。

将其放入您的客户端配置文件

<netNamedPipeBinding>
  <binding name ="duplexEndpoint" closeTimeout="00:05:00" openTimeout="00:20:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" transactionProtocol="OleTransactions"
          hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="50000000" maxBufferSize="50000000" maxConnections="10" maxReceivedMessageSize="50000000">
    <readerQuotas maxDepth="32" maxStringContentLength="50000000" maxArrayLength="50000000" maxBytesPerRead="50000000" maxNameTableCharCount="50000000"/>
  </binding>
</netNamedPipeBinding>

这必须放在 serviceModel.Bindings 括号下面。

然后,绑定端点括号中的配置

bindingConfiguration="duplexEndpoint" 

这应该符合您的预期。