[英]Passing a stream to a method via .NET remoting
讓我簡要描述一下情況。
首先,我有一個使用此方法的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 ?? "";
}
這是GetBridgeObject()方法的內容(將是遠程客戶端):
private IBridge GetBridgeObject()
{
return (IBridge)Activator.GetObject(typeof(IBridge), "tcp://localhost:4626/RemoteBridge");
}
接下來,有一個包含此方法的橋接進程(將是遠程服務器):
public void StartService()
{
//-- Initialize .NET remoting
var oServerFormatSinkProvider = new BinaryServerFormatterSinkProvider();
oServerFormatSinkProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary aoProps = new Hashtable();
aoProps["port"] = 4626;
aoProps["timeout"] = "-1";
aoProps["name"] = "serverChan";
TcpServerChannel channel = new TcpServerChannel(aoProps, oServerFormatSinkProvider);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof (Bridge), "RemoteBridge", WellKnownObjectMode.Singleton);
}
最后,在遠程網橋對象中,此方法:
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:此遠程代理沒有通道接收器,這意味着服務器沒有正在偵聽的注冊服務器通道,或者此應用程序沒有合適的客戶端通道可以與服務器通信。 在System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(IMessage reqMsg)在System.Runtime.Remoting.Proxies。 (MessageData&msgData,Int32類型)位於System.IO.Stream.Read(字節[]緩沖區,Int32偏移量,Int32計數)
我知道我可以通過.NET遠程傳遞流,因為在網橋中,還有其他方法可以返回流並且可以很好地工作。
我想問題出在注冊頻道時是在遠程服務器或客戶端中的某個地方,但是經過兩天的研究和測試,我仍然沒有找到答案。
在此先感謝您的幫助。
由於我遇到了類似的問題,並且也需要解決方案,因此我終於找到了解決方案:需要對客戶端通道進行不同的注冊才能使其正常工作。
// 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);
有趣的是
感興趣的是,為什么該解決方案可以在生產代碼中使用,所以我的答案為什么會被否決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.