我们确实尝试了 wal 的方法,该方法几乎解决了问题,但我们遇到了单击一次部署的问题,请参阅我对 wal 答案的评论。
我们目前使用的解决方案是本文建议的条件编译:
http://www.codeproject.com/Articles/451734/Visual-Studio-Use-Conditional-Compilation-to-Contr
优点是,除了单击一次可以完美地工作之外,您不必调整 VS 项目文件或使用任何第三方组件。缺点是如果要添加/更改端点,则需要更新源代码。
我们所做的是在项目中添加一个新的.settings文件。这不是强制性的,但我们认为将端点配置保存在单独的设置文件中是个好主意,因为必须稍微调整该文件。根据为编译启用的配置,它被调整为使用条件编译来启用正确的端点:
public ServiceSettings() {
// // To add event handlers for saving and changing settings, uncomment the lines below:
//
// this.SettingChanging += this.SettingChangingEventHandler;
//
// this.SettingsSaving += this.SettingsSavingEventHandler;
//
// Each method corrsponds to a build version. We call all four methods, because
// the conditional compilation will only compile the one indicated:
this.SetLocalApplicationSettings();
this.SetAS12ApplicationSettings();
}
[Conditional("LOCAL")]
private void SetLocalApplicationSettings()
{
this["LoginAddress"] = "https://localhost/services/loginservice";
this["SettingsAddress"] = "https://localhost/services/settingsservice";
}
[Conditional("EXAMPLE")]
private void SetAS12ApplicationSettings()
{
this["LoginAddress"] = "https://example.com/services/loginservice";
this["SettingsAddress"] = "https://example.com/services/settingsservice";
}
在 VS 中,我们为每个端点创建了一个配置,并在 Build 选项卡上定义了适当的条件编译符号,即 LOCAL 或EXAMPLE。
我们还使用 VS 生成的 WS 客户端类更新了代码,以使用设置文件中定义的端点:
var client = new SettingsServiceClient("SettingsServiceImplPort",
ServiceSettings.Default.SettingsAddress);
在 app.config 中,我们只保留了默认配置(localhost)和绑定配置以使 VS 满意:
<system.serviceModel>
<bindings>
<customBinding>
<binding name="SettingsServiceImplServiceSoapBinding">
<textMessageEncoding messageVersion="Soap12" />
<httpsTransport />
</binding>
<binding name="LoginServiceImplServiceSoapBinding">
<textMessageEncoding messageVersion="Soap12" />
<httpsTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://localhost/services/settingsservice"
binding="customBinding" bindingConfiguration="SettingsServiceImplServiceSoapBinding"
contract="SettingsServiceReference.SettingsService" name="SettingsServiceImplPort" />
<endpoint address="https://localhost/services/loginservice"
binding="customBinding" bindingConfiguration="LoginServiceImplServiceSoapBinding"
contract="LoginServiceReference.LoginService" name="LoginServiceImplPort" />
</client>
</system.serviceModel>
<applicationSettings>
<ConfigurationTest.ServiceSettings>
<setting name="SettingsAddress" serializeAs="String">
<value>https://localhost/services/settingsservice</value>
</setting>
<setting name="LoginAddress" serializeAs="String">
<value>https://localhost/services/loginservice</value>
</setting>
</ConfigurationTest.ServiceSettings>
</applicationSettings>