【问题标题】:WCF Callback between two services两个服务之间的 WCF 回调
【发布时间】:2012-04-01 04:44:27
【问题描述】:

我正在尝试在两个服务之间建立连接。 Service1 将向 service2 发送事件通知。

我想在这两个服务之间使用回调。 service1 将向 service2 发送事件,而 service2 将把事件传递给客户端。

是否可以在两个服务之间使用回调?

我已经查看了 wcf 回调的示例,但在服务到服务的通信之间找不到任何东西。

感谢任何帮助。请参阅下面的服务代码 1。 我的问题:其他服务的代码中包含回调的情况如何?

或者在这种情况下,它是充当客户端的服务之一吗?那应该是什么样子?

Service1代码:(接口类)

public interface IMyEvents
{
    [OperationContract(IsOneWay = true)]
    void Event1();
}

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMyEvents))]
public interface SampleInterface
{
    [OperationContract]
    void DoSomethingAndFireEvent();

    [OperationContract]
    void SubscribeEvent(); 
}

这是服务类

public class SampleHandlerSimulator : SampleInterface
{
    static Action m_Event1 = delegate { Console.WriteLine("Catch");};

    public void SubscribeEvent()
    {
        IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
        m_Event1 += subscriber.Event1;
    }

    public static void FireEvent()
    {
        m_Event1();
    }

    public void DoSomethingAndFireEvent()
    {
        CCStateHandlerSimulator.FireEvent(); 
    }

【问题讨论】:

    标签: c# .net callback


    【解决方案1】:

    通过添加对其中一个服务的引用(引用另一个服务)来配置您的服务。为此,请确保要引用的服务已启动并正在运行,然后右键单击子服务项目并选择添加服务引用。现在您可以调用父服务公开的方法了。

    您需要一个为回调配置了双工绑定的端点。有关双面绑定的信息,请参阅here。当您向父服务添加新的端点配置时更新您的服务参考。您将需要托管您的服务。

    here 提出了类似的问题。

    您的 enpoint 将如下所示:

     <endpoint address="http://localhost:61775/DUPLEXFUN/" binding="wsDualHttpBinding"
          bindingConfiguration="" name="DUPEND" contract="IDuplexCallback" />
    

    注意 wsDualHttpBinding 是需要注意的重要一点。

    您的服务合同如下所示:

        [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IDuplexCallback))]
    public interface IDuplexService
    {
    
        [OperationContract(IsOneWay = true)]
        void FormatString(string institution);
    }
    

    服务实现如下:

       [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class DuplexService : IDuplexService
    {
        IDuplexCallback _callback;
        string _requestString = String.Empty;
    
        public DuplexService()
        {
            _callback = OperationContext.Current.GetCallbackChannel<IDuplexCallback>();
        }
    
        public void FormatString(string text)
        {
            //Make the thread to sleep for 10 seconds
            Thread.Sleep(4000);
            //Format the input data
            _requestString = string.Format("Welcome to {0}", text);
            //Pass the string to the client through the call back function
            _callback.DuplexCallbackFunction(_requestString);
        }
    }
    

    最后 IDuplexCallback 声明为:

     public interface IDuplexCallback
        {
            [OperationContract(IsOneWay = true)]
            void DuplexCallbackFunction(string requestString);
        }
    

    【讨论】:

    • 感谢您的回答,但我还不能让它运行。我正在使用 netnamedpipebinding,它也是双工的。我必须在两个服务中实现 netnamedpipebinding 还是只在一个服务中实现?您的代码是如何实现第二个服务的示例吗?因为如您所见,我提供了第一个服务的代码。而且您的代码并不真正适合我的 service1 代码。
    • 我对管道不太了解,但你应该只需要在父服务中设置端点,然后将服务引用添加到子服务后,子服务就会反映其配置中的父服务。我发布的代码并不特定于您,但它确实突出了您需要的标签和东西。也许您可以发布您的父服务 app.config?
    • PubSub 可能会更好,这取决于您要完成的工作。我在双工绑定方面遇到过各种各样的问题,而且它们并不是那么有效。此处提供了 Pub-Sub 的良好示例和执行此操作的框架:idesign.net/Downloads
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 2011-03-02
    相关资源
    最近更新 更多