2013-09-25 34 views
1

我有一個與.net遠程處理問題(是的,這是我第一次嘗試使用它,所以我很抱歉,如果這是一個簡單的解決方案一個愚蠢的問題)我一直在尋找兩天,並且找不到解決我的問題的方法,所以如果我在某處錯過了答案,我會提前道歉。.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(); 
} 

每當我(可以使用本地主機,但也儘量在本地與廣域網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派生出來的,所以它是回調部分。

我試圖爲客戶端通道使用特定的端口,但無濟於事。

我希望有人有這個解決方案,最終可能只是我缺少關鍵部分。

回答

0

我沒有通過使包裝類對象解決它,並利用它來進行的傳輸代替。多一點工作,但它的工作。顯然你不應該能夠通過這些渠道發送實體框架生成的對象。

0

我想我找到了它,這要感謝您提供您的樣品!

ChannelServices.RegisterChannel(channel, true); 

設置ensureSecurity假

ChannelServices.RegisterChannel(channel, false); 
+0

感謝您的回覆。我確實嘗試過,但沒有什麼區別。但是,我確實解決了這個問題,通過爲這些對象創建一個包裝類並將其用於傳輸。多一點工作,但它的工作。顯然你不應該能夠通過這些渠道發送實體框架生成的對象。 – andidegn