【问题标题】:Host WCF service using top shelf and windsor castle使用顶层架子和温莎城堡托管 WCF 服务
【发布时间】:2015-03-02 10:23:33
【问题描述】:

我正在使用 Topshelf 运行我的 wcf 服务和 Windsor castle 进行依赖注入。当我直接运行主机程序时出现问题,它作为控制台主机运行良好,但同一程序在尝试将其作为 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;
                }
            }
        }
    }

【问题讨论】:

  • 可能 Start 中的代码在作为服务运行时会抛出异常。尝试在每条语句之后和第一条语句之前添加一条日志消息,以查看抛出了哪一行。
  • 这可能是真的,但为什么它在通过 Visual Studio 调试或只是在 bin 文件夹中运行时运行良好。
  • 您是否检查过 Windows 事件日志以查看那里是否记录了异常?
  • 是的,这是记录到 Windows 事件日志中的异常 应用程序:MyServiceHost.exe 框架版本:v4.0.30319 描述:进程因未处理的异常而终止。异常信息:Castle.MicroKernel.ComponentActivator.ComponentActivatorException 堆栈:在 MyServiceHost.Program.Main(System.String[])
  • 你能发布你的 Dependency 和 MyService 类吗?我怀疑问题出在他们身上。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多