26

I am in a situation where I just want to update a part of a the URL of a WCF endpoint. Right now we do this by including different configs with all the endpoints per 'variety'. This is tedious to manage. I would like to setup a transform in the web.config to do so.

These are two examples of the files

Dev

  <endpoint address="http://servicesdev.host.com/RPUtilityServices/LogException.svc/restService"
        behaviorConfiguration="restfulBehavior"
        binding="webHttpBinding"
        contract="Host.RP.Shared.Common.Services.Utility.Interfaces.IExceptionUtilityService"
        name="LogService" />

and some more of these

Staging

  <endpoint address="http://servicessta.host.com/RPUtilityServices/LogException.svc/restService"
            behaviorConfiguration="restfulBehavior"
            binding="webHttpBinding"
            contract="Host.RP.Shared.Common.Services.Utility.Interfaces.IExceptionUtilityService"
            name="LogService" />

The difference is the servicessta versus servicesdev. Now I also have servicesuat and a servicesqa etcera. I would like to setup a transform to just replace the 'dev' with 'sta' etc and not the entire block (using xdt:Transform="Replace")

But how do I do that?

ranieuwe
  • 2,268
  • 1
  • 24
  • 30
  • Just curious what do you gain by replacing a substring of the url instead of entire attribute? – Mike Cheel Aug 12 '13 at 15:28
  • I have a few more blocks like that, restService, the soap one etc. What I would gain is that I need to update just one file to replace the service address instead of all other files. We have 5 environments, so the replaces make the management easier. – ranieuwe Aug 13 '13 at 11:29
  • 2
    I think I misunderstood your post. I thought you wanted to run some sort of substring on the attribute when you really just wanted to change just the attribute (at least that is what I am getting from the answer you marked as correct). – Mike Cheel Aug 13 '13 at 12:56
  • 2
    No, you are right in that I wanted just the substring replacement. It is just impossible so I accepted the answer as correct as it is the next best alternative. – ranieuwe Aug 13 '13 at 14:22

1 Answers1

39

The first piece of code above (for dev environment) can go to Web.config (or Web.debug.config but have to add xdt transform as well). In your Web.release.config (this one will go to staging environment) define the following element.

<endpoint address="http://servicessta.host.com/RPUtilityServices/LogException.svc/restService"
        behaviorConfiguration="restfulBehavior"
        binding="webHttpBinding" 
        contract="Host.RP.Shared.Common.Services.Utility.Interfaces.IExceptionUtilityService"
        name="LogService" xdt:Transform="Replace" />

Note that I added xdt:Transform="Replace" in the release config file. With this attribute present the settings defined within the endpoint element will replace those in your base Web.config file.

For more information see MSDN.

UPDATE:

Using the xdt:Transform="Replace" would replace the entire <endpoint /> element. To selectively replace the address attribute of the <endpoint /> element use the following transform.

<endpoint address="http://servicessta.host.com/RPUtilityServices/LogException.svc/restService"
 xdt:Transform="SetAttributes(address)"/>

(Note that if there are several <endpoint /> elements you might want to use the Locator attribute as well.)

What I said is described in detail on the MSDN page I posted above.

sakura-bloom
  • 4,524
  • 7
  • 46
  • 61
  • 4
    I am familiar with how to do a replace on a node (with xdt:Transform="Replace"). I want to only replace a part of the string so I do not have that redefinition everywhere. – ranieuwe Aug 12 '13 at 08:24
  • 1
    Can you please show an example with multiple endpoints and the `Locator` attribute? Nm, found the answer here: http://stackoverflow.com/questions/4637107/web-config-transformation-how-to-apply-a-transformation-to-all-node-matching-a – Serj Sagan Oct 29 '15 at 20:31
  • 2
    This does not answer the question at all. This is not a substring replacement. – Soeren L. Nielsen Jul 13 '17 at 14:25
  • This does not answer the question asked which was to replace only part of the string, not the entire string. – Holf May 22 '20 at 10:41
  • This does not answer the question. – edgar_is Sep 08 '20 at 23:42