[英]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.