michael
michael

Reputation: 15282

Update Service Reference Address based on Configuration?

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):

Upvotes: 3

Views: 1713

Answers (2)

carlosfigueira
carlosfigueira

Reputation: 87228

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.

Upvotes: 3

Related Questions