2014-04-17 132 views
0

我想讓我的客戶端訂閱發生在我的服務器上的事件。參數少構造函數錯誤

我有一個看起來像這樣的接口:

public delegate void RemoteEventHandler(object sender, ClientEventArgs args); 


[Serializable] 
public class ClientEventArgs : EventArgs 
{ 
    public ClientEventArgs() 
    { } 

    public ClientEventArgs(Client _client) 
    { 
     MyClient = _client; 
    } 
    public Client MyClient { get; set; } 
} 

public interface IMonitor 
{ 
    event RemoteEventHandler RemoteEvent; 
} 

我的服務器類看起來是這樣的:

public class ConnectionManager : MarshalByRefObject, IMonitor 
{ 
    public event RemoteEventHandler RemoteEvent; 

    // call the below code when th event should fire. 
    if (RemoteEvent != null) 
      RemoteEvent(this, new ClientEventArgs(e.MyClient)); 
} 

然後把我的渠道了我做這在服務器上:

BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider(); 
provider.TypeFilterLevel = TypeFilterLevel.Full; 
IDictionary props = new Hashtable(); 
props["port"] = 5001; 
TcpChannel channel = new TcpChannel(props, null, provider); 
ChannelServices.RegisterChannel(channel, false); 
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ConnectionManager), 
ConnectionManager", 
WellKnownObjectMode.Singleton); 

並在客戶端設置渠道和訂閱事件:

TcpChannel channel = new TcpChannel(); 
     ChannelServices.RegisterChannel(channel, false); 
     _monitorObject = (IMonitor)Activator.GetObject(
      typeof(IMonitor), 
      "tcp://localhost:5001/ConnectionManager"); 

_monitorObject.RemoteEvent += _monitorObject_RemoteEvent; 

任何人都可以解釋哪裏出問題了嗎?

例外:

system.missingMethodException而是未處理的HResult = -2146233069消息=此對象定義無參數的構造函數。使用Serializable當你需要無參數的構造函數:
來源= mscorlib程序

+1

異常是否有內部異常? –

+0

它無法加載主程序集或它的一個依賴項? – dev6754

+0

然後你可能會缺少一個引用程序集。你只複製了exe文件嗎? –

回答

1

要回答你的最後一個問題。所以這一塊肯定會失敗:

[Serializable] 
public class ClientEventArgs : EventArgs 
{ 
    public ClientEventArgs(Client _client) 
    { 
     MyClient = _client; 
    } 
    public Client MyClient { get; set; } 
} 

您需要添加一個參數的構造函數:

[Serializable] 
public class ClientEventArgs : EventArgs 
{ 
    public ClientEventArgs() 
    { } 

    public ClientEventArgs(Client _client) 
    { 
     MyClient = _client; 
    } 
    public Client MyClient { get; set; } 
} 
+0

感謝您的迴應,重建後仍然拋出相同的錯誤。 – dev6754

+0

你確定沒有其他地方存在這個問題嗎?也許這個例外會告訴失敗的班級? –

+0

[Serializable]的唯一參考是來自上面的類。謝謝你的耐心 ! – dev6754

1

我的錢是不是有一個缺省的/參數的構造函數的ConnectionManager類。遠程處理基礎架構需要能夠在服務器端創建它的一個實例。

+0

感謝您的支持,它編譯,事件永遠不會發生,但至少它編譯! – dev6754