.net remoting: No message was deserialized prior to calling the DispatchChannelS
Server对象
C# code
public class Server : MarshalByRefObject
{
public string Name { get; private set; }
public Server()
{ Name = "aaa"; }
}
服务器程序
C# code
static void Main(string[] args)
{
//发布服务
BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();
IDictionary props = new Hashtable();
props["port"] = 17017;
var channel = new HttpChannel(props, clientProv, serverProv);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Server), "server", WellKnownObjectMode.Singleton);
Console.WriteLine("服务已开启");
Console.ReadLine();
}
客户端程序
C# code
static void Main(string[] args)
{
var server = (Server)Activator.GetObject(typeof(Server),
string.Format("http://localhost:{0}/{1}", 17017,"server"));
var dict = System.Runtime.Remoting.Channels.ChannelServices.GetChannelSinkProperties(server);
dict["timeout"] = 50000;
Console.WriteLine(server.Name);
Console.ReadLine();
}
没有app.config文件。
现在我用的的是HTTP channel。先运行服务器程序,再运行客户端程序,客户端会在Console.WriteLine(server.Name)时出错。异常消息是
System.ArgumentNullException: ?????? DispatchChannelSink ???????????????????????????????????????
?????????: requestMsg
??? System.Runtime.Remoting.Channels.DispatchChannelSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)
??? System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)
??? System.Runtime.Remoting.Channels.Http.HttpServerTransportSink.ServiceRequest(Object state)
??? System.Runtime.Remoting.Channels.SocketHandler.ProcessRequestNow()
经我猜测,应该是No message was deserialized prior to calling the DispatchChannelSink. Parameter name: requestMsg。
如果改为tcp channel,即
var channel = new HttpChannel(props, clientProv, serverProv);改为TcpChannel
string.Format("http://localhost:{0}/{1}", 17017,"server"));改为tcp://
这样就不会出异常了。
但是如果用TCP,超时设置就无效。尽管http://msdn.microsoft.com/zh-cn/library/bb397839.aspx说tcp是支持timeout的,但我测试不出来。要造成超时,可以把localhost改成任意一个ip。
具体用TCP还是HTTP我无所谓。现在的问题是,TCP超时设置无效;HTTP有ArgumentNullException的问题。另外我不是本机通讯,所以不考虑ipx。
求各位大大帮我解决任何一个问题。。
------解决方案--------------------
HttpChannel与BinaryClientFormatterSinkProvider冲突,所以乱码了。
TcpChannel配BinaryClientFormatterSinkProvider或HttpChannel配Soap。