2013-03-04 71 views
1

我在我的WCF服務器上執行回調的方法如下所示。這工作正常,包括回調。客戶端上的wcf服務器啓動方法

但是我如何從服務器啓動客戶端上的方法的執行。即代碼段C#按鈕代碼我想有下面。

目前我得到的錯誤是:

Object reference not set to an instance of an object. 

可能是我的架構是所有的錯在這裏和WCF客戶端也需要有建立在一個WCF服務器? WCF服務器上

C#方法WCF服務器上

public void ChatToServer(string texttoServer) // send some text to the server 
{ 
    Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", 1); 

    try 
    { 
     IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel<IMyContractCallBack>(); 
     callback.ChatToClient("your message: " + texttoServer + " has been recieved");   

     Logging.Write_To_Log_File("Exit", MethodBase.GetCurrentMethod().Name, "", "", "", 1); 

    } 
    catch (Exception ex) 
    { 
     Logging.Write_To_Log_File("Error", MethodBase.GetCurrentMethod().Name, "", "", ex.ToString(), 2); 
    } 

} 

C#按鈕的代碼,我想有

private void radButtonSend_Click(object sender, EventArgs e) 
     { 

      try 
      { 
       Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", 1); 

       IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel<IMyContractCallBack>(); 
       callback.ChatToClient(radTextBox2Send.Text); 

       Logging.Write_To_Log_File("Exit", MethodBase.GetCurrentMethod().Name, "", "", "", 1); 
      } 

      catch (Exception ex) 
      { 
       Logging.Write_To_Log_File("Error", MethodBase.GetCurrentMethod().Name, "", "", ex.ToString(), 2); 
       MessageBox.Show(ex.Message.ToString()); 

      } 

     } 

WCF服務配置

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="Throttled"> 
      <serviceMetadata httpGetEnabled="False" /> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
      <serviceThrottling maxConcurrentCalls="100000" maxConcurrentInstances="100000" maxConcurrentSessions="100000" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
    <service name="WCFService.WCFJobsLibrary" behaviorConfiguration="Throttled" > 
     <host> 
     <baseAddresses> 
      <add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFService/WCFJobsLibrary/" /> 
     </baseAddresses> 
     </host> 
     <endpoint name ="duplexendpoint" 
      address ="" 
      binding ="wsDualHttpBinding" 
      contract ="WCFService.IWCFJobsLibrary"/> 
     <endpoint name ="MetaDataTcpEndpoint" 
       address="mex" 
       binding="mexHttpBinding" 
       contract="IMetadataExchange"/> 
    </service> 
    </services> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

WCF客戶端配置

<configuration> 
    <system.serviceModel> 
    <bindings> 
     <wsDualHttpBinding> 
     <binding name="duplexendpoint" closeTimeout="00:01:00" openTimeout="00:01:00" 
      receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" 
      transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" 
      textEncoding="utf-8" useDefaultWebProxy="true"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
      maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" /> 
      <security mode="Message"> 
      <message clientCredentialType="Windows" negotiateServiceCredential="true" 
       algorithmSuite="Default" /> 
      </security> 
     </binding> 
     </wsDualHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:8732/Design_Time_Addresses/WCFService/WCFJobsLibrary/" 
     binding="wsDualHttpBinding" bindingConfiguration="duplexendpoint" 
     contract="ServiceReference1.IWCFJobsLibrary" name="duplexendpoint"> 
     </endpoint> 
    </client> 
    </system.serviceModel> 
</configuration> 

合同

namespace WCFService 
{ 
    [ServiceContract(CallbackContract = typeof(IMyContractCallBack))] 
    public interface IWCFJobsLibrary 
    { 
     [OperationContract(IsOneWay = true)] 
     void ChatToServer(string texttoServer); // send some text to the server 

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

     [OperationContract(IsOneWay = false)] 
     int ChatToServerWithResult(string texttoServer); // send some text to the server and send back success indication 

    } 

    public interface IMyContractCallBack 
    { 
     [OperationContract(IsOneWay = true)] 
     void CallBackFunction(string str); 


     [OperationContract(IsOneWay = true)] 
     void ChatToClient(string str); 

    } 
} 
+1

調試代碼並嘗試瞭解哪個對象拋出該異常。你有1個對象沒有正確初始化,在你的代碼的某個地方。 – 2013-03-04 14:22:00

+0

其行(在radButtonSend_Click中)IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel (); - 我試圖從服務器啓動客戶端上的方法 – user1438082 2013-03-04 14:23:24

+1

調試代碼並檢查** OperationContext **或** OperationContext.Current **是** null **。 PS:如果你可以發佈你的WCF配置(對於客戶端應用和WCF服務) – 2013-03-04 14:27:21

回答

1

在你的情況我建議你使用wsDualHttpBinding:一個安全,可互操作的結合,設計用於與雙工服務合同使用這允許服務和客戶端發送和接收消息。 請參閱示例:http://blog.binarymist.net/2010/05/23/duplex-communication-and-callbacks-in-wcf/

+0

嗨 - 我使用雙工 - - 在第一種方法客戶端發送請求和此方法運行回調客戶端。 - 在第二種方法(駐留在服務器上 - 我想告訴客戶端運行一些代碼) - – user1438082 2013-03-04 14:27:35

+1

哦,你可以顯示你的配置設置和數據合同嗎? – Alex 2013-03-04 14:31:55

+0

添加配置和合同 – user1438082 2013-03-04 14:34:33

相關問題