protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { List <Uri> endpoints = new List <Uri>(); //foreach (Uri baseAddress in baseAddresses) //{ // var scheme = "net.tcp"; // var host = baseAddress.Host; // var port = baseAddress.Port; // var pathAndQuery = baseAddress.PathAndQuery; // if (pathAndQuery.StartsWith("/")) // { // pathAndQuery = pathAndQuery.Substring(1); // } // endpoints.Add(new Uri($"{scheme}://{host}:{port}/{pathAndQuery}")); //} var interfaceType = serviceType.GetInterfaces().Except(new[] { typeof(IServiceBase), typeof(IDisposable) }).FirstOrDefault(); var baseAddress = baseAddresses[0]; var scheme = "net.tcp"; var host = baseAddress.Host; var port = baseAddress.Port; var pathAndQuery = baseAddress.PathAndQuery; if (pathAndQuery.StartsWith("/")) { pathAndQuery = pathAndQuery.Substring(1); } endpoints.Add(new Uri($"{scheme}://{host}:{port}/{pathAndQuery}")); endpoints.AddRange(baseAddresses); var serviceHost = new ServiceHost(serviceType, endpoints.ToArray()); // endpoint http serviceHost.AddServiceEndpoint(interfaceType, new BasicHttpBinding(), ""); // endpoint net.tcp var netTcpBinding = new NetTcpBinding(SecurityMode.None) { PortSharingEnabled = true, MaxReceivedMessageSize = 1024 * 1024 * 10 }; serviceHost.AddServiceEndpoint(interfaceType, netTcpBinding, ""); // endpoint mex ServiceMetadataBehavior mexBehavior = serviceHost.Description.Behaviors.Find <ServiceMetadataBehavior>(); if (mexBehavior != null) { mexBehavior.HttpGetEnabled = false; mexBehavior.HttpsGetEnabled = false; } serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); return(serviceHost); }
public static bool StartServer() { bool result = false; string ipBase = Helper.GetMyIP(); Uri tcpAdrs = new Uri("net.tcp://" + ipBase + ":8000/ShareP/"); Uri httpAdrs = new Uri("http://" + ipBase + ":8001/ShareP/"); Uri[] baseAdresses = { tcpAdrs, httpAdrs }; SharePService sharePService = new SharePService(); SelfHost = new ServiceHost( sharePService, baseAdresses); NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None, true); tcpBinding.MaxBufferPoolSize = (int)67108864; tcpBinding.MaxBufferSize = 67108864; tcpBinding.MaxReceivedMessageSize = (int)67108864; tcpBinding.TransferMode = TransferMode.Buffered; tcpBinding.ReaderQuotas.MaxArrayLength = 67108864; tcpBinding.ReaderQuotas.MaxBytesPerRead = 67108864; tcpBinding.ReaderQuotas.MaxStringContentLength = 67108864; tcpBinding.MaxConnections = 100; // Maybe change? ServiceThrottlingBehavior throttle; // Performance throttle = SelfHost.Description.Behaviors.Find <ServiceThrottlingBehavior>(); if (throttle == null) { throttle = new ServiceThrottlingBehavior(); throttle.MaxConcurrentCalls = 100; //Here as well throttle.MaxConcurrentSessions = 100; //-- SelfHost.Description.Behaviors.Add(throttle); } tcpBinding.ReceiveTimeout = new TimeSpan(24, 0, 0); tcpBinding.ReliableSession.Enabled = true; tcpBinding.ReliableSession.InactivityTimeout = new TimeSpan(0, 0, 3); // Disconnect after 3 seconds of inactivity SelfHost.AddServiceEndpoint(typeof(IShareP), tcpBinding, "tcp"); ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior(); SelfHost.Description.Behaviors.Add(metadataBehavior); SelfHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "net.tcp://" + ipBase + ":8002/ShareP/mex"); try { SelfHost.Open(); } catch (Exception e) { result = false; Log.LogException(e); } finally { if (SelfHost.State == CommunicationState.Opened) { result = true; Log.LogInfo("Server opened on " + ipBase); } } return(result); }
private ServiceHost StartNetTcpAndHttpService(ModelManager modelManager, Boolean onlyNetTcp) { Uri[] baseAddresses; if (onlyNetTcp) { baseAddresses = new Uri[] { new Uri($"net.tcp://localhost:{FiskmoMTEngineSettings.Default.MtServicePort}/") }; } else { baseAddresses = new Uri[] { new Uri($"net.tcp://localhost:{FiskmoMTEngineSettings.Default.MtServicePort}/"), new Uri($"http://localhost:8500/") }; }; var mtService = new MTService(modelManager); Log.Information($"Creating service host with following URIs: {String.Join(",",baseAddresses.Select(x => x.ToString()))}"); var selfHost = new ServiceHost(mtService, baseAddresses); // Check to see if the service host already has a ServiceMetadataBehavior ServiceMetadataBehavior smb = selfHost.Description.Behaviors.Find <ServiceMetadataBehavior>(); // If not, add one if (smb == null) { smb = new ServiceMetadataBehavior(); } smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; selfHost.Description.Behaviors.Add(smb); // Add MEX endpoint selfHost.AddServiceEndpoint( ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexTcpBinding(), "mex" ); var nettcpBinding = new NetTcpBinding(); //Use default net.tcp security, which is based on Windows authentication: //using the service is only possible from other computers in the same domain. //TODO: add a checkbox (with warning) in the UI for using security mode None, //to allow connections from IP range (also add same checkbox to clients). //nettcpBinding.Security.Mode = SecurityMode.None; /*nettcpBinding.Security.Mode = SecurityMode.Transport; * nettcpBinding.Security.Transport.ClientCredentialType = * TcpClientCredentialType.Windows;*/ //Customization tuning sets tend to be big nettcpBinding.MaxReceivedMessageSize = 20000000; selfHost.AddServiceEndpoint(typeof(IMTService), nettcpBinding, "MTService"); if (!onlyNetTcp) { selfHost.AddServiceEndpoint(typeof(IMTService), new WebHttpBinding(), "MTRestService"); WebHttpBehavior helpBehavior = new WebHttpBehavior(); helpBehavior.HelpEnabled = true; selfHost.Description.Endpoints[2].Behaviors.Add(helpBehavior); } Log.Information($"Opening the service host"); selfHost.Open(); return(selfHost); }