Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web.config with XDT transform to do partial replace

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?

like image 657
ranieuwe Avatar asked Aug 09 '13 16:08

ranieuwe


People also ask

What is Xdt transform replace?

A Transform attribute on a parent element can affect child elements even if no Transform is specified for them. For example, if you put the attribute xdt:Transform="Replace" in the system. web element, all the elements that are children of the system. web element are replaced with the content from the transform file.

How do I create a new transformation in web config?

If you have a web application project, Right-click on web. config and choose Add Config Transform. This will add any config transforms that are missing from your project based on build configurations (i.e. if you have Production and Staging build configs, both will get a transform added).

How do I rename a web config transform?

If you are using the extension Configuration Transform go to Build > Configuration Manager and find the Configuration dropdown for the project of which you want to change the app config. You can then select the edit and rename the build configurations for that project.

What is config transformation?

config transformation file contains XML markup that specifies how to change the Web. config file when it is deployed. You can specify different changes for specific build configurations and for specific publish profiles.


1 Answers

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.

like image 114
nomad Avatar answered Sep 26 '22 08:09

nomad