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

时间:2013-02-01 17:17:45

标签: c# web-services url configuration asmx

我们正在开发一个传统的C#企业应用程序。它的客户端使用多个Web服务,其中许多其他设置的URL都是从本地app.config文件中读取的。我们希望将这些设置迁移到全局数据库中以简化其管理。但是,我无法弄清楚如何(以及是否)可以迁移Web服务URL。这些是从VS生成的服务客户端代码中读取的,我似乎找不到告诉VS使用与Settings.Designer.cs生成的提供者不同的设置提供者的方法。

我们可以使用我们想要的值覆盖服务外观的Url属性,创建后 - 这是当前在代码中的几个位置使用的解决方案。但是,我不想触及我们的代码库中使用任何这些服务(现在和将来)的每个部分。我想修改生成的代码更少。

必须有一个更好,更清洁,更安全的解决方案 - 或者在那里?

我们的应用程序运行在.NET 2.0上,在可预见的将来我们不会迁移到该平台的新版本。

1 个答案:

答案 0 :(得分:1)

Visual Studio生成的Refernce.cs文件表示将从设置中检索Web服务的URL:

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

我相信John Saunders在评论中给了你很好的建议。您需要一个SettingsProvider class

  

...定义了存储配置数据的机制   应用设置架构。 .NET Framework包含一个   单个默认设置提供程序,LocalFileSettingsProvider,其中   将配置数据存储到本地文件系统。但是,你可以   通过从抽象派生来创建备用存储机制   SettingsProvider类。包装器类使用的提供者是   通过用。装饰包装类来确定   SettingsProviderAttribute。如果未提供此属性,则   默认使用LocalFileSettingsProvider。

我不知道你采用这种方法取得了多大进展,但它应该非常简单:

  1. 创建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. 扩展项目的YourProjectName.Properties.Settings部分类的类定义,并使用SettingsProviderAttribute装饰它:

    [System.Configuration.SettingsProvider(typeof(MySettings.Providers.MySettingsProvider))]
    internal sealed partial class Settings
    {
        //
    }
    
  3. 在重写的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;
    }
    
  4. 正如您在代码中看到的那样,为了做到这一点,您需要知道在app.configSettings.settings添加引用时添加的设置名称网络服务(ConsoleApplication1_net_webservicex_www_BarCode):

    <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>
    

    这是一个非常简单的示例,但您可以使用更复杂的对象将配置信息与上下文中可用的其他属性(例如item.Attributescontext)一起存储,以便获取正确的配置值。