public static string GetEndpointAddress(this BindingScheme scheme, string processID, ConnectionOptions option, bool isMex = false) { String transport = String.Empty; var serverHostName = option.HostName; var port = scheme == BindingScheme.HTTP ? option.HTTPPort : option.TCPPort; switch (scheme) { case BindingScheme.NAMED_PIPE: transport = "net.pipe"; break; case BindingScheme.TCP: transport = "net.tcp"; serverHostName = $"{option.HostName}:{port}"; break; case BindingScheme.HTTP: transport = "http"; serverHostName = $"{option.HostName}:{port}"; break; } if (isMex) return $"{transport}://{serverHostName}/Design_Time_Addresses/Codex/{processID}/mex"; else return $"{transport}://{serverHostName}/Design_Time_Addresses/Codex/{processID}/IPCService"; }
public static Binding GetBinding(this BindingScheme scheme, ConnectionOptions options) { Binding binding = null; switch (scheme) { case BindingScheme.TCP: { binding = new NetTcpBinding(SecurityMode.None); var tcpBinding = ((NetTcpBinding)binding); tcpBinding.MaxBufferPoolSize = Constants.MAX_MSG_SIZE; tcpBinding.MaxReceivedMessageSize = Constants.MAX_MSG_SIZE; break; } case BindingScheme.NAMED_PIPE: { binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None); var npBinding = ((NetNamedPipeBinding)binding); npBinding.MaxBufferPoolSize = Constants.MAX_MSG_SIZE; npBinding.MaxReceivedMessageSize = Constants.MAX_MSG_SIZE; break; } case BindingScheme.HTTP: { binding = new NetHttpBinding(BasicHttpSecurityMode.None); var httpBinding = ((NetHttpBinding)binding); httpBinding.MaxBufferPoolSize = Constants.MAX_MSG_SIZE; httpBinding.MaxReceivedMessageSize = Constants.MAX_MSG_SIZE; httpBinding.WebSocketSettings.TransportUsage = WebSocketTransportUsage.Always; httpBinding.MessageEncoding = NetHttpMessageEncoding.Text; break; } } if (binding != null) { binding.CloseTimeout = options.CloseTimeout; binding.OpenTimeout = options.OpenTimeout; binding.ReceiveTimeout = options.ReceiveTimeout; binding.SendTimeout = options.SendTimeout; } return binding; }
/// <summary> /// Start the IPC server for this process. /// </summary> /// <param name="resetEvent">Reset event to gracefully shutdown the server.</param> /// <param name="processID">Base address for the process.</param> /// <param name="options">Connections options for the server.</param> /// <param name="scheme">Connection schemes that can be used in the server, this is a flags enum so multiple schemes can be provided.</param> public void Start(ManualResetEvent resetEvent, string processID, ConnectionOptions options, BindingScheme scheme) { List<Uri> baseAddresses = new List<Uri>(); if (scheme.IsBindingScheme(BindingScheme.NAMED_PIPE)) { baseAddresses.Add(new Uri(BindingScheme.NAMED_PIPE.GetEndpointAddress(processID, options, false))); } if (scheme.IsBindingScheme(BindingScheme.TCP)) { baseAddresses.Add(new Uri(BindingScheme.TCP.GetEndpointAddress(processID, options, false))); } if (scheme.IsBindingScheme(BindingScheme.HTTP)) { baseAddresses.Add(new Uri(BindingScheme.HTTP.GetEndpointAddress(processID, options, false))); } using (var host = new ServiceHost(IPCService.Instance, baseAddresses.ToArray())) { // Check to see if the service host already has a ServiceMetadataBehavior var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); // If not, add one if (smb == null) { smb = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(smb); } // Check to see if the service host already has a ServiceDebugBehavior var sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>(); // If not, add one if (sdb == null) { sdb = new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true }; host.Description.Behaviors.Add(sdb); } // Setup the bindings if (scheme.IsBindingScheme(BindingScheme.TCP)) { var tcpBinding = (NetTcpBinding)BindingScheme.TCP.GetBinding(options); host.AddServiceEndpoint(typeof(IIPC), tcpBinding, ""); host.AddServiceEndpoint(typeof(IIPCDuplex), tcpBinding, ""); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), BindingScheme.TCP.GetEndpointAddress(processID, options, true)); } if (scheme.IsBindingScheme(BindingScheme.NAMED_PIPE)) { var namedPipeBinding = (NetNamedPipeBinding)BindingScheme.NAMED_PIPE.GetBinding(options); host.AddServiceEndpoint(typeof(IIPC), namedPipeBinding, ""); host.AddServiceEndpoint(typeof(IIPCDuplex), namedPipeBinding, ""); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), BindingScheme.NAMED_PIPE.GetEndpointAddress(processID, options, true)); } if (scheme.IsBindingScheme(BindingScheme.HTTP)) { var httpBinding = (NetHttpBinding)BindingScheme.HTTP.GetBinding(options); host.AddServiceEndpoint(typeof(IIPC), httpBinding, ""); host.AddServiceEndpoint(typeof(IIPCDuplex), httpBinding, ""); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), BindingScheme.HTTP.GetEndpointAddress(processID, options, true)); } host.Open(); Trace.WriteLine("Service up and running at:"); foreach (var ea in host.Description.Endpoints) { Trace.WriteLine(ea.Address); } resetEvent.WaitOne(); host.Close(); } }
/// <summary> /// Gets a duplex client /// </summary> /// <param name="processID">Process identifier for the server on the host machine.</param> /// <param name="options">Connection Options</param> /// <returns>Client object.</returns> public static IPCDuplexClient GetDuplexClient(InstanceContext context, string processID, ConnectionOptions options, BindingScheme scheme = BindingScheme.NAMED_PIPE) { return new IPCDuplexClient(context, scheme.GetBinding(options), new EndpointAddress(scheme.GetEndpointAddress(processID, options, false))); }
/// <summary> /// Gets a basic request response client /// </summary> /// <param name="processID">Process identifier for the server on the host machine.</param> /// <param name="options">Connection Options</param> /// <returns>Client object.</returns> public static IPCClient GetClient(string processID, ConnectionOptions options, BindingScheme scheme = BindingScheme.NAMED_PIPE) { return new IPCClient(scheme.GetBinding(options), new EndpointAddress(scheme.GetEndpointAddress(processID, options, false))); }