我在使用 .net 远程处理时遇到了问题(是的,这是我第一次尝试使用它,所以如果这是一个简单的解决方案的愚蠢问题,我很抱歉),我现在已经搜索了两天并且可以找不到任何解决我的问题的方法,所以如果我在某个地方错过了答案,我会提前道歉。
我正在使用以下代码构建服务器/客户端系统:
服务器:
public Server() {
DbConnector db = new DbConnector();
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = DbConnector.PORT;
TcpChannel channel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(channel, true);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(DbConnector), "test",
WellKnownObjectMode.SingleCall);
}
客户:
public Form1() {
InitializeComponent();
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 0;
props["ip"] = DbConnector.IP;
TcpChannel channel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(channel, true);
db = (DbConnector)Activator.GetObject(typeof(DbConnector), "tcp://188.x.x.x/test");
InitRest();
}
每当我在本地尝试(使用 localhost 和 WAN IP)时,一切正常,但只要我在不同的机器上尝试,客户端就会抛出异常。
{System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.0.100:9090
Server stack trace:
ved System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
ved System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
ved System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket(EndPoint ipEndPoint)
ved System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket()
ved System.Runtime.Remoting.Channels.RemoteConnection.GetSocket()
ved System.Runtime.Remoting.Channels.SocketCache.GetSocket(String machinePortAndSid, Boolean openNew)
ved System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.SendRequestWithRetry(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream)
ved System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, ITransportHeaders& responseHeaders, Stream& responseStream)
ved System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMessage(IMessage msg)
Exception rethrown at [0]:
ved ECTunes.Database_Reader.Form1.InitRest() i d:\Dropbox\visual studio 2012\Projects\ECTunes\Database Communicator v0.2\Database Reader\Form1.cs:linje 69
ved ECTunes.Database_Reader.Form1..ctor() i d:\Dropbox\visual studio 2012\Projects\ECTunes\Database Communicator v0.2\Database Reader\Form1.cs:linje 56
ved ECTunes.Database_Reader.Program.Main() i d:\Dropbox\visual studio 2012\Projects\ECTunes\Database Communicator v0.2\Database Reader\Program.cs:linje 16
ved System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
ved System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
ved Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
ved System.Threading.ThreadHelper.ThreadStart_Context(Object state)
ved System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
ved System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
ved System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
ved System.Threading.ThreadHelper.ThreadStart()}
每当调用 DbConnector 的构造函数时,我都会打印出文本,并且我可以看到客户端与服务器联系,但我被困在那里。
经过更多测试后,问题似乎不在于连接本身,而是当我调用一个从系统的实体框架部分返回一个对象的函数时,该对象是生成的代码。我已经用 [Serializable] 和 :MarshalByRefObject 修改了类,如下所示:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Database
{
using System;
using System.Collections.Generic;
[Serializable]
public partial class customer: MarshalByRefObject
{
public customer()
{
this.car = new HashSet<car>();
}
public int customerId { get; set; }
public string name { get; set; }
public virtual ICollection<car> car { get; set; }
}
}
然而,当客户端程序调用一个返回“客户”对象的函数并尝试访问它时,客户端会抛出上述异常。
我的猜测是,由于该类是从 MarshalByRefObject 派生的,所以它是回调部分。
我也尝试为客户端通道使用特定端口,但无济于事。
我希望有人对此有解决方案,最后可能只是我缺少关键部分。