开发者

Update Service Reference Address based on Configuration?

开发者 https://www.devze.com 2023-03-08 18:43 出处:网络
During debugging I added a bunch of service references pointing to services on the Debug开发者_Python百科 machine. Is there any way to automatically regenerate the service references based upon the Co

During debugging I added a bunch of service references pointing to services on the Debug开发者_Python百科 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):

  • Debug -> http://localhost/App/Service1.svc
  • Release -> http://myserver/Service1.svc


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.


You can use web.config transformations to solve this without code.

http://blogs.msdn.com/b/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消