internal void StartHosting(string port) { try { //var ips = Dns.GetHostEntry(Dns.GetHostName()); //var ipAddress = ips.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork); var ipAddress = PNStatic.GetLocalIPv4(NetworkInterfaceType.Wireless80211) ?? PNStatic.GetLocalIPv4(NetworkInterfaceType.Ethernet); // Create the url that is needed to specify where the service should be started if (ipAddress != null) { _UrlService = "net.tcp://" + ipAddress + ":" + port + "/PNService"; } var service = new PNService(); _Host = new ServiceHost(service); //// For debug purpose only //_Host.Opening += host_Opening; //_Host.Opened += host_Opened; //_Host.Closing += host_Closing; //_Host.Closed += host_Closed; // The binding is where we can choose what transport layer we want to use. HTTP, TCP ect. var tcpBinding = new NetTcpBinding { TransactionFlow = false }; tcpBinding.Security.Transport.ProtectionLevel = ProtectionLevel.EncryptAndSign; tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows; tcpBinding.Security.Mode = SecurityMode.None; tcpBinding.ReceiveTimeout = TimeSpan.FromSeconds(600); tcpBinding.MaxReceivedMessageSize = 80000000; // Add a endpoint _Host.AddServiceEndpoint(typeof(IPNService), tcpBinding, _UrlService); //// http hosting requires administrator rights //// A channel to describe the service. Used with the proxy scvutil.exe tool //ServiceMetadataBehavior metadataBehavior; //metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); //if (metadataBehavior == null) //{ // // This is how I create the proxy object that is generated via the svcutil.exe tool // metadataBehavior = new ServiceMetadataBehavior(); // metadataBehavior.HttpGetUrl = new Uri("http://" + _ipAddress.ToString() + ":8001/PNService"); // metadataBehavior.HttpGetEnabled = true; // //metadataBehavior.ToString(); // host.Description.Behaviors.Add(metadataBehavior); // urlMeta = metadataBehavior.HttpGetUrl.ToString(); //} _Host.Open(); var pnService = _Host.SingletonInstance as PNService; if (pnService != null) { pnService.PNDataReceived += service_PNDataReceived; } } catch (Exception ex) { if (PNDataError != null) { PNDataError(this, new PNDataErrorEventArgs(ex)); } StopHosting(); } }