WCF集成工具和自托管服务

3

我自己托管了几个服务,注册服务时我会这样做:

host = new ServiceHost(typeof(MyService));
host.Open();

在幕后,wcf通过默认构造函数实例化了我的服务。
当我在自主托管时,是否可以使用Castle Windsor的WCF集成设施让WCF调用Windsor创建服务?
示例似乎展示了IIS托管服务,其中MyService.svc文件的第一行如下所示:
<%@ServiceHost language=c# Debug="true" 
     Service="Microsoft.ServiceModel.Samples.CalculatorService" 
     Factory=WindsorServiceHostFactory%>

WCF使用工厂来实例化服务实例,这是一个预设的地方。


实际上,工厂用于实例化“ServiceHost”的一个实例。然后,该主机将根据需要创建服务类实例来处理传入的请求。 - marc_s
2个回答

4

谢谢,这几乎就是我需要的。太棒了。 - IanT8

0

使用Castle 3.0,实现此操作的方法如下:

// setup IoC, including registering the WcfFacility from Castle.WcfIntegrationFacility
container.AddFacility<WcfFacility>();
// and all other registrations you need

然后,假设您在app.config中具有必要的详细信息或者对默认值感到满意:

// T is an interface with a backing service implementation registered in your IoC
public static ServiceHostBase StartHostService<T>(string serviceUrl)
{
    var assemblyQualifiedName = typeof(T).AssemblyQualifiedName;
    var serviceHost = new DefaultServiceHostFactory()
        .CreateServiceHost(assemblyQualifiedName, new[] { new Uri(serviceUrl) });
    serviceHost.Open();

    return serviceHost;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接