0

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

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

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

  1. 服务.cs
  2. MyPrivateServiceService.cs
  3. AppRegistry.cs

代码:公共类 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();
        });
    }
}

谢谢

奥里

4

1 回答 1

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 吗?

谢谢你

奥里

于 2012-11-23T12:48:57.127 回答