gpt4 book ai didi

c# - 配置 WCF 依赖项

转载 作者:行者123 更新时间:2023-11-30 17:59:36 27 4
gpt4 key购买 nike

我正在尝试在 Windows 服务主机中托管我的 WCF 服务。为了管理依赖项,我使用了 CaSTLe WCF 工具。

这是我的 ContainerConfiguration(BootStrapper) 的样子:

 public class ConfigureContainer : IConfigureContainer
{
private const string ServiceOne= "ServiceOne";
private const string ServiceTwo = "ServiceTwo";
private const string ServiceThree = "ServiceThree";
private const string CurrentAssembly = "MyAssembly";

private readonly IWindsorContainer container;

public ConfigureContainer(IWindsorContainer container)
{
this.container = container;
}


public IWindsorContainer WindsorContainer { get { return container; } }


public void AndRegisterComponents()
{
container.Register(AllTypes.FromAssemblyNamed(CurrentAssembly)
.Pick().If(type => type.GetInterfaces().Any(i => i.IsDefined(typeof(ServiceContractAttribute), true)))
.Configure(configurer => configurer
.Named(configurer.Implementation.Name)
.AsWcfService(
new DefaultServiceModel()
.AddEndpoints(
WcfEndpoint.FromConfiguration(ServiceOne),
WcfEndpoint.FromConfiguration(ServiceTwo),
WcfEndpoint.FromConfiguration(ServiceThree))
.PublishMetadata()))
.WithService.Select((type, baseTypes) => type.GetInterfaces()
.Where(i => i.IsDefined(typeof(ServiceContractAttribute), true))));

}
}

这就是我在服务主机中进行托管的方式:

partial class DataServicesHost : ServiceBase
{
private IWindsorContainer windsorContainer;
public DataServicesHost()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
var configure = new ConfigureContainer();
windsorContainer = configure.WindsorContainer;
}

protected override void OnStop()
{
if(windsorContainer != null)
{
windsorContainer.Dispose();
windsorContainer = null;
}
}
}

我的ServiceOne实现如下:

[ServiceContract]
internal interface IServiceOne
{
[OperationContract]
void DoSomething();
}

public class ServiceOne : IServiceOne
{
private readonly IDependency dependency;

public ServiceOne(IDependency dependency)
{
this.dependency = dependency;
}

public void DoSomething()
{
dependency.GetSomething();
//do something
}
}

public interface IDependency
{
void GetSomething();
}

public class Dependency : IDependency
{
public void GetSomething()
{
//GetSomeThing
}
}

现在我的问题是:如何将 IDependency 传递给容器?我将如何配置它,以便在托管它时,它不会提示没有让主机知道依赖项并继续查找和故障转移默认构造函数实现?

谢谢,-迈克

最佳答案

您需要在 WCF ServiceHost 中为 WCF 服务启动 Windsor 容器,而不是 Windows 服务代码。查看 Castle WCF facility 上的这两篇博文和 WCF in a Windows Service看看你需要做什么。基本上,Windows 服务代码只是启动一个 WCF ServiceHostFactory,它是您从 CaSTLe WCF 设施获得的,而该工厂实际上是配置 CaSTLe 容器的。

关于c# - 配置 WCF 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11046175/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com