WCF回调未被执行

时间:2011-06-22 16:54:53

标签: wcf wcf-client

我一直在谷歌搜索这一天,似乎无法找到答案。希望有人可以对此有所了解。我正在尝试实现一个简单的WCF客户端 - 服务器回调,在客户端和服务器端都有控制台应用程序。该操作在服务器上执行,一切似乎都正常,除了回调不在客户端上执行。即它永远不会写“Callback called !!!”,并且回调中的断点永远不会跳闸。客户只需写“完成”。并等待用户输入。

我确信这很简单。任何帮助将不胜感激。谢谢!

//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();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你不应该在服务器的DoSomething方法中调用回调方法吗?