简体   繁体   English

如何在不重新编译的情况下动态切换 .NET 中的 Web 服务地址?

[英]How can I dynamically switch web service addresses in .NET without a recompile?

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.我有引用 Web 服务的代码,我希望该 Web 服务的地址是动态的(从数据库、配置文件等中读取),以便轻松更改。 One major use of this will be to deploy to multiple environments where machine names and IP addresses are different.它的一个主要用途是部署到机器名称和 IP 地址不同的多个环境中。 The web service signature will be the same across all deployments, just located elsewhere. 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 引用”向导宠坏了——不过,这似乎应该是相对容易的。

When you generate a web reference and click on the web reference in the Solution Explorer.当您生成 Web 参考并单击解决方案资源管理器中的 Web 参考时。 In the properties pane you should see something like this:在属性窗格中,您应该会看到如下内容:

Web 参考属性

Changing the value to dynamic will put an entry in your app.config.将值更改为动态将在您的 app.config 中放置一个条目。

Here is the CodePlex article that has more information.这是包含更多信息的CodePlex 文章

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 文件中的值:

  1. Is a mess;是一个烂摊子;

  2. 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 文件和动态设置。

I know this is an old question, but our solution is much simpler than what I see here.我知道这是一个老问题,但我们的解决方案比我在这里看到的要简单得多。 We use it for WCF calls with VS2010 and up.我们将它用于 VS2010 及更高版本的 WCF 调用。 The string url can come from app settings or another source.字符串 url 可以来自应用程序设置或其他来源。 In my case it is a drop down list where the user picks the server.在我的情况下,它是一个下拉列表,用户可以在其中选择服务器。 TheService was configured through VS add service reference. TheService 是通过 VS 添加服务引用配置的。

private void CallTheService( string url )
{
   TheService.TheServiceClient client = new TheService.TheServiceClient();
   client.Endpoint.Address = new System.ServiceModel.EndpointAddress(url);
   var results = client.AMethodFromTheService();
}

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.能够在运行时更改 Web 服务 URL 的关键是覆盖构造函数,我使用部分类声明完成了该构造。 The above, setting the URL behavior to Dynamic must also be done.以上,将 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.这基本上创建了一个 web 服务包装器,如果您必须在某个时候重新加载 web 服务,通过添加服务引用,您不会丢失您的工作。 The Microsoft help for Partial classes specially states that part of the reason for this construct is to create web service wrappers. Microsoft 对 Partial 类的帮助特别指出,这种构造的部分原因是为了创建 Web 服务包装器。 http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.100).aspx 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;
            }
        }
    }
}

将 URL 行为更改为“动态”。

As long as the web service methods and underlying exposed classes do not change, it's fairly trivial.只要 Web 服务方法和底层公开的类不改变,它就相当简单。 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.对于 Visual Studio 2005(和更新版本),添加 Web 引用会创建一个具有此 URL 的 app.config(或 web.config,用于 Web 应用程序)部分。 All you have to do is edit the app.config file to reflect the desired URL.您所要做的就是编辑 app.config 文件以反映所需的 URL。

In our project, our simple approach was to just have the app.config entries commented per environment type (development, testing, production).在我们的项目中,我们的简单方法是根据环境类型(开发、测试、生产)对 app.config 条目进行注释。 So we just uncomment the entry for the desired environment type.因此,我们只需取消注释所需环境类型的条目。 No special coding needed there.那里不需要特殊的编码。

Just a note about difference beetween static and dynamic.只是关于静态和动态之间差异的说明。

  • Static : you must set URL property every time you call web service.静态:每次调用 Web 服务时都必须设置 URL 属性。 This because base URL if web service is in the proxy class constructor.这是因为如果 Web 服务位于代理类构造函数中,则基 URL。
  • Dynamic : a special configuration key will be created for you in your web.config file.动态:将在您的web.config文件中为您创建一个特殊的配置密钥。 By default proxy class will read URL from this key.默认情况下,代理类将从该键读取 URL。

If you are fetching the URL from a database you can manually assign it to the web service proxy class URL property.如果您从数据库中获取 URL,您可以手动将其分配给 Web 服务代理类 URL 属性。 This should be done before calling the web method.这应该在调用 Web 方法之前完成。

If you would like to use the config file, you can set the proxy classes URL behavior to dynamic.如果您想使用配置文件,您可以将代理类 URL 行为设置为动态。

Definitely using the Url property is the way to go.绝对使用 Url 属性是要走的路。 Whether to set it in the app.config, the database, or a third location sort of depends on your configuration needs.是在 app.config、数据库还是第三个位置中设置它取决于您的配置需求。 Sometimes you don't want the app to restart when you change the web service location.有时您不希望应用程序在更改 Web 服务位置时重新启动。 You might not have a load balancer scaling the backend.您可能没有扩展后端的负载均衡器。 You might be hot-patching a web service bug.您可能正在修补 Web 服务错误。 Your implementation might have security configuration issues as well.您的实现也可能存在安全配置问题。 Whether it's production db usernames and passwords or even the ws security auth info.无论是生产数据库用户名和密码,还是 ws 安全身份验证信息。 The proper separation of duties can get you into some more involved configuration setups.适当的职责分离可以让您进入一些更复杂的配置设置。

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 属性。

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
      }

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>

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

相关问题 如何动态设置使用SSL的.Net 3.5 Web服务? - How can I dynamically set a .Net 3.5 Web Service that uses SSL? 我可以拆分.net DLL而不必重新编译引用原始DLL的其他DLL吗? - Can I split a .net DLL without having to recompile other DLLs that reference the original one? 如何在 .NET 5.0 中添加 soap web 服务作为参考 - How can I add soap web service as reference in .NET 5.0 如何将.NET Web服务转换为基于Spring WS的Web服务? - How can I convert a .NET web service to Spring WS based web service? 如何在Web Service / Web API中的XML和SQL存储库之间动态切换? - How to Switch Dynamically between XML and SQL Repository in Web Service/ Web API? 我可以在没有Visual Studio .net的情况下用C#创建Web服务吗 - Can I create web-service in C# without Visual Studio .net 在不更新服务引用的情况下,两个Web服务需要什么样的相似性才能使端点地址互换? - How similar do two web services need to be for the endpoint addresses to be interchangeable without updating the service reference? 我怎样才能避免开关(.net) - How can I avoid the switch (.net) 如何动态调用net.tcp Web服务 - How to dynamically call a net.tcp Web Service 如何在不添加Web引用的情况下调用SOAP Web服务 - How can I call a SOAP Web Service without Adding Web Reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM