///Client window public partial class Autorization : Window { private bool Autos; public Autorization() { InitializeComponent(); NetNamedPipeBinding binding = new NetNamedPipeBinding(); EndpointAddress address = new EndpointAddress("net.pipe://localhost/Service"); CurUser._clientCallback = new ClientCallback(); CurUser._clientCallback.RecieveAut += UpdateAutos; //InstanceContext створює оболонку для екземпляра класу ClientCallback //завдяки цьому служба може викликати обєкт через канал InstanceContext context = new InstanceContext(CurUser._clientCallback); DuplexChannelFactory<duplexdllclass.imessagecallbackservice> factory = new DuplexChannelFactory<duplexdllclass.imessagecallbackservice>(context, binding, address); CurUser.Proxy = factory.CreateChannel(); } void UpdateAutos() { Autos = CurUser._clientCallback.Autos; } private void btn_LoginClick(object sender, RoutedEventArgs e) { CurUser.Proxy.AutorizationSendToServiceMessage(new User()); UpdateAutos(); Autos.ToString(); if (Autos) MessageBox.Show("Happy"); MessageBox.Show("dfasdf"); } private void btn_RegClick(object sender, RoutedEventArgs e) { } ///Service public class Service : DuplexDllClass.IMessageCallbackService { private void ThreadAutorizationCallback(object callback) { DuplexDllClass.IMessageCallbackClient messageCallback = callback as DuplexDllClass.IMessageCallbackClient; messageCallback.AutorizationSendToClient(true); Thread.Sleep(1000); } public void AutorizationSendToServiceMessage(User user) { DuplexDllClass.IMessageCallbackClient proxy = OperationContext.Current.GetCallbackChannel<duplexdllclass.imessagecallbackclient>(); //посилаємо повідомлення клієнту // proxy.MessageToClient("Message from server"); //create new thread //запускаємо потік та передаємо йому об’єкт проксі класу new Thread(ThreadAutorizationCallback).Start(proxy); } } //Service window internal static ServiceHost serviceHost; internal static void StartService() { serviceHost = new ServiceHost(typeof(Service), new Uri("net.pipe://localhost/Service")); serviceHost.AddServiceEndpoint(typeof(DuplexDllClass.IMessageCallbackService), new NetNamedPipeBinding(), ""); serviceHost.Open(); } internal static void StopService() { if (serviceHost.State != CommunicationState.Closed) serviceHost.Close(); } static void Main(string[] args) { Console.WriteLine("Service runnig"); Service service= new Service(); StartService(); Console.ReadLine(); StopService(); } } //Client class ClientCallback : DuplexDllClass.IMessageCallbackClient { public Action RecieveAut; public bool Autos; public void AutorizationSendToClient(bool rezalt) { Autos = rezalt; } }</duplexdllclass.imessagecallbackclient></duplexdllclass.imessagecallbackservice></duplexdllclass.imessagecallbackservice>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)