Пример #1
0
 /// <summary>
 /// StopService method implementation
 /// </summary>
 public void StopService()
 {
     try
     {
         if (Servicehost != null)
         {
             if (Servicehost.State == CommunicationState.Opened)
             {
                 Servicehost.Close();
             }
             Servicehost = null;
         }
         Started = false;
     }
     catch (Exception e)
     {
         Started = false;
         throw e;
     }
 }
Пример #2
0
        /// <summary>
        /// StartService method implementation
        /// </summary>
        public void StartService(ServiceBase svc)
        {
            try
            {
                Servicebase = svc;
                if (Servicehost != null)
                {
                    if (Servicehost.State == CommunicationState.Opened)
                    {
                        Servicehost.Close();
                    }
                    Servicehost = null;
                }

                Servicehost = new ReplayServiceHost(new EventLogDependency(svc.EventLog), typeof(T), new Uri(string.Format("net.tcp://{0}:5987/ReplayService", Dns.GetHostEntry("localhost").HostName)));
                ServiceMetadataBehavior   smb = Servicehost.Description.Behaviors.Find <ServiceMetadataBehavior>();
                ServiceDebugBehavior      dbg = Servicehost.Description.Behaviors.Find <ServiceDebugBehavior>();
                ServiceThrottlingBehavior prf = Servicehost.Description.Behaviors.Find <ServiceThrottlingBehavior>();
                if (smb == null)
                {
                    smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled  = false;
                    smb.HttpsGetEnabled = false;
                    smb.MetadataExporter.PolicyVersion = PolicyVersion.Default;
                    Servicehost.Description.Behaviors.Add(smb);
                }
                else
                {
                    smb.HttpGetEnabled  = false;
                    smb.HttpsGetEnabled = false;
                    smb.MetadataExporter.PolicyVersion = PolicyVersion.Default;
                }
                if (dbg == null)
                {
                    dbg = new ServiceDebugBehavior();
                    dbg.HttpHelpPageEnabled            = false;
                    dbg.HttpsHelpPageEnabled           = false;
                    dbg.IncludeExceptionDetailInFaults = true;
                    Servicehost.Description.Behaviors.Add(dbg);
                }
                else
                {
                    dbg.HttpHelpPageEnabled            = false;
                    dbg.HttpsHelpPageEnabled           = false;
                    dbg.IncludeExceptionDetailInFaults = false;
                }
                if (prf == null)
                {
                    prf = new ServiceThrottlingBehavior();
                    prf.MaxConcurrentCalls     = 256;
                    prf.MaxConcurrentInstances = 256;
                    prf.MaxConcurrentSessions  = 256;
                    Servicehost.Description.Behaviors.Add(prf);
                }
                else
                {
                    prf.MaxConcurrentCalls     = 250;
                    prf.MaxConcurrentInstances = 250;
                    prf.MaxConcurrentSessions  = 250;
                }

                NetTcpBinding tcp = new NetTcpBinding(SecurityMode.None);
                tcp.MaxConnections = 256;

                if (useEncryption)
                {
                    CustomBinding custombinding = new CustomBinding(tcp);
                    custombinding.Name = "MFABinding";
                    var currentEncoder = custombinding.Elements.Find <MessageEncodingBindingElement>();
                    if (currentEncoder != default(MessageEncodingBindingElement))
                    {
                        custombinding.Elements.Remove(currentEncoder);
                        custombinding.Elements.Insert(0, new ReplayMessageEncodingBindingElement());
                    }
                    Servicehost.AddServiceEndpoint(typeof(IReplay), custombinding, "");
                }
                else
                {
                    Servicehost.AddServiceEndpoint(typeof(IReplay), tcp, "");
                }
                Servicehost.Open();
                Started = true;
            }
            catch (Exception e)
            {
                Started = false;
                throw e;
            }
        }