WCF服务调用客户端

时间:2015-03-09 16:12:21

标签: c# .net wcf service client

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

我的问题是,我希望通知我的客户因我的其他界面而发生的数据库更改。

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

我的用户客户端是简单的WinForms应用程序,并且引用了它们。

我希望避免从客户端发起呼叫并等待响应,我更喜欢该服务在客户端方向进行呼叫。

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

1 个答案:

答案 0 :(得分:1)

这可以在WCF中实现。

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

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

"本主题介绍了创建使用双工(双向)合同的方法的基本步骤。双工合同允许客户端和服务器彼此独立通信,以便任何一方可以发起对另一方的呼叫。双工合同是Windows Communication Foundation(WCF)服务可用的三种消息模式之一。另外两种消息模式是单向和请求 - 回复。双工合同由客户端和服务器之间的两个单向契约组成,并不要求方法调用是相关的。 当您的服务必须向客户查询更多信息或在客户端明确提出事件时,请使用此类合同。"

Encase他们取下页面,我不喜欢只链接答案...

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