I am writing a service. Which task: notifying customers about the update. Here is a contract:

[ServiceContract(CallbackContract = typeof(IClientCallBackContract))] public interface IBHDashBoardContract { [OperationContract(IsOneWay = true)] void RegisterClient(); [OperationContract(IsOneWay = true)] void UnRegisterClient(); } 

The contract that implements the client:

 public interface IClientCallBackContract { [OperationContract(IsOneWay = true)] void UpdateAqua(DataSet AquaDS); [OperationContract(IsOneWay = true)] void UpdateCalls(DataSet CallsDS); [OperationContract(IsOneWay = true)] void UpdateCooler(DataSet CoolerDS); [OperationContract(IsOneWay = true)] void UpdateDriver(DataSet DriverDS); } 

Implementation:

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)] public class BHDashBoardService : IBHDashBoardContract { static List<Client> _clients = new List<Client>(); public object Dataset { get; private set; } public void RegisterClient() { var client = new Client() { ClientId = Guid.NewGuid(), Callback = OperationContext.Current.GetCallbackChannel<IClientCallBackContract>() }; _clients.Add(client); Console.WriteLine("Registered :" + client.ClientId); } public void UnRegisterClient() { var _callback = OperationContext.Current.GetCallbackChannel<IClientCallBackContract>(); Console.WriteLine("UnRegistered :" + _clients.First(c => c.Callback == _callback)); _clients.RemoveAll(c => c.Callback == _callback); } public BHDashBoardService() { int msToUpdate = 10000; int msToStart = 1000; Timer timerAqua = new Timer(UpdateAquaMenu, null, msToStart, msToUpdate); Timer timerCalls = new Timer(UpdateCalls, null, msToStart, msToUpdate); Timer timerCoolers = new Timer(UpdateCoolerMenu, null, msToStart, msToUpdate); } ~BHDashBoardService() { } void UpdateAquaMenu(object obj) { Console.WriteLine("UpdateAqua: " + DateTime.Now); //creating and fill dataset; DataSet _aquaDS = null; foreach (var client in _clients) client.Callback.UpdateAqua(_aquaDS); // send to client new dataset } void UpdateCalls(object obj) { Console.WriteLine("UpdateCalls: " + DateTime.Now); //creating and fill dataset; DataSet _aquaDS = null; foreach (var client in _clients) client.Callback.UpdateCalls(_aquaDS); // send to client new dataset } void UpdateCoolerMenu(object obj) { Console.WriteLine("UpdateCoolers: " + DateTime.Now); //creating and fill dataset; DataSet _aquaDS = null; foreach (var client in _clients) client.Callback.UpdateCooler(_aquaDS); // send to client new dataset } } 

Customer:

 public class Client { public Guid ClientId { get; set; } public IClientCallBackContract Callback { get; set; } } 

Host itself:

 Uri address = new Uri("http://localhost:4000/IBHDashBoardContract"); WSDualHttpBinding binding = new WSDualHttpBinding(); ServiceHost host = new ServiceHost(typeof(BHDashBoardService)); Console.WriteLine("Host created"); Console.ReadKey(); host.AddServiceEndpoint(typeof(IBHDashBoardContract), binding, address); host.Open(); Console.WriteLine("Host Opened"); Console.ReadKey(); host.Close(); Console.WriteLine("Host Closed"); 

Why, before opening the host, the update methods are executed every 10 seconds, and after the opening, they cease to be executed.

Host Startup Example

I will not be able to connect the client to a non-open host. And it is necessary that the methods work and the host is turned on. Help!

  • And what's the problem with this ??? I connected users. Displays the registration message. Console.WriteLine ("Registered:" + client.ClientId); Yes, and before the discovery is also worth ReadKey () and everything works - Be Happy
  • one
    Oh, everything is easier. Your timers collected garbage collector. - Pavel Mayorov
  • Right now I will try to declare timers in the class itself. Then should not collect. - Be Happy
  • It worked. I'm a fool. I forgot about the Garbage Collector .... He immediately deleted everything. Minus c # ... I thought it would not immediately delete when the object reference disappeared, and when it does not have enough memory, then it just does Collect ... I can put a plus sign. Write in response. - Be Happy

0