Author: drewbai Date: Tue Apr 28 05:40:07 2009 New Revision: 769249 URL: http://svn.apache.org/viewvc?rev=769249&view=rev Log: STONEHENGE-39 Allow restart on .NET WCF Service Hosts Applied patch for Ben Thanks!
Modified: incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConfigurationSettings/Settings.cs incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConsole/Program.cs Modified: incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConfigurationSettings/Settings.cs URL: http://svn.apache.org/viewvc/incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConfigurationSettings/Settings.cs?rev=769249&r1=769248&r2=769249&view=diff ============================================================================== --- incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConfigurationSettings/Settings.cs (original) +++ incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConfigurationSettings/Settings.cs Tue Apr 28 05:40:07 2009 @@ -39,6 +39,7 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.Configuration; using System.Web; using System.ServiceModel; @@ -111,6 +112,11 @@ public Settings() { + if (Settings.ORDER_PROCESSING_MODE != null) + { + ConfigurationManager.RefreshSection("appSettings"); + } + Settings.ORDER_PROCESSING_MODE = ConfigurationManager.AppSettings.Get("ORDER_PROCESSING_MODE"); Settings.EVENT_LOG = ConfigurationManager.AppSettings.Get("EVENT_LOG"); Settings.DBServer = ConfigurationManager.AppSettings.Get("DBServer"); Modified: incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConsole/Program.cs URL: http://svn.apache.org/viewvc/incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConsole/Program.cs?rev=769249&r1=769248&r2=769249&view=diff ============================================================================== --- incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConsole/Program.cs (original) +++ incubator/stonehenge/trunk/stocktrader/dotnet/business_service/BusinessServiceConsole/Program.cs Tue Apr 28 05:40:07 2009 @@ -57,19 +57,21 @@ namespace Trade.BusinessServiceConsole { - + class BusinessService_ConsoleHost { /// <summary> /// The program entry class. Note how this simply inherits from the provided base class. /// </summary> - class MyHost + public class BusinessServiceConsoleHost : IDisposable { + private ServiceHost serviceHost; + /// <summary> /// This is the key call where you will define parameters for the Master host startup, and call /// the base 'startService' method. /// </summary> - public void startUp() + public void start() { string shortHostName = System.Net.Dns.GetHostName(); IPHostEntry myEntry = System.Net.Dns.GetHostEntry(shortHostName); @@ -78,52 +80,97 @@ //List<ServiceHostInfo> startupList = new List<ServiceHostInfo>(new ServiceHostInfo[] { new ServiceHostInfo(Settings.SELFHOST_VHOST_NAME, false, null, new object[] { new Trade.BusinessServiceImplementation.ErrorBehaviorAttribute() }, new TradeServiceWcf()) }); ////Stock call to startup the Master Host. //base.startService(new Settings(), new ConfigurationService(), new NodeCommunication(), null, new ConfigurationActions(true), startupList, null, new object[] { typeof(IOrderProcessor) }); - - Trade.BusinessServiceConfigurationSettings.Settings thisSettings = new Trade.BusinessServiceConfigurationSettings.Settings(); + + Trade.BusinessServiceConfigurationSettings.Settings thisSettings = new Trade.BusinessServiceConfigurationSettings.Settings(); Trade.OrderProcessorServiceConfigurationSettings.Settings thisOPSSettings = new Trade.OrderProcessorServiceConfigurationSettings.Settings(); - - + + Uri TradeServiceUriBase = new Uri("http://" + myName + ":9000/tradebusinessservice"); //Create a ServiceHost for the Orderprocessor service. Type serviceType = typeof(TradeServiceWcf); - ServiceHost host = new ServiceHost(serviceType, TradeServiceUriBase); - addEndpointsAndBehaviors(host, TradeServiceUriBase); - host.Open(); + serviceHost = new ServiceHost(serviceType, TradeServiceUriBase); + addEndpointsAndBehaviors(serviceHost, TradeServiceUriBase); + serviceHost.Open(); } - + private ServiceHost addEndpointsAndBehaviors(ServiceHost host, Uri uri) { - ServiceMetadataBehavior mexbehavior = new ServiceMetadataBehavior(); - mexbehavior.HttpGetEnabled = true; - mexbehavior.HttpGetUrl = uri; - host.Description.Behaviors.Add(mexbehavior); - Binding mexHttpBinding = MetadataExchangeBindings.CreateMexHttpBinding(); - ServiceEndpoint sepMex = host.AddServiceEndpoint(typeof(IMetadataExchange),mexHttpBinding,uri.AbsoluteUri + "/mex"); - BasicHttpBinding httpBinding = new BasicHttpBinding("Host_BasicHttpBinding"); - WSHttpBinding wsHttpBinding = new WSHttpBinding("Host_WsHttpBinding_M_Security"); - host.AddServiceEndpoint(typeof(ITradeServices), httpBinding, ""); - host.AddServiceEndpoint(typeof(ITradeServices), wsHttpBinding, "msec"); - - //iterate and display all endpoints via host.Description.Endpoints - //Describe for Console output - StockTraderUtility.DescribeService(host); + ServiceMetadataBehavior mexbehavior = new ServiceMetadataBehavior(); + mexbehavior.HttpGetEnabled = true; + mexbehavior.HttpGetUrl = uri; + host.Description.Behaviors.Add(mexbehavior); + Binding mexHttpBinding = MetadataExchangeBindings.CreateMexHttpBinding(); + ServiceEndpoint sepMex = host.AddServiceEndpoint(typeof(IMetadataExchange), mexHttpBinding, uri.AbsoluteUri + "/mex"); + BasicHttpBinding httpBinding = new BasicHttpBinding("Host_BasicHttpBinding"); + WSHttpBinding wsHttpBinding = new WSHttpBinding("Host_WsHttpBinding_M_Security"); + host.AddServiceEndpoint(typeof(ITradeServices), httpBinding, ""); + host.AddServiceEndpoint(typeof(ITradeServices), wsHttpBinding, "msec"); - return host; + //iterate and display all endpoints via host.Description.Endpoints + //Describe for Console output + StockTraderUtility.DescribeService(host); + + return host; + } + + public void Dispose() + { + if (serviceHost != null) + serviceHost.Close(); } } [STAThread] static void Main(string[] args) { + Console.Clear(); Console.SetWindowSize(Console.LargestWindowWidth - 30, Console.LargestWindowHeight - 30); Console.Title = ".NET StockTrader Business Services Host"; - MyHost myHost = new MyHost(); - myHost.startUp(); - Console.WriteLine(" ORDER_PROCESSING_MODE is currently = {0}\n", Trade.BusinessServiceConfigurationSettings.Settings.ORDER_PROCESSING_MODE); - Console.WriteLine(" {0} is started.\n Press any key to quit.", Console.Title); - Console.ReadLine(); + + bool restart = true; + while (restart) + { + using (var serviceHost = new BusinessServiceConsoleHost()) + { + serviceHost.start(); + + Console.WriteLine(" ORDER_PROCESSING_MODE is currently = {0}\n", + Trade.BusinessServiceConfigurationSettings.Settings.ORDER_PROCESSING_MODE); + Console.WriteLine(" {0} is started\n", Console.Title); + + Console.WriteLine("Press Enter to quit or Ctrl+R to restart"); + + restart = WaitForRestart(); + } + } + } + + private static bool WaitForRestart() + { + while (true) + { + // clear users input + Console.CursorLeft = 0; + Console.Write(' '); + Console.CursorLeft = 0; + + // read users input + var key = Console.ReadKey(); + if ((key.Modifiers & ConsoleModifiers.Control) != 0 + && key.Key == ConsoleKey.R) + { + // create a new object to reload the static data. + var settings = + new Trade.BusinessServiceConfigurationSettings.Settings(); + return true; + } + if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Escape) + { + return false; + } + } } } }