C# 如何在不重新编译的情况下动态切换 .NET 中的 Web 服务地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/125399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I dynamically switch web service addresses in .NET without a recompile?
提问by stames
I have code that references a web service, and I'd like the address of that web service to be dynamic (read from a database, config file, etc.) so that it is easily changed. One major use of this will be to deploy to multiple environments where machine names and IP addresses are different. The web service signature will be the same across all deployments, just located elsewhere.
我有引用 Web 服务的代码,我希望该 Web 服务的地址是动态的(从数据库、配置文件等中读取),以便轻松更改。它的一个主要用途是部署到机器名称和 IP 地址不同的多个环境中。Web 服务签名在所有部署中都相同,只是位于其他地方。
Maybe I've just been spoiled by the Visual Studio "Add Web Reference" wizard - seems like this should be something relatively easy, though.
也许我刚刚被 Visual Studio 的“添加 Web 引用”向导宠坏了——不过,这似乎应该是相对容易的。
采纳答案by Eric Schoonover
When you generate a web reference and click on the web reference in the Solution Explorer. In the properties pane you should see something like this:
当您生成 Web 参考并单击解决方案资源管理器中的 Web 参考时。在属性窗格中,您应该会看到如下内容:
Changing the value to dynamic will put an entry in your app.config.
将值更改为动态将在您的 app.config 中放置一个条目。
Here is the CodePlex articlethat has more information.
这是包含更多信息的CodePlex 文章。
回答by Aaron Fischer
If you are fetching the URL from a database you can manually assign it to the web service proxy class URL property. This should be done before calling the web method.
如果您从数据库中获取 URL,您可以手动将其分配给 Web 服务代理类 URL 属性。这应该在调用 Web 方法之前完成。
If you would like to use the config file, you can set the proxy classes URL behavior to dynamic.
如果您想使用配置文件,您可以将代理类 URL 行为设置为动态。
回答by cruizer
As long as the web service methods and underlying exposed classes do not change, it's fairly trivial. With Visual Studio 2005 (and newer), adding a web reference creates an app.config (or web.config, for web apps) section that has this URL. All you have to do is edit the app.config file to reflect the desired URL.
只要 Web 服务方法和底层公开的类不改变,它就相当简单。对于 Visual Studio 2005(和更新版本),添加 Web 引用会创建一个具有此 URL 的 app.config(或 web.config,用于 Web 应用程序)部分。您所要做的就是编辑 app.config 文件以反映所需的 URL。
In our project, our simple approach was to just have the app.config entries commented per environment type (development, testing, production). So we just uncomment the entry for the desired environment type. No special coding needed there.
在我们的项目中,我们的简单方法是根据环境类型(开发、测试、生产)对 app.config 条目进行注释。因此,我们只需取消注释所需环境类型的条目。那里不需要特殊的编码。
回答by stefano m
Just a note about difference beetween static and dynamic.
只是关于静态和动态之间差异的说明。
- Static: you must set URL property every time you call web service. This because base URL if web service is in the proxy class constructor.
- Dynamic: a special configuration key will be created for you in your web.configfile. By default proxy class will read URL from this key.
- 静态:每次调用 Web 服务时都必须设置 URL 属性。这是因为如果 Web 服务位于代理类构造函数中,则基 URL。
- 动态:将在您的web.config文件中为您创建一个特殊的配置密钥。默认情况下,代理类将从该键读取 URL。
回答by Brad Bruce
If you are truly dynamically setting this, you should set the .Url field of instance of the proxy class you are calling.
如果你真的是动态设置这个,你应该设置你正在调用的代理类的实例的 .Url 字段。
Setting the value in the .config file from within your program:
从程序中设置 .config 文件中的值:
Is a mess;
Might not be read until the next application start.
是一个烂摊子;
在下一个应用程序启动之前可能不会被读取。
If it is only something that needs to be done once per installation, I'd agree with the other posters and use the .config file and the dynamic setting.
如果每次安装只需要完成一次,我会同意其他海报并使用 .config 文件和动态设置。
回答by mspmsp
Definitely using the Url property is the way to go. Whether to set it in the app.config, the database, or a third location sort of depends on your configuration needs. Sometimes you don't want the app to restart when you change the web service location. You might not have a load balancer scaling the backend. You might be hot-patching a web service bug. Your implementation might have security configuration issues as well. Whether it's production db usernames and passwords or even the ws security auth info. The proper separation of duties can get you into some more involved configuration setups.
绝对使用 Url 属性是要走的路。是在 app.config、数据库还是第三个位置中设置它取决于您的配置需求。有时您不希望应用程序在更改 Web 服务位置时重新启动。您可能没有扩展后端的负载均衡器。您可能正在修补 Web 服务错误。您的实现也可能存在安全配置问题。无论是生产数据库用户名和密码,还是 ws 安全身份验证信息。适当的职责分离可以让您进入一些更复杂的配置设置。
If you add a wrapper class around the proxy generated classes, you can set the Url property in some unified fashion every time you create the wrapper class to call a web method.
如果在代理生成的类周围添加包装类,则可以在每次创建包装类以调用 Web 方法时以某种统一的方式设置 Url 属性。
回答by John Kocktoasten
I've struggled with this issue for a few days and finally the light bulb clicked. The KEY to being able to change the URL of a webservice at runtime is overriding the constructor, which I did with a partial class declaration. The above, setting the URL behavior to Dynamic must also be done.
我已经为这个问题苦苦挣扎了几天,最后灯泡亮了。能够在运行时更改 Web 服务 URL 的关键是覆盖构造函数,我使用部分类声明完成了该构造。以上,将 URL 行为设置为 Dynamic 也必须完成。
This basically creates a web-service wrapper where if you have to reload web service at some point, via add service reference, you don't loose your work. The Microsoft help for Partial classes specially states that part of the reason for this construct is to create web service wrappers. http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.100).aspx
这基本上创建了一个 web 服务包装器,如果您必须在某个时候重新加载 web 服务,通过添加服务引用,您不会丢失您的工作。Microsoft 对 Partial 类的帮助特别指出,这种构造的部分原因是为了创建 Web 服务包装器。 http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.100).aspx
// Web Service Wrapper to override constructor to use custom ConfigSection
// app.config values for URL/User/Pass
namespace myprogram.webservice
{
public partial class MyWebService
{
public MyWebService(string szURL)
{
this.Url = szURL;
if ((this.IsLocalFileSystemWebService(this.Url) == true))
{
this.UseDefaultCredentials = true;
this.useDefaultCredentialsSetExplicitly = false;
}
else
{
this.useDefaultCredentialsSetExplicitly = true;
}
}
}
}
回答by mesutpiskin
open solition explorer
打开孤岛资源管理器
right click the webservice change URL Behavior to Dynamic
右键单击 webservice 将 URL 行为更改为动态
click the 'show all files' icon in solution explorer
单击解决方案资源管理器中的“显示所有文件”图标
in the web reference edit the Reference.cs file
在 Web 参考中编辑 Reference.cs 文件
change constructer
改变构造器
public Service1() {
this.Url = "URL"; // etc. string variable this.Url = ConfigClass.myURL
}
回答by djciko
For me a Reference to a WebService is a
对我来说,对 WebService 的引用是一个
SERVICE REFERENCE
服务参考
.
.
Anyway it's very easy. As someone said, you just have to change the URL in the web.config file.
无论如何,这很容易。正如有人所说,您只需要更改 web.config 文件中的 URL。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="YourServiceSoap" />
</basicHttpBinding>
</bindings>
<client>
**** CHANGE THE LINE BELOW TO CHANGE THE URL ****
<endpoint address="http://10.10.10.100:8080/services/YourService.asmx"
binding="basicHttpBinding" bindingConfiguration="YourServiceSoap"
contract="YourServiceRef.YourServiceSoap" name="YourServiceSoap" />
</client>