[英]WCF service calls to clients
我正在尝试创建WCF服务来处理客户端请求和修改数据库。 除了我的用户客户端,我还有其他接口,可以调用WCF服务并修改数据库。
我的问题是,由于我的其他界面,我希望通知我的客户有关数据库更改的信息。
基本上就是这样:客户端正在运行,WAS / IIS托管的WCF服务,数据库更改和服务应将更改通知我所有连接的客户端。
我的用户客户端是简单的WinForms应用程序,并且已引用了该服务。
我想避免从客户端发起呼叫并等待响应,我希望该服务在客户端方向进行呼叫。
我的问题是,有没有一种方法可以实现此目的,或者我应该使用类似侦听器的方法?
这可以在WCF中实现。
实施所谓的“双工通信模式”,基本上是两个单向合同。
“本主题显示了创建使用双工(双向)协定的方法的基本步骤。双工协定允许客户端和服务器相互独立通信,以便双方可以发起对对方的呼叫。双工协定是其中之一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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.