C# 通过.NET远程处理将流传递给方法

C# 通过.NET远程处理将流传递给方法,c#,.net-4.0,stream,remoting,channel,C#,.net 4.0,Stream,Remoting,Channel,让我简要描述一下情况 首先,我有一个使用此方法的WCF RestFul Web服务: [WebInvoke(UriTemplate = "/Dynamic/{*sParameter}", Method = "POST", ResponseFormat = WebMessageFormat.Json)] public string ExecuteWebAPIRequest(string sParameter, Stream streamPost) { ...

让我简要描述一下情况

首先,我有一个使用此方法的WCF RestFul Web服务:

[WebInvoke(UriTemplate = "/Dynamic/{*sParameter}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    public string ExecuteWebAPIRequest(string sParameter, Stream streamPost)
    {
        ...

        var oClientFormatSinkProvider = new BinaryClientFormatterSinkProvider();
        IDictionary aoProps = new Hashtable();
        aoProps["port"] = 4626;
        aoProps["timeout"] = "-1";
        aoProps["name"] = "clientChan";
        TcpClientChannel channel = new TcpClientChannel(aoProps, oClientFormatSinkProvider);
        ChannelServices.RegisterChannel(channel, false);

        //-- Call Bridge
        string result = GetBridgeObject().ExecuteWebAPIRequest(sIpAddress, streamPost, sParameter);

        //-- Return result
        return result ?? "";
    }
public string WUPP_ExecuteWebAPIRequestI(string sPPInstanceName, Stream oInputStream, string sParameter)
    {
        ...

        int read = oInputStream.Read(buffer, 0, buffer.Length); //That's where the problem is
        ...    
    }
下面是GetBridgeObject()方法(即远程处理客户端)的内容:

接下来是包含此方法的桥接进程(即远程处理服务器):

最后,在远程网桥对象中,此方法:

[WebInvoke(UriTemplate = "/Dynamic/{*sParameter}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    public string ExecuteWebAPIRequest(string sParameter, Stream streamPost)
    {
        ...

        var oClientFormatSinkProvider = new BinaryClientFormatterSinkProvider();
        IDictionary aoProps = new Hashtable();
        aoProps["port"] = 4626;
        aoProps["timeout"] = "-1";
        aoProps["name"] = "clientChan";
        TcpClientChannel channel = new TcpClientChannel(aoProps, oClientFormatSinkProvider);
        ChannelServices.RegisterChannel(channel, false);

        //-- Call Bridge
        string result = GetBridgeObject().ExecuteWebAPIRequest(sIpAddress, streamPost, sParameter);

        //-- Return result
        return result ?? "";
    }
public string WUPP_ExecuteWebAPIRequestI(string sPPInstanceName, Stream oInputStream, string sParameter)
    {
        ...

        int read = oInputStream.Read(buffer, 0, buffer.Length); //That's where the problem is
        ...    
    }
正如代码片段中所述,当我尝试读取流时出现问题,我得到以下错误:

异常:System.Runtime.Remoting.RemotingException:此远程代理没有通道接收器,这意味着服务器没有正在侦听的注册服务器通道,或者此应用程序没有合适的客户端通道与服务器对话。 at System.Runtime.Remoting.Proxy.RemotingProxy.InternalInvoke(IMethodCallMessage reqMcmMsg、布尔useDispatchMessage、Int32 callType) at System.Runtime.Remoting.proxy.RemotingProxy.Invoke(IMessage reqMsg) at System.Runtime.Remoting.proxy.RealProxy.PrivateInvoke(MessageData&msgData,Int32类型) 在System.IO.Stream.Read(字节[]缓冲区,Int32偏移量,Int32计数)

我确信我可以通过.NETRemoting传递流,因为在网桥中,还有其他方法可以返回流,并且效果很好

我想问题出在我注册频道时远程处理服务器或客户端的某个地方,但经过两天的研究和测试,我仍然没有找到答案


提前感谢你的帮助

由于我遇到了类似的问题,也需要一个解决方案,我最终找到了一个解决方案:客户端通道需要以不同的方式注册才能正常工作

        // Creating a custom formatter for a TcpChannel sink chain.
        BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
        BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
        provider.TypeFilterLevel = TypeFilterLevel.Full;

        Dictionary<string, object> dict = new Dictionary<string, object>();
        dict.Add("typeFilterLevel", "Full");
        dict.Add("port", "0");

        ChannelServices.RegisterChannel(new TcpChannel(dict, clientProvider, provider), false);
//为TCP通道接收器链创建自定义格式化程序。
BinaryServerFormatterSinkProvider=新的BinaryServerFormatterSinkProvider();
BinaryClientFormatterSinkProvider clientProvider=新的BinaryClientFormatterSinkProvider();
provider.TypeFilterLevel=TypeFilterLevel.Full;
Dictionary dict=新字典();
添加(“类型过滤器级别”、“完整”);
添加(“端口”、“0”);
ChannelServices.RegisterChannel(新的TCPCChannel(dict、clientProvider、provider),false);
这里有趣的是

  • 通道是双向的
  • 端口0告诉远程处理框架决定应该使用哪个端口
  • 对于流的启动和下载,需要设置typeFilterLevel Full 设定

  • 我很想知道为什么我的答案在生产代码中得到了否决票。

    因为我有一个类似的问题,也需要一个解决方案,所以我终于找到了一个解决方案:需要以不同的方式注册客户端频道才能使其正常工作

            // Creating a custom formatter for a TcpChannel sink chain.
            BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            provider.TypeFilterLevel = TypeFilterLevel.Full;
    
            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict.Add("typeFilterLevel", "Full");
            dict.Add("port", "0");
    
            ChannelServices.RegisterChannel(new TcpChannel(dict, clientProvider, provider), false);
    
    //为TCP通道接收器链创建自定义格式化程序。
    BinaryServerFormatterSinkProvider=新的BinaryServerFormatterSinkProvider();
    BinaryClientFormatterSinkProvider clientProvider=新的BinaryClientFormatterSinkProvider();
    provider.TypeFilterLevel=TypeFilterLevel.Full;
    Dictionary dict=新字典();
    添加(“类型过滤器级别”、“完整”);
    添加(“端口”、“0”);
    ChannelServices.RegisterChannel(新的TCPCChannel(dict、clientProvider、provider),false);
    
    这里有趣的是

  • 通道是双向的
  • 端口0告诉远程处理框架决定应该使用哪个端口
  • 对于流的启动和下载,需要设置typeFilterLevel Full 设定
  • 我很想知道为什么我的答案在生产代码中得到了否决票