/// <summary> /// Open the service endpoint(s) and wait for connection from client /// For this method to operate properly the following must be setup: /// 1. .Net port sharing must be set up by issueing the following /// command under an admin account: sc.exe config NetTcpPortSharing start= demand /// 2. Must run the app in admin mode or the current user must be added /// to the allowedAccounts section in the SMSvcHost.exe.config file. /// </summary> private static void OpenServiceHost() { int processId = Process.GetCurrentProcess().Id; NativeSessionHost nativeSessionHost = // Create the native session host obj for the IPC and NetTcp service hosts to use new NativeSessionHost(); ServiceHost serviceHost = // Create the host object new ServiceHost(nativeSessionHost); // Add the native session's IPC endpoint (for use by the NativeSessionManager) string endpointAddressName = Nomenclator.GetIPCEndpointAddress(typeof(Types.Internal.INativeSession), processId); NetNamedPipeBinding ipcBinding = new NetNamedPipeBinding("ipcBinding"); ServiceEndpoint ipcEndpoint = serviceHost.AddServiceEndpoint( typeof(Types.Internal.INativeSession), ipcBinding, new Uri(endpointAddressName)); // Add the NetTcp endpoint (for use by the native client) NativeSessionEndpointAddress.ServiceHostName = ServiceHostUtil.HostName; NativeSessionEndpointAddress.ServicePort = ServiceHostUtil.BasePort + 5; // //port offset for native services = +2 endpointAddressName = NativeSessionEndpointAddress.Build(processId); NetTcpBinding tcpBinding = new NetTcpBinding("tcpBinding"); ServiceEndpoint tcpEndpoint = serviceHost.AddServiceEndpoint( typeof(Native.INativeSession), tcpBinding, new Uri(endpointAddressName)); // Up the value of maxItemsInObjectGraph so that the MetaTree can pass over the wire... foreach (OperationDescription op in tcpEndpoint.Contract.Operations) { if (op.Name != "InvokeNativeMethod") { continue; } DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find <DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior; if (dataContractBehavior != null) { dataContractBehavior.MaxItemsInObjectGraph = 2147483647; } } // We really don't need to do this anymore. Why rewrite the address? Just keep the address from the config file. //ServiceHostUtil.ApplyEndpointAddressAndIdentityConfig(serviceHost); serviceHost.Open(); // Open the host }
static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form f = new Form(); // need to create a form so certain UI operations work properly (e.g. ISIS, LSW) NativeSessionHost.StartUp(args); // fire up the session Application.Run(); // Process UI messages until app exits return; }