[英]Change in one WCF service should update the same change in other instances of the same WCF services
我托管了同一WCF服務的多個實例。 如果服務實例之一發生更改,則也應在其他服務中通知/更新它。 我實現了該解決方案,以使WCF服務在DuplexChannelFactory中既充當客戶端又充當服務器。 但是為了做到這一點,服務需要注冊其對等體,而我正在服務的構造函數中進行此操作。 這將導致死鎖,因為它永遠不會互相初始化。 在WCF服務中如何實現這種邏輯?
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Timers;
using WcfServiceLibrary.PeerServiceReference;
namespace WcfServiceLibrary
{
public interface IHeartbeat
{
bool Pulse();
void Ping();
}
[ServiceContract(CallbackContract = typeof(INotifications))]
public interface ISubscription : IHeartbeat, INotifications
{
[OperationContract(IsOneWay = true)]
void Subscribe(string ServiceName);
[OperationContract(IsOneWay = true)]
void Unsubscribe(string ServiceName);
}
[ServiceContract]
public interface INotifications
{
[OperationContract(IsOneWay = true)]
void UpdateData(int newValue);
}
[ServiceContract]
public interface IUserService
{
[OperationContract(IsOneWay = true)]
void MethodThatWillChangeData(int value);
}
public class ObservableService : IUserService, ISubscription
{
private static Dictionary<string, ISubscription> _Peers = new Dictionary<string, ISubscription>();
private int ClientAge;
private string ServiceName { get; }
private Timer _timer = new Timer() { Interval = 5000 };
private IWriter _writer;
public ObservableService() : this(new ConsoleWriter())
{
}
public ObservableService(IWriter writer)
{
_writer = writer;
_writer.WriteLine("Initiating construction...");
ServiceName = ConfigurationManager.AppSettings["ServiceName"];
_timer.Elapsed += Timer_Elapsed;
_timer.Start();
var PeerServersList = ConfigurationManager.AppSettings["PeerServers"].Split(';');
var callback = new InstanceContext(this);
foreach (var peer in PeerServersList)
{
try
{
var dualBinding = new WSDualHttpBinding();
var address = new EndpointAddress(peer);
var PeerServiceFactory = new DuplexChannelFactory<ISubscription>(callback, dualBinding);
var PeerService = PeerServiceFactory.CreateChannel(address);
PeerService.Subscribe(ServiceName);
}
catch (Exception ex)
{
//TODO handle the exception
}
}
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
_writer.WriteLine("Pinging the data");
this.Ping();
}
public void MethodThatWillChangeData(int newValue)
{
_writer.WriteLine($"MethodThatWillChangeData with {newValue}");
var PreviousClientAge = ClientAge;
ClientAge = newValue;
foreach (var peer in _Peers)
{
_writer.WriteLine($"Calling update on the client {peer.Key}");
peer.Value.UpdateData(newValue);
}
}
public void Ping()
{
var test = _Peers.Keys.ToList();
for (int i = _Peers.Count - 1; i >= 0; i--)
{
try
{
_writer.WriteLine($"Checking the pulse of {test[i]}");
_Peers[test[i]].Pulse();
}
catch (Exception)
{
_Peers.Remove(test[i]);
}
}
}
public bool Pulse()
{
_writer.WriteLine($"Pulse requested...");
return true;
}
public void UpdateData(int newValue)
{
_writer.WriteLine($"Updating the data to {newValue} from {ClientAge}");
ClientAge = newValue;
}
public void Unsubscribe(string ServiceName)
{
if (_Peers.Keys.Contains(ServiceName))
{
_Peers.Remove(ServiceName);
}
}
public void Subscribe(string ServiceName)
{
if (!_Peers.Keys.Contains(ServiceName))
{
_writer.WriteLine($"Registering {ServiceName}...");
_Peers.Add(ServiceName, OperationContext.Current.GetCallbackChannel<ISubscription>());
}
}
}
}
我將有一個單獨的WCF服務,該服務可以管理您要在服務之間保持同步的任何數據,並使每個服務都與之通信。
另外,您也可以將數據保存在數據庫中,以便所有更改都會自動反映在服務中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.