public void Connect()
        {
            //Discover WCF service via broadcasting
            _broadcastSearch = new Thread(() => {
                DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
                Collection <EndpointDiscoveryMetadata> helloWorldServices = discoveryClient.Find(new FindCriteria(typeof(IControllerService))).Endpoints;
                discoveryClient.Close();

                if (helloWorldServices.Count == 0)
                {
                    OnConnectionFailed();
                    Trace.TraceWarning("Client not found");
                }
                else
                {
                    Trace.TraceInformation("Connecting to client");
                    EndpointAddress serviceAddress  = helloWorldServices[0].Address;
                    NetTcpBinding binding           = new NetTcpBinding(SecurityMode.None);
                    binding.ReliableSession.Enabled = true;
                    binding.ReliableSession.Ordered = false;

                    IControllerServiceCallback evnt = new ControllerCallback(this);
                    InstanceContext evntCntx        = new InstanceContext(evnt);

                    proxy = new EventServiceController(evntCntx, binding, serviceAddress);

                    proxy.ChannelFactory.Faulted += new EventHandler(ChannelFactory_Faulted);
                    proxy.Connect();
                }
            });

            _broadcastSearch.Start();
        }
 public static T OnComplete <T>(this T t, ControllerCallback action) where T : BaseController
 {
     if (t != null)
     {
         t.onComplete = action;
     }
     return(t);
 }
Esempio n. 3
0
        public void Run()
        {
            try
            {
                //     AppSettingsProperty hui = new AppSettingsProperty("Threshold", "", typeof(float).);

                Console.WriteLine("Searching for service...");

                DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());

                Collection <EndpointDiscoveryMetadata> helloWorldServices = discoveryClient.Find(new FindCriteria(typeof(IControllerService))).Endpoints;

                discoveryClient.Close();

                if (helloWorldServices.Count == 0)
                {
                    Console.WriteLine("No services. Try again? (y/n)");
                    var input = Console.ReadKey().KeyChar.ToString();
                    if (YES_VARIANTS.Any(x => x == input))
                    {
                        new Program().Run();
                    }
                }
                else
                {
                    Console.WriteLine("Something finded, connecting...");
                    EndpointAddress serviceAddress = helloWorldServices[0].Address;

                    IControllerServiceCallback evnt     = new ControllerCallback();
                    InstanceContext            evntCntx = new InstanceContext(evnt);

                    NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
                    binding.ReliableSession.Enabled = true;
                    binding.ReliableSession.Ordered = false;

                    proxy = new EventServiceController(evntCntx, binding, serviceAddress);

                    proxy.Connect();

                    AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);

                    //  Console.WriteLine(output);

                    string tmp = "";
                    Console.Write(Environment.NewLine + "Enter Alarm Threshold: ");

                    AppSettingsProperty setting = new AppSettingsProperty("Threshold", "", typeof(float).FullName);
                    while (tmp.ToLower() != "exit")
                    {
                        tmp           = Console.ReadLine();
                        setting.value = tmp;
                        proxy.SendSettings(setting);
                    }

                    Console.WriteLine(proxy.DisconnectPrepare());
                    //try
                    //{
                    //    if (proxy.State != System.ServiceModel.CommunicationState.Faulted)
                    //    {
                    //        proxy.Close();
                    //    }
                    //}
                    //catch (Exception ex)
                    //{
                    //    Console.WriteLine(ex.Message);
                    //    proxy.Abort();
                    //}
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }