2011-06-22 74 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