Web.config with XDT transform to do partial replace
Asked Answered
M

1

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?

Malaria answered 9/8, 2013 at 16:11 Comment(4)
Just curious what do you gain by replacing a substring of the url instead of entire attribute?Inglenook
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.Malaria
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).Inglenook
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.Malaria
G
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.

Gine answered 9/8, 2013 at 16:41 Comment(5)
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.Malaria
Can you please show an example with multiple endpoints and the Locator attribute? Nm, found the answer here: #4637607Jazmin
This does not answer the question at all. This is not a substring replacement.Brandon
This does not answer the question asked which was to replace only part of the string, not the entire string.Orfield
This does not answer the question.Chrysanthemum

© 2022 - 2024 — McMap. All rights reserved.