主机WCF服务使用顶层货架和温莎城堡

时间:2015-03-02 10:23:33

标签: c# wcf castle-windsor topshelf windsor-facilities

我正在使用Topshelf来运行我的wcf服务和windsor城堡以进行依赖注入。问题当我直接运行主机程序时,它作为控制台主机运行正常,但同一程序在尝试启动它作为Windows服务时出现此错误。

Topshelf.Hosts.StartHost Error: 0 : The service failed to start., System.Invalid
OperationException: Cannot start service MyService on computer '.'. ---> Sy
stem.ComponentModel.Win32Exception: The service did not respond to the start or
control request in a timely fashion
   --- End of inner exception stack trace ---
   at System.ServiceProcess.ServiceController.Start(String[] args)
   at System.ServiceProcess.ServiceController.Start()
   at Topshelf.Runtime.Windows.WindowsHostEnvironment.StartService(String servic
eName, TimeSpan startTimeOut)
   at Topshelf.Hosts.StartHost.Run()

以下是我的实施: -

      class Program{
       static void Main(string[] args)
       {
        var boot = new ServiceInstaller();

        HostFactory.Run(x =>
        {
            x.Service<Program>(s =>
            {
                s.ConstructUsing(name => new Program());
                s.WhenStarted(p => p.Start());
                s.WhenStopped(p => p.Stop());
            });
            x.DependsOnMsSql();
            x.StartAutomatically();

            x.SetDisplayName("My Service");
            x.SetServiceName("MyService");
        });
        }

        public void Start()
        {
            try
            {

                _serviceProvider = typeof(IMyService).AssemblyQualifiedName;
                LogMessage(LogLevel.INFO, _serviceProvider, null);
                if (_serviceProvider == null)
                {
                    throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, "Failed to start service: {0}", _serviceProvider));
                }
                _serviceHost = new DefaultServiceHostFactory().CreateServiceHost(_serviceProvider, new Uri[0]);
                _serviceHost.Open();
            }
            catch (Exception ex)
            {
                LogMessage(LogLevel.ERROR, "Error occurred while starting My Service", ex);
                throw;
            }
        }


        public void Stop()
        {

            try
            {

                _serviceHost.Close();

                _serviceHost = null;
            }
            catch (Exception ex)
            {
                LogMessage(LogLevel.ERROR, "Error occurred while stopping My service ", ex);
                throw;
            }

        }
 }
public Class ServiceInstaller{
public static IWindsorContainer Container { get; private set; }

        public ServiceInstaller()
        {
            try
            {
                Container = new WindsorContainer();
                Container.Register(Component.For<IWindsorContainer>().Instance(Container));


                Container.AddFacility<WcfFacility>().Register
                (
                     Component.For<IDependency>().ImplementedBy<Dependency>().LifestyleTransient(),
                     Component.For<IMyService>().ImplementedBy<MyService>().LifestyleTransient()
                );

            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (Container != null)
                {
                    Container.Dispose();
                    Container = null;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我发现在我注入的依赖项中,我有代码在构造函数中访问数据库。删除该代码后,它运行正常。不知道为什么使用控制台主机运行时相同的代码运行正常。我想城堡温莎有些问题。