2011-06-22 69 views
0

我一直在谷歌搜索了一天,似乎無法找到答案。希望有人能夠對此有所瞭解。我試圖實現一個簡單的WCF客戶端服務器回調,在客戶端和服務器端都有控制檯應用程序。該操作在服務器上執行,並且除了客戶端上沒有執行回調之外,一切都可以正常工作。即它永遠不會寫入「Callback called !!!」,並且放置在回調中的斷點永遠不會跳閘。客戶只需寫下「完成」。並等待用戶輸入。未執行WCF回調

我確定這很簡單。任何幫助將不勝感激。謝謝!

//SERVER CODE: 
namespace NodeServiceLib 
{ 
    public interface ISomeCallbackContract 
    { 
     [OperationContract] 
     void OnCallback(); 
    } 

    [ServiceContract(CallbackContract = typeof(ISomeCallbackContract))] 
    public interface IMyContract 
    { 
     [OperationContract] 
     void DoSomething(); 
    } 

    public class NodeService : IMyContract 
    { 
     public void DoSomething() 
     { 
      Console.WriteLine("I'm doing something!!!"); 
     } 
    } 
} 

<configuration> 
    <system.web> 
    <compilation debug="true"/> 
    </system.web> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <bindings/> 
    <services> 
     <service name="NodeServiceLib.NodeService" behaviorConfiguration="MEX"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8000/Node" /> 
      <add baseAddress="net.tcp://localhost:8001/Node" /> 
      </baseAddresses> 
     </host> 
     <endpoint 
      address="MyContract" 
      binding="netTcpBinding" 
      contract="NodeServiceLib.IMyContract" 
     /> 
     <endpoint 
      address="MEX" 
      binding="mexHttpBinding" 
      contract="IMetadataExchange" 
     /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="MEXGET"> 
      <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="True"/> 
      <!-- To receive exception details in faults for debugging purposes, 
      set the value below to true. Set to false before deployment 
      to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="False"/> 
     </behavior> 
     <behavior name="MEX"> 
      <serviceMetadata/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup</configuration> 



//CLIENT CODE: 
namespace TestConsole 
{ 
    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] 
    class Callback : NodeServices.IMyContractCallback 
    { 
     public void OnCallback() 
     { 
      Console.WriteLine("Callback called!!!"); 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      System.Threading.Thread.Sleep(5000); // Give server time to spin up 

      Console.WriteLine("=== CLIENT ==="); 

      InstanceContext context = new InstanceContext(new Callback()); 
      NodeServices.MyContractClient proxy = new NodeServices.MyContractClient(context); 
      proxy.DoSomething(); 
      Console.WriteLine("Done."); 
      Console.ReadLine(); 
     } 
    } 
} 

回答

2

你不應該在服務器的DoSomething方法中調用回調方法嗎?

+0

是的,你會認爲這將是顯而易見的,但我工作的例子看起來好像是由WCF自動完成的,除非你想覆蓋併發模式。回想起來,solu5tion很簡單:添加 [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant) 服務定義,然後 公共無效DoSomething的(){ Console.WriteLine(「我做的事!!!「); ISomeCallbackContract callback = OperationContext.Current.GetCallbackChannel (); callback.OnCallback(); } 謝謝。 – hdt