简体   繁体   English

如何从* .config文件以外的其他位置进行动态生成的.net服务客户端读取配置

[英]How to make dynamically generated .net service client read configuration from another location than *.config files

I've currently written code to use the ServiceContractGenerator to generate web service client code based on a wsdl, and then compile it into an assembly in memory using the code dom. 我目前编写的代码是使用ServiceContractGenerator生成基于wsdl的Web服务客户端代码,然后使用代码dom将其编译到内存中的程序集中。 I'm then using reflection to set up the binding, endpoint, service values/types, and then ultimately invoke the web service method based on xml configuration that can be altered at run time. 然后我使用反射来设置绑定,端点,服务值/类型,然后最终调用基于可在运行时更改的xml配置的Web服务方法。

This all currently works fine. 这一切目前工作正常。 However, the problem I'm currently running into, is that I'm hitting several exotic web services that require lots of custom binding/security settings. 但是,我目前遇到的问题是,我遇到了几个需要大量自定义绑定/安全设置的异国情调的Web服务。 This is forcing me to add more and more configuration into my custom xml configurations, as well as the corresponding updates to my code to interpret and set those binding/security settings in code. 这迫使我在我的自定义xml配置中添加越来越多的配置,以及我的代码的相应更新,以解释和设置代码中的绑定/安全设置。

Ultimately, this makes adding these 'exotic' services slower, and I can see myself eventually reimplementing the 'system.serviceModel' section of the web or app.config file, which is never a good thing. 最终,这使得添加这些“异国情调”的服务变得更慢,我可以看到自己最终重新实现了web或app.config文件的'system.serviceModel'部分,这从来都不是一件好事。

My question is, and this is where my lack of experience .net and C# shows, is there a way to define the configuration normally found in the web.config or app.config 'system.serviceModel' section somewhere else, and at run time supply this to configuration to the web service client? 我的问题是,这是我缺乏经验.net和C#显示的地方,有没有办法定义通常在web.config或app.config'system.serviceModel'部分中找到的配置,在运行时将此配置提供给Web服务客户端?

Is there a way to attach an app.config directly to an assembly as a resource or any other way to supply this configuration to the client? 有没有办法将app.config作为资源直接附加到程序集或以其他任何方式将此配置提供给客户端?

Basically, I'd like attach an app.config only containing a 'system.serviceModel' to the assembly containing a web service client so that it can use its configuration. 基本上,我想将一个仅包含'system.serviceModel'的app.config附加到包含Web服务客户端的程序集,以便它可以使用其配置。 This way I wouldn't need to handle every configuration under the sun, I could let .net do it for me. 这样我就不需要处理太阳下的每一个配置,我可以让.net为我做。

Fyi, it's not an option for me to put the configuration for every service in the app.config for the running application. Fyi,我不能选择为app.config中的每个服务配置正在运行的应用程序。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks in advance! 提前致谢! Bryan 布赖恩

  • Create a custom class deriving from ChannelFactory. 创建一个从ChannelFactory派生的自定义类。
  • Override the protected CreateDescription method. 覆盖受保护的CreateDescription方法。 In the override, you need to... 在覆盖中,你需要......
  • Call base.CreateDescription(). 调用base.CreateDescription()。
  • Read in your custom configuration. 读入您的自定义配置。
  • Create a custom ServiceEndpoint based on your configuration. 根据您的配置创建自定义ServiceEndpoint。 Don't forget the bindings, behaviors, etc. 不要忘记绑定,行为等。
  • Return that custom ServiceEndpoint. 返回自定义ServiceEndpoint。

More details HERE 更多细节在这里

Are your proxy classes deriving from ClientBase<T> ? 你的代理类派生自ClientBase<T>吗? If so, then there is a constructor that accepts a Binding and an EndpointAddress . 如果是这样,那么有一个接受BindingEndpointAddress的构造函数。 You should be able to use those instead of the corresponding configuration data. 您应该能够使用它们而不是相应的配置数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Azure 应用配置动态更新 .net 核心配置 - Dynamically update .net core config from Azure App Configuration 如何从 ASP.NET5 中的 config.json 正确读取嵌套配置值? - How to properly read nested configuration values from config.json in ASP.NET5? WCF服务客户端配置生成错误 - WCF Service client config generated wrong 如何允许使用 MSIX 生成可编辑的.Net 配置文件? - How to allow for editable .Net generated config files with MSIX? 来自 appsetting.json (.NET Core) 的 WCF 服务客户端配置 - WCF service client configuration from appsetting.json (.NET Core) 如何在WCF服务和客户端配置文件中禁用安全性 - How Can I Disable Security In WCF Service And Client Configuration Files 使用.NET C#从azure云服务读取配置设置 - Read configuration settings from azure cloud service with .NET C# .net服务-如何在web.config中设置多个数据库 - .net service - how to set more than 1 database in web.config 如何通过不同的配置动态使用不同的 Web.config 值并使用 Base.config (.NET Framework) - How to use different Web.config value by different configuration dynamically and use Base.config (.NET Framework) 如何在ASP.NET MVC中统计动态生成的控件 - How To Make Count Of Dynamically Generated Controls In ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM