简体   繁体   English

如何从远程源配置ASMX Web服务URL

[英]How to configure ASMX web service URL from remote source

We are working on a legacy C# enterprise app. 我们正在开发旧的C#企业应用程序。 Its client uses several web services, whose URLs, among lots of other settings, are read from the local app.config file. 它的客户端使用几个Web服务,这些URL的URL以及许多其他设置都是从​​本地app.config文件中读取的。 We want to migrate these settings into a global DB to simplify their management. 我们希望将这些设置迁移到全局数据库中,以简化其管理。 However, I can't figure out how (and whether) it is possible to migrate the web service URLs. 但是,我无法弄清楚如何(以及是否)可以迁移Web服务URL。 These are read from the service client code generated by VS and I can't seem to find a way to tell VS to use a different settings provider than the one generated into Settings.Designer.cs . 这些是从VS生成的服务客户端代码中读取的,我似乎找不到一种方法来告诉VS使用与Settings.Designer.cs生成的设置提供程序不同的设置提供程序。

We can overwrite the service facade's Url property with the value we want, after it is created - this is the solution currently used in several places in the code. 我们可以覆盖服务门面的Url与我们想要的值属性,在创建 -这是目前在代码的几个地方使用的解决方案。 However, I wouldn't like to touch every part of our codebase where any of these services is used (now and in the future). 但是,我不想触及使用这些服务中的任何一个的代码库的每个部分(现在和将来)。 Even less would I like to modify generated code. 我什至不希望修改生成的代码。

There has to be a better, cleaner, safer solution - or is there? 必须有一个更好,更清洁,更安全的解决方案-还是有?

Btw our app runs on .NET 2.0 and we won't migrate to newer versions of the platform in the foreseeable future. 顺便说一句,我们的应用程序在.NET 2.0上运行,在可预见的将来,我们不会迁移到该平台的较新版本。

The Refernce.cs file that is generated by the Visual Studio indicates that the URL of the webservice will be retrieved from the settings: Visual Studio生成的Refernce.cs文件指示将从设置中检索Web服务的URL:

this.Url = global::ConsoleApplication1.Properties.
    Settings.Default.ConsoleApplication1_net_webservicex_www_BarCode;

I believe that John Saunders gave you a wonderful suggestion in his comment. 我相信约翰·桑德斯John Saunders )在他的评论中给了您一个很好的建议。 You need a SettingsProvider class which: 您需要一个SettingsProvider该类

...defines the mechanism for storing configuration data used in the application settings architecture. ...定义用于存储在应用程序设置体系结构中使用的配置数据的机制。 The .NET Framework contains a single default settings provider, LocalFileSettingsProvider, which stores configuration data to the local file system. .NET Framework包含一个默认设置提供程序LocalFileSettingsProvider,它将配置数据存储到本地文件系统。 However, you can create alternate storage mechanisms by deriving from the abstract SettingsProvider class. 但是,您可以通过派生抽象的SettingsProvider类来创建备用存储机制。 The provider that a wrapper class uses is determined by decorating the wrapper class with the SettingsProviderAttribute. 包装器类使用的提供程序是通过用SettingsProviderAttribute装饰包装器类来确定的。 If this attribute is not provided, the default, LocalFileSettingsProvider, is used. 如果未提供此属性,则使用默认值LocalFileSettingsProvider。

I don't know how much you have progressed following this approach, but it should go pretty straighforward: 我不知道您采用这种方法取得了多少进展,但是应该很简单:

  1. Create the SettingsProvider class: 创建SettingsProvider类:

     namespace MySettings.Providers { Dictionary<string, object> _mySettings; class MySettingsProvider : SettingsProvider { // Implement the constructor, override Name, Initialize, // ApplicationName, SetPropertyValues and GetPropertyValues (see step 3 below) // // In the constructor, you probably might want to initialize the _mySettings // dictionary and load the custom configuration into it. // Probably you don't want make calls to the database each time // you want to read a setting's value } } 
  2. Extend the class definition for the project's YourProjectName.Properties.Settings partial class and decorate it with the SettingsProviderAttribute : 扩展项目的YourProjectName.Properties.Settings类的类定义,并使用SettingsProviderAttribute装饰它:

     [System.Configuration.SettingsProvider(typeof(MySettings.Providers.MySettingsProvider))] internal sealed partial class Settings { // } 
  3. In the overridden GetPropertyValues method, you have to get the mapped value from the _mySettings dictionary: 在重写的GetPropertyValues方法中,您必须从_mySettings字典中获取映射的值:

     public override SettingsPropertyValueCollection GetPropertyValues( SettingsContext context, SettingsPropertyCollection collection) { var spvc = new SettingsPropertyValueCollection(); foreach (SettingsProperty item in collection) { var sp = new SettingsProperty(item); var spv = new SettingsPropertyValue(item); spv.SerializedValue = _mySettings[item.Name]; spv.PropertyValue = _mySettings[item.Name]; spvc.Add(spv); } return spvc; } 

As you can see in the code, in order to do that, you need to know the setting name as it was added in the app.config and the Settings.settings when you have added the reference to the web service ( ConsoleApplication1_net_webservicex_www_BarCode ): 正如您在代码中看到的那样,为了做到这一点,您需要知道在添加对Web服务的引用( ConsoleApplication1_net_webservicex_www_BarCode )时在app.config中添加的设置名称和Settings.settings

<applicationSettings>
    <ConsoleApplication30.Properties.Settings>
        <setting name="ConsoleApplication1_net_webservicex_www_BarCode"
            serializeAs="String">
            <value>http://www.webservicex.net/genericbarcode.asmx</value>
        </setting>
    </ConsoleApplication30.Properties.Settings>
</applicationSettings>

This is a very simple example, but you might use a more complex object to store the configuration information in conjunction with other properties available in the context such as item.Attributes or context in order to get the proper configuration value. 这是一个非常简单的示例,但是您可以使用一个更复杂的对象将配置信息与上下文中可用的其他属性(例如item.Attributescontext ,以获得正确的配置值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM