【问题标题】:WCF service calls to clients对客户端的 WCF 服务调用
【发布时间】:2015-03-09 16:12:20
【问题描述】:

我正在尝试创建 WCF 服务来处理客户端请求和修改数据库。除了我的用户客户端,我还有其他接口,可以调用我的 WCF 服务并修改数据库。

我的问题是,我希望通知我的客户由于我的其他接口而发生的数据库更改。

所以基本上:客户端正在运行,由 WAS/IIS 托管的 WCF 服务,数据库更改和服务应该通知我所有连接的客户端有关更改。

我的用户客户端是简单的 WinForms 应用程序,服务被它们引用。

我想避免从客户端发起呼叫并等待响应,我宁愿服务在客户端方向进行呼叫。

我的问题是有没有办法实现这个,或者我应该使用像监听器这样的东西?

【问题讨论】:

    标签: c# .net wcf service client


    【解决方案1】:

    这可以在 WCF 中实现。

    实施所谓的“双工通信模式”,基本上是两个单向合同。

    Here is an MSDN article for just that... How to: Create a Duplex Contract

    “本主题展示了创建使用双工(双向)合约的方法的基本步骤。双工合约允许客户端和服务器相互独立通信,以便任何一方都可以发起对另一方的调用。双工合约是 Windows Communication Foundation (WCF) 服务可用的三种消息模式之一。另外两种消息模式是单向和请求-答复。双工合同由客户端和服务器之间的两个单向合同组成,不需要方法调用是相关的。当您的服务必须向客户端查询更多信息或显式在客户端上引发事件时,请使用这种合同。"

    将他们撤下的页面包裹起来,我真的不喜欢只有链接的答案...

    // Define a duplex service contract.
    // A duplex contract consists of two interfaces.
    // The primary interface is used to send messages from client to service.
    // The callback interface is used to send messages from service back to client.
    // ICalculatorDuplex allows one to perform multiple operations on a running result.
        // The result is sent back after each operation on the ICalculatorCallback interface.
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode=SessionMode.Required,
                 CallbackContract=typeof(ICalculatorDuplexCallback))]
    public interface ICalculatorDuplex
    {
        [OperationContract(IsOneWay=true)]
        void Clear();
        [OperationContract(IsOneWay = true)]
        void AddTo(double n);
        [OperationContract(IsOneWay = true)]
        void SubtractFrom(double n);
        [OperationContract(IsOneWay = true)]
        void MultiplyBy(double n);
        [OperationContract(IsOneWay = true)]
        void DivideBy(double n);
    }
    
    // The callback interface is used to send messages from service back to client.
    // The Equals operation will return the current result after each operation.
    // The Equation opertion will return the complete equation after Clear() is called.
    public interface ICalculatorDuplexCallback
    {
        [OperationContract(IsOneWay = true)]
        void Equals(double result);
        [OperationContract(IsOneWay = true)]
        void Equation(string eqn);
    }
    
    // Service class which implements a duplex service contract.
    // Use an InstanceContextMode of PerSession to store the result
    // An instance of the service will be bound to each duplex session
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class CalculatorService : ICalculatorDuplex
    {
        double result;
        string equation;
        ICalculatorDuplexCallback callback = null;
    
        public CalculatorService()
        {
            result = 0.0D;
            equation = result.ToString();
            callback = OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>();
        }
    
        public void Clear()
        {
            callback.Equation(equation + " = " + result.ToString());
            result = 0.0D;
            equation = result.ToString();
        }
    
        public void AddTo(double n)
        {
            result += n;
            equation += " + " + n.ToString();
            callback.Equals(result);
        }
    
        public void SubtractFrom(double n)
        {
            result -= n;
            equation += " - " + n.ToString();
            callback.Equals(result);
        }
    
        public void MultiplyBy(double n)
        {
            result *= n;
            equation += " * " + n.ToString();
            callback.Equals(result);
        }
    
        public void DivideBy(double n)
        {
            result /= n;
            equation += " / " + n.ToString();
            callback.Equals(result);
        }
    }
    

    【讨论】:

    • 谢谢,我已经找到并实现了这个模式。我的问题是,如果我的服务器崩溃了怎么办。如果我的理解是正确的,那么 IIS 只有在客户端收到请求时才能动态托管服务。所以基本上我的问题是,有没有办法在不托管所有时间运行的服务的情况下进行双工模式?
    • @KrisztiánKis 看看 MSMQ 是否是您想要的,它用于为不总是连接的系统构建可靠的消息传递系统...msdn.microsoft.com/en-us/library/ms978430.aspx
    • @KrisztiánKis 阅读“使用 WCF 和 MSMQ 排队的初学者指南”以帮助您入门codeproject.com/Articles/520323/…
    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2023-03-27
    相关资源
    最近更新 更多