如何以编程方式修改 WCF app.config 端点地址设置?
我想以编程方式修改我的 app.config 文件以设置应使用哪个服务文件端点。 在运行时执行此操作的最佳方法是什么? 以供参考:
<endpoint address="http://mydomain/MyService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IASRService"
contract="ASRService.IASRService" name="WSHttpBinding_IASRService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
I'd like to programmatically modify my app.config file to set which service file endpoint should be used. What is the best way to do this at runtime? For reference:
<endpoint address="http://mydomain/MyService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IASRService"
contract="ASRService.IASRService" name="WSHttpBinding_IASRService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是在客户端吗?
如果是这样,您需要创建一个 WsHttpBinding 实例和一个 EndpointAddress,然后将这两个传递给将这两个作为参数的代理客户端构造函数。
如果它位于服务器端,您需要以编程方式创建自己的 ServiceHost 实例,并向其添加适当的服务端点。
当然,您可以将多个服务端点添加到您的服务主机。 完成后,您需要通过调用 .Open() 方法打开服务主机。
如果您希望能够在运行时动态选择要使用的配置,您可以定义多个配置,每个配置都有一个唯一的名称,然后使用该配置调用适当的构造函数(针对您的服务主机或代理客户端)您想要使用的名称。
例如,您可以轻松地拥有:(
三个不同的名称,通过指定不同的绑定配置来获得不同的参数),然后只需选择正确的一个来实例化您的服务器(或客户端代理)。
但在这两种情况下(服务器和客户端),您必须在实际创建服务主机或代理客户端之前进行选择。 一旦创建,它们就是不可变的 - 一旦它们启动并运行,您就无法对其进行调整。
马克
Is this on the client side of things??
If so, you need to create an instance of WsHttpBinding, and an EndpointAddress, and then pass those two to the proxy client constructor that takes these two as parameters.
If it's on the server side of things, you'll need to programmatically create your own instance of ServiceHost, and add the appropriate service endpoints to it.
Of course you can have multiple of those service endpoints added to your service host. Once you're done, you need to open the service host by calling the .Open() method.
If you want to be able to dynamically - at runtime - pick which configuration to use, you could define multiple configurations, each with a unique name, and then call the appropriate constructor (for your service host, or your proxy client) with the configuration name you wish to use.
E.g. you could easily have:
(three different names, different parameters by specifying different bindingConfigurations) and then just pick the right one to instantiate your server (or client proxy).
But in both cases - server and client - you have to pick before actually creating the service host or the proxy client. Once created, these are immutable - you cannot tweak them once they're up and running.
Marc
我使用以下代码更改 App.Config 文件中的端点地址。 您可能需要在使用前修改或删除命名空间。
I use the following code to change the endpoint address in the App.Config file. You may want to modify or remove the namespace before usage.
我就是这样做的。 好处是它仍然从配置中获取端点绑定设置的其余部分并仅替换 URI。
I did it like this. The good thing is it still picks up the rest of your endpoint binding settings from the config and just replaces the URI.
这个简短的代码对我有用:
当然,您必须在配置更改后创建 ServiceClient 代理。
您还需要引用 System.Configuration 和 System.ServiceModel 程序集才能完成这项工作。
干杯
this short code worked for me:
Of course you have to create the ServiceClient proxy AFTER the config has changed.
You also need to reference the System.Configuration and System.ServiceModel assemblies to make this work.
Cheers
这是最短的代码,即使没有定义配置部分,您也可以使用它来更新应用程序配置文件:
This is the shortest code that you can use to update the app config file even if don't have a config section defined:
我修改并扩展了 Malcolm Swaine 的代码,以通过名称属性修改特定节点,并修改外部配置文件。 希望能帮助到你。
}
I have modified and extended Malcolm Swaine's code to modify a specific node by it's name attribute, and to also modify an external config file. Hope it helps.
}
修改 config 中的 uri 或 config 中的绑定信息非常容易。
这是你想要的吗?
It's very easy to modify the uri in config or binding info in config.
Is this what you want?
不管怎样,我需要更新 RESTFul 服务的 SSL 端口和方案。 这就是我所做的。 抱歉,这比原来的问题多了一点,但希望对某人有用。
For what it's worth, I needed to update the port and scheme for SSL for my RESTFul service. This is what I did. Apologies that it is a bit more that the original question, but hopefully useful to someone.
我认为你想要的是在运行时交换配置文件的版本,如果是的话,创建一个具有正确地址的配置文件副本(也给它相关的扩展名,如 .Debug 或 .Release)(这给了你调试版本和运行时版本)并创建一个构建后步骤,根据构建类型复制正确的文件。
这是我过去使用过的构建后事件的示例,它使用正确的版本(调试/运行时)覆盖输出文件,
其中:
$(ProjectDir) 是配置文件所在的项目目录
$(ConfigurationName) 是活动配置构建类型
编辑:
请参阅 Marc 的回答,了解如何以编程方式执行此操作的详细说明。
I think what you want is to swap out at runtime a version of your config file, if so create a copy of your config file (also give it the relevant extension like .Debug or .Release) that has the correct addresses (which gives you a debug version and a runtime version ) and create a postbuild step that copies the correct file depending on the build type.
Here is an example of a postbuild event that I've used in the past which overrides the output file with the correct version (debug/runtime)
where :
$(ProjectDir) is the project directory where the config files are located
$(ConfigurationName) is the active configuration build type
EDIT:
Please see Marc's answer for a detailed explanation on how to do this programmatically.
您可以这样做:
例如,我想在运行时修改我的服务端点地址,因此我有以下 ServiceEndpoint.xml 文件。
用于读取您的 xml:
然后在运行时获取客户端的 web.config 文件并将服务端点地址分配为:
You can do it like this:
For example , i want to modify my service endpoint address at runtime so i have the following ServiceEndpoint.xml file.
For reading your xml :
Then get your web.config file of your client at runtime and assign the service endpoint address as:
查看您是否将客户端部分放置在正确的 web.config 文件中。 SharePoint 大约有 6 到 7 个配置文件。
http://msdn.microsoft.com/ en-us/library/office/ms460914(v=office.14).aspx (http://msdn.microsoft.com/en-us/library/office/ms460914%28v=office.14%29.aspx)
发布此内容您可以简单地尝试
ServiceClient client = new ServiceClient("ServiceSOAP");
see if you are placing the client section in the correct web.config file. SharePoint has around 6 to 7 config files.
http://msdn.microsoft.com/en-us/library/office/ms460914(v=office.14).aspx (http://msdn.microsoft.com/en-us/library/office/ms460914%28v=office.14%29.aspx)
Post this you can simply try
ServiceClient client = new ServiceClient("ServiceSOAP");