Advertisement
Guest User

Untitled

a guest
May 25th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Description;
  10. using System.ServiceModel.Security;
  11. using System.ServiceProcess;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14.  
  15. namespace Zaneuski.Casino.WindowsService
  16. {
  17.     public partial class CasinoService : ServiceBase
  18.     {
  19.         private ServiceHost[] _hosts;
  20.  
  21.         public CasinoService()
  22.         {
  23.             Assembly assembly = Assembly.Load("Zaneuski.Casino.WcfServiceLibrary");
  24.  
  25.             Type[] types = assembly.GetTypes().Where(o => o.IsClass && o.GetInterfaces().Count() == 1).ToArray();
  26.  
  27.             _hosts = new ServiceHost[types.Count()];
  28.  
  29.             for (int i = 0; i < this._hosts.Count(); ++i)
  30.             {
  31.                 this._hosts[i] = null;
  32.             }
  33.             InitializeComponent();
  34.         }
  35.  
  36.         protected override void OnStart(string[] args)
  37.         {
  38.             //http://localhost:9001/CalcService
  39.             string adrHttpBase = "http://localhost:8943/";
  40.  
  41.             Assembly assembly = Assembly.Load("Zaneuski.Casino.WcfServiceLibrary");
  42.  
  43.             Type[] types = assembly.GetTypes().Where(o => o.IsClass && o.GetInterfaces().Count() == 1).ToArray();
  44.  
  45.             for (int i = 0; i < this._hosts.Count(); ++i)
  46.             {
  47.                 if (this._hosts[i] != null) this._hosts[i].Close();
  48.  
  49.                 string serviceName = types[i].ToString().Split('.').Last();
  50.                 string strAdrHTTP = adrHttpBase + serviceName;
  51.  
  52.                 Uri adrbase = new Uri(strAdrHTTP);
  53.  
  54.                 this._hosts[i] = new ServiceHost(types[i], adrbase);
  55.  
  56.                 ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
  57.                 this._hosts[i].Description.Behaviors.Add(mBehave);
  58.  
  59.                 Type interfaceType = types[i].GetInterfaces().Last();
  60.  
  61.                 BasicHttpBinding httpb = new BasicHttpBinding();
  62.                 this._hosts[i].AddServiceEndpoint(interfaceType, httpb, strAdrHTTP);
  63.                 this._hosts[i].AddServiceEndpoint(typeof(IMetadataExchange),
  64.                 MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
  65.  
  66.                 this._hosts[i].Open();
  67.             }
  68.         }
  69.  
  70.         protected override void OnStop()
  71.         {
  72.             for (int i = 0; i < this._hosts.Count(); ++i)
  73.             {
  74.                 if (this._hosts[i] != null)
  75.                 {
  76.                     this._hosts[i].Close();
  77.                     this._hosts[i] = null;
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement