如何在Windows服务中初始化StructureMap

时间:2012-11-22 17:05:25

标签: web-services service windows-services inversion-of-control structuremap

我想问一下如何在Windows服务中初始化StructureMap?

我得到的错误是: 提供的服务类型无法作为服务加载,因为它没有默认(无参数)构造函数。要解决此问题,请在类型中添加默认构造函数,或将类型的实例传递给主机。

我使用visual studio 2008创建了一个Windows窗体应用程序。 在这个应用程序中,我添加了这些文件:

  1. service.cs
  2. MyPrivateServiceService.cs
  3. AppRegistry.cs
  4. 代码:  公共类MyPrivateServiceService

    {
        private readonly IAdapter adapter;
        public MyPrivateServiceService(IAdapter adapter)
        {
            this.adapter = adapter;
        }
        [OperationBehavior]
        public void DoSomthing()
        {
            var res = adapter.GetById(1);
        }
    }
    partial class Service : WcfServiceBase
    {
        static Service()
        {
            GlobalContext.Properties["PROGRNAAM"] = ServiceNaamKort;
            log = LogManager.GetLogger(typeof(Service));
            InitializeIoc();
        }
        private static void InitializeIoc()
        {
            ObjectFactory.Configure(x =>
            {
                x.AddRegistry<AppRegistry>();
            });
        }
        protected override void OnStart(string[] args)
        {
            try
            {
                base.OnStart(args);
                host = new ServiceHost(typeof(MyPrivateServiceService));
                host.Open();
    
            }
            catch (Exception ex)
            {
                log.Fatal("....", ex);
                DisposeHost();
                throw;
            }
        }
    }
    public class AppRegistry : Registry
    {
        public AppRegistry()
        {
            Scan(s =>
            {
                s.WithDefaultConventions();
            });
        }
    }
    

    谢谢

    大利

1 个答案:

答案 0 :(得分:0)

我找到了这个链接:http://lostechies.com/jimmybogard/2008/07/30/integrating-structuremap-with-wcf/

当我添加像这样的新构造函数时,它可以工作:

public CustomerSearchService()
: this(ObjectFactory.GetInstance<ICustomerRepository>(), 
    ObjectFactory.GetInstance<ICustomerSummaryMapper>())

{ }

我试图实现StructureMapServiceHostFactory,但我正在使用Windows服务,任何想法如何使用service.cs初始化StructureMapServiceHostFactory?

谢谢

大利