我想问我如何在 Windows 服务中初始化 StructureMap?
我得到的错误是:提供的服务类型无法作为服务加载,因为它没有默认(无参数)构造函数。要解决此问题,请将默认构造函数添加到类型,或将类型的实例传递给主机。
我已经使用 Visual Studio 2008 创建了一个 Windows 窗体应用程序。在这个应用程序中,我添加了这些文件:
- 服务.cs
- MyPrivateServiceService.cs
- 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();
});
}
}
谢谢
奥里