c# - 使用结构图注入(inject) wcf 客户端的可能方法

标签 c# wcf structuremap

我想使用 Structuremap 来注入(inject) WCF 客户端,但我也希望该客户端从配置中读取端点和绑定(bind)配置。 我尝试了以下方法:

For<IServiceClient>().LifecycleIs(new UniquePerRequestLifecycle()).Use<ServiceClient>().
            Ctor<string>("endpointConfigurationName").Is("WsHttpBinding_IService");

但这会导致错误:

StructureMap Exception Code: 205\nMissing requested Instance property \"remoteAddress\" for InstanceKey \"e50e036b-9d71-47de-8ac2-d53a641e9be8\"

当我传递远程地址时,它的工作方式如预期:

 For<IServiceClient>().LifecycleIs(new UniquePerRequestLifecycle()).Use<ServiceClient>()
           .Ctor<string>("endpointConfigurationName").Is("WsHttpBinding_IService")
           .Ctor<string>("remoteAddress").Is("https://myurl/Service.svc");

我不明白为什么第一个构造函数重载不从配置中读取端点地址? 配置如下所示:

>

 <client>
      <endpoint address="https://myurl/Service.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService" contract="IService" name="WsHttpBinding_IService" />
 </client>

最佳答案

第一个重载可能确实读取了端点地址,但 StructureMap 从不调用第一个重载,而是始终调用最贪婪的构造函数。首选使用 Use 方法并提供工厂委托(delegate):

For<IService>()
  .LifecycleIs(new UniquePerRequestLifecycle())
  .Use(() => new ServiceClient("WsHttpBinding_IService"))

这会强制为此生成的类使用正确的构造函数。也许将该类隐藏在代理后面会更好。这样就可以隐藏the annoying quirks您的应用程序中的 WCF。对于您自己创建的类型,prevent having multiple constructors .

关于c# - 使用结构图注入(inject) wcf 客户端的可能方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19049685/

相关文章:

c# - 如何强制 WCF 线程中未处理的异常使进程崩溃?

c# - 如何使用 Azure 语音转文本和 C# 生成时间戳?

c# - 隐藏 app.config

wcf - WCF 客户端代理引发的 TimeoutException

c# - 对 WCF 服务的异步调用不保留 CurrentCulture

asp.net - 尝试获取 AccountController 类型的实例时发生激活错误, key \"\""

c# - 一次调用即可接收按文档类型分组的搜索结果(NEST,AWS Elasticsearch)

c# - C# 对象的深拷贝

asp.net-mvc-5 - 使用 StructureMap 时找不到 ServiceLocatorImplBase.cs

inversion-of-control - StructureMap - 覆盖命名实例的构造函数参数