/// <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 }
/// <summary> /// Main method for native session host /// </summary> /// <param name="args"></param> public static void StartUp(string[] args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); bool debug = (args.Length == 0); InitializeServicesLogFileName(); try { // Debug mode if (debug) { LogServiceCalls = true; NativeDebugMode = true; ServicesLog.LogToConsole = true; Initialize(); OpenServiceHost(); // Start the service host int processId = Process.GetCurrentProcess().Id; string endpointAddress = NativeSessionEndpointAddress.Build(processId); ServicesLog.Message("NativeSessionHost started in debug mode at endpoint address: " + endpointAddress); StartIdleMonitor(); // debug idle monitor } // Started by HostingApp else { ServiceHostProcessId = int.Parse(args[0]); string creatorClassTypeName = args[1]; int sessionId = int.Parse(args[2]); AppSettingsReader asr = new AppSettingsReader(); string enableLoggingStr = asr.GetValue("EnableNativeSessionLogging", typeof(string)) as string; bool.TryParse(enableLoggingStr, out LogServiceCalls); Initialize(); OpenServiceHost(); // Start the service host listening for requests RegisterSession(ServiceHostProcessId, sessionId); // register session with the service host that started us string msg = "MobiusNativeSession started for session " + sessionId + " in process " + Process.GetCurrentProcess().Id; Version version = Assembly.GetExecutingAssembly().GetName().Version; string tok = VersionMx.FormatVersion(version); msg += ", Services: " + tok; ServicesLog.Message(msg); // log initial message StartIdleMonitor(); // start thread that will exit if idle too long } return; } catch (Exception ex) { string msg = DebugLog.FormatExceptionMessage(ex); if (debug) { MessageBox.Show(msg); } ServicesLog.Message(msg); return; } }