Nếu bạn muốn giữ mọi thứ trong một tệp cấu hình, bạn có thể giới thiệu phần cấu hình tùy chỉnh cho app.settings của bạn để lưu trữ các thuộc tính cho chế độ gỡ rối và phát hành.
Bạn có thể lưu giữ đối tượng trong ứng dụng lưu trữ cài đặt cụ thể của chế độ nhà phát triển hoặc ghi đè ứng dụng hiện có dựa trên công tắc gỡ lỗi.
Đây là một ví dụ ngắn gọn giao diện điều khiển ứng dụng (DevModeDependencyTest):
App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="DevModeSettings">
<section name="debug" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" />
<section name="release" type="DevModeDependencyTest.DevModeSetting,DevModeDependencyTest" allowLocation="true" allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
<DevModeSettings>
<debug webServiceUrl="http://myDebuggableWebService.MyURL.com" />
<release webServiceUrl="http://myWebservice.MyURL.com" />
</DevModeSettings>
<appSettings>
<add key="webServiceUrl" value="http://myWebservice.MyURL.com" />
</appSettings>
</configuration>
Đối tượng để lưu trữ cấu hình tùy chỉnh của bạn (DevModeSettings.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace DevModeDependencyTest
{
public class DevModeSetting : ConfigurationSection
{
public override bool IsReadOnly()
{
return false;
}
[ConfigurationProperty("webServiceUrl", IsRequired = false)]
public string WebServiceUrl
{
get
{
return (string)this["webServiceUrl"];
}
set
{
this["webServiceUrl"] = value;
}
}
}
}
Trình xử lý để truy cập cài đặt cấu hình tùy chỉnh của bạn (DevModeSettingsHandler.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace DevModeDependencyTest
{
public class DevModeSettingsHandler
{
public static DevModeSetting GetDevModeSetting()
{
return GetDevModeSetting("debug");
}
public static DevModeSetting GetDevModeSetting(string devMode)
{
string section = "DevModeSettings/" + devMode;
ConfigurationManager.RefreshSection(section); // This must be done to flush out previous overrides
DevModeSetting config = (DevModeSetting)ConfigurationManager.GetSection(section);
if (config != null)
{
// Perform validation etc...
}
else
{
throw new ConfigurationErrorsException("oops!");
}
return config;
}
}
}
Và cuối cùng điểm mấu chốt của bạn để ứng dụng giao diện điều khiển (DevModeDependencyTest.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace DevModeDependencyTest
{
class DevModeDependencyTest
{
static void Main(string[] args)
{
DevModeSetting devMode = new DevModeSetting();
#if (DEBUG)
devMode = DevModeSettingsHandler.GetDevModeSetting("debug");
ConfigurationManager.AppSettings["webServiceUrl"] = devMode.WebServiceUrl;
#endif
Console.WriteLine(ConfigurationManager.AppSettings["webServiceUrl"]);
Console.ReadLine();
}
}
}
Cảm ơn bạn hhravn! –