是由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。
我不知道你有多少進步以下這種方法,但它應該很簡單明瞭:
創建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
}
}
擴展類項目的YourProjectName.Properties.Settings
部分類的定義和裝飾SettingsProviderAttribute
:
[System.Configuration.SettingsProvider(typeof(MySettings.Providers.MySettingsProvider))]
internal sealed partial class Settings
{
//
}
在重寫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;
}
正如你可以在代碼中看到,爲了做到這一點,你需要要知道設置名稱,因爲它是在app.config
,當你已經添加了參考Web服務(ConsoleApplication1_net_webservicex_www_BarCode
)的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>
這是一個非常簡單的例子,但是您可能會使用更復雜的對象來存儲配置信息,以及上下文中可用的其他屬性,例如item.Attributes
或context
以獲得正確的配置值。
看來您需要一個[Settings Provider](http://msdn.microsoft.com/en-us/library/system.configuration.settingsprovider.aspx),它可以使用數據庫來存儲設置。 –
@John,看起來確實如此。我怎麼能告訴VS在生成的Web服務客戶端代碼中使用我的SettingsProvider? –
@PéterTörök也許這些鏈接有助於http://www.codeproject.com/Articles/20917/Creating-a-Custom-Settings-Provider和http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx和http://www.codeproject.com/Articles/136152/Create-a-Custom-Settings-Provider-to-Share-Setting – Yahia