未执行WCF回调

未执行WCF回调,wcf,wcf-client,Wcf,Wcf Client,我已经在谷歌上搜索了一天,似乎找不到答案。希望有人能解释一下。我正在尝试实现一个简单的WCF客户端-服务器回调,在客户端和服务器端都有控制台应用程序。该操作在服务器上执行,除了回调没有在客户端执行之外,其他一切似乎都正常工作。也就是说,它从不写入“callbackcalled!!!”,并且放置在回调中的断点从不跳闸。客户端只写“完成”,并等待用户输入 我相信这很简单。任何帮助都将不胜感激。谢谢 //SERVER CODE: namespace NodeServiceLib { publi

我已经在谷歌上搜索了一天,似乎找不到答案。希望有人能解释一下。我正在尝试实现一个简单的WCF客户端-服务器回调,在客户端和服务器端都有控制台应用程序。该操作在服务器上执行,除了回调没有在客户端执行之外,其他一切似乎都正常工作。也就是说,它从不写入“callbackcalled!!!”,并且放置在回调中的断点从不跳闸。客户端只写“完成”,并等待用户输入

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

//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();
        }
    }
}
//服务器代码:
命名空间NodeServiceLib
{
公共接口回调契约
{
[经营合同]
void OnCallback();
}
[ServiceContract(CallbackContract=typeof(CallbackContract))]
公共接口IMyContract
{
[经营合同]
无效剂量();
}
公共类节点服务:IMyContract
{
公共无效剂量测定法()
{
Console.WriteLine(“我正在做某事!!!”;
}
}
}

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

是的,您可能认为这很明显,但我所使用的示例使它看起来好像是由WCF自动完成的,除非您希望覆盖并发模式。回想起来,解决方案很简单:将[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]添加到服务定义中,然后将public void DoSomething(){Console.WriteLine(“我正在做某事!!!”);ISomeCallbackContract callback=OperationContext.Current.GetCallbackChannel();callback.OnCallback();}谢谢。