2011-04-10 41 views
0

仍然在我的wcf應用程序的服務器端掙扎。 :)有點幫助實例化一個wcf對象,事件不起作用

正如你可以從代碼中看到的,我試圖觸發一個事件,只要客戶端使得StartConnection。但不知何故似乎我不能實例服務器對象,因爲它給了我這個3錯誤。

請建議,什麼是正確的方法來做到這一點?謝謝! :)

namespace server2 
{ 
    public partial class Form2 : Form 
    { 
     public Form2() 
     { 
      InitializeComponent(); 
      myServer.eventHappened += new EventHandler(eventFunction); 
      // 'server2.IfaceClient2Server' does not contain a definition for 'eventHappened' and no extension method 'eventHappened' accepting a first argument of type 'server2.IfaceClient2Server' could be found (are you missing a using directive or an assembly reference?) 
     } 

     IfaceClient2Server myServer = new ServerClass(); 
     // The type or namespace name 'eventCaller' could not be found (are you missing a using directive or an assembly reference?) 

     void eventFunction(object sender, EventArgs e) 
     { 
      label1.Text = myServer.clientName; 
      // 'server2.IfaceClient2Server' does not contain a definition for 'clientName' and no extension method 'clientName' accepting a first argument of type 'server2.IfaceClient2Server' could be found (are you missing a using directive or an assembly reference?) 
     } 

    } 




    class ServerClass : IfaceClient2Server 
    { 

     public event EventHandler eventHappened; 

     //IfaceServer2Client callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>(); 

     public string clientName; 

     public void StartConnection(string name) 
     { 
      clientName = name; 
      eventHappened(this, new EventArgs()); 
      MessageBox.Show(clientName + " has connected!"); 
     } 

     public void Message_Cleint2Server(string msg) 
     { 
     } 

     public void Message2Client(string msg) 
     { 
     } 

    } 




    [ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)] 


    public interface IfaceClient2Server   ///// what comes from the client to the server. 
    { 
     [OperationContract(IsOneWay = true)] 
     void StartConnection(string clientName); 

     [OperationContract(IsOneWay = true)] 
     void Message_Cleint2Server(string msg); 
    } 


    public interface IfaceServer2Client   ///// what goes from the sertver, to the client. 
    { 
     [OperationContract(IsOneWay = true)] 
     void AcceptConnection(); 

     [OperationContract(IsOneWay = true)] 
     void RejectConnection(); 

     [OperationContract(IsOneWay = true)] 
     void Message_Server2Client(string msg); 
    } 

} 

回答

1

您正在將myserver創建爲IfaceClient2Server的實例,該實例沒有事件或客戶端名稱字符串的定義。應該是

ServerClass myServer = new ServerClass(); 

您仍然可以訪問接口方法,因爲ServerClass繼承自IfaceClient2Server。

+0

謝謝,它幫助了很多! :)只剩下一件事......這行「IfaceServer2Client callback = OperationContext.Current.GetCallbackChannel ();」導致「System.NullReferenceException未處理」...它應該如何改變它的工作?再次感謝! – Roger 2011-04-10 17:48:39

+0

您的一個服務接口(IfaceServer2Client)需要將CallbackContract選項設置爲ServiceControl的一部分。你可以在http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.current.aspx找到一個例子。 – 2011-04-10 17:57:49