根据配置更新服务参考地址?

发布于 2024-11-09 12:47:30 字数 363 浏览 5 评论 0原文

在调试过程中,我添加了一堆指向调试计算机上的服务的服务引用。有没有办法根据配置自动重新生成服务引用?当我准备好发布时,我真的不想将它们全部指向发布服务器,然后当我需要调试时返回并再次更改它们,等等。

基本上,我想要以下(完成)自动):

During debugging I added a bunch of service references pointing to services on the Debug machine. Is there any way to automatically regenerate the service references based upon the Configuration? I'd really rather not have to go through and point them all to the Release server when I'm ready to release, then when I need to debug go back and change them all again, etc.

Basicaly, I want the following (done automatically):

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2024-11-16 12:47:30

无法对配置进行条件编译。我在某些项目中使用的一件事是在代码中使用 #if 语句,用于更新配置中的服务引用。类似于下面的代码:

static void Main() {
    TestClient client = new TestClient();
    UpdateAddress(client.Endpoint);
}
static void UpdateAddress(ServiceEndpoint endpoint) {
    string address = endpoint.Address.Uri.ToString();
    int svcIndex = address.IndexOf(".svc");
    int serviceIndex = address.LastIndexOf("/", svcIndex);
    address = address.Substring(serviceIndex);
#if DEBUG
    address = "http://localhost/App" + address;
#else
    address = "http://myserver" + address;
#endif
    endpoint.Address = new EndpointAddress(address);
}

另一件我没有做过但我认为可能的事情是查看 msbuild 目标。 IIRC 您可以从 msbuild 执行任意命令,因此您可以根据构建配置使用自定义目标,并运行一些命令来根据该命令更新您的配置文件。

There's no way to do a conditional compilation for configuration. One thing I've used in some projects was to have #if statements in the code which updates the service reference from the config. Something similar to the code below:

static void Main() {
    TestClient client = new TestClient();
    UpdateAddress(client.Endpoint);
}
static void UpdateAddress(ServiceEndpoint endpoint) {
    string address = endpoint.Address.Uri.ToString();
    int svcIndex = address.IndexOf(".svc");
    int serviceIndex = address.LastIndexOf("/", svcIndex);
    address = address.Substring(serviceIndex);
#if DEBUG
    address = "http://localhost/App" + address;
#else
    address = "http://myserver" + address;
#endif
    endpoint.Address = new EndpointAddress(address);
}

Another thing, which I haven't done, but I think may be possible, is to look at the msbuild targets. IIRC you can execute arbitrary commands from msbuild, so you could use a custom target depending on the build configuration, and run some command which would update your config file based on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文