【问题标题】:Custom ServiceHost with DI and InstanceContextMode.Percall具有 DI 和 InstanceContextMode.Percall 的自定义 ServiceHost
【发布时间】:2013-05-30 23:24:10
【问题描述】:

在我的托管应用程序中,我目前的 WCF 服务运行为:

SomeService service = new SomeService(container) //IUnityContainer
ServiceHost serviceHost = new ServiceHost(service, serviceAddress);

有什么收获? SomeService 定义为:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single

这已经不行了,我需要让它InstanceContextMode.PerCall

当尝试 .Open() 如果将 InstanceContextMode 更改为“PerCall” - 它会抛出:

System.InvalidOperationException: In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single.  This can be configured via the ServiceBehaviorAttribute.  Otherwise, please consider using the ServiceHost constructors that take a Type argument

这是我的问题的解决方案吗? How do I pass values to the constructor on my wcf service?

我的主要担忧:
我在这个托管应用程序中运行不同类型的服务,看来这个解决方案只有在我运行一种类型的服务时才是好的。

【问题讨论】:

    标签: c# wcf servicehost


    【解决方案1】:

    当需要多个服务实例(PerCall 或 PerSession)时,将单个服务实例传递给 ServiceHost 是不够的……这是个例外。

    控制实例创建由IInstanceProvider 管理。

    这是我的问题的解决方案吗? How do I pass values to the constructor on my wcf service?

    这只回答了你问题的一半。您正在使用 Unity。创建容器的管理需要成为实现的一部分。最常见的解决方案是使用Unity.WCF,它也可以作为NuGet 包提供。

    请注意,Unity.WCF 不支持基于对象生命周期的 WCF OperationContexts。有多个(更复杂的)实现,例如this

    【讨论】:

    • 你的意思是我需要这样的东西:weblogs.asp.net/fabio/archive/2009/03/24/…
    • 但是当我有许多接口而不仅仅是一个派生的服务主机时会发生什么?
    • 是的。 Fabio Cozzolino 的博客正是您所需要的。不确定“许多接口”评论指的是什么。你能详细说明一下吗?
    • 许多接口我的意思是服务,我有 ClientService、CheckService,它们都在本地和并行运行.. 我的意思是我的应用程序在不同的端点上有不同的 IService,具有相同的行为,例如 localhost:7005/ ClientService、localhost:7005/CheckService 等。
    • 你知道我如何在容器内注册服务吗?太糟糕了,他不提供编码容器注册,他正在使用配置文件..配置文件编辑从版本到版本:(
    猜你喜欢
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多