Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing web.config transform when no value exists for a transform

We had a bit of an incident today which has got me thinking. We have a project with a pretty standard web.config transform setup for our various configs. There is a section which controls access to our DAO services which looks like this:

<endpoint address="http://myserver/myservice1.svc/basicHttp"
binding="basicHttpBinding" contract="MyAssembly.IItem" name="DataAccessEndPoint"
kind="" endpointConfiguration="" />
<endpoint address="http://myserver/myservice2.svc/basicHttp"
binding="basicHttpBinding" contract="MyAssembly.IItem2" name="LoggingEndPoint"
kind="" endpointConfiguration="" />

And a transform like this:

<endpoint address="http://mytestserver/myservice1.svc" name="DaoEndPoint" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"  />
<endpoint address="http://mytestserver/myservice2.svc" name="LoggingEndPoint" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"  />

Hopefully you will have spotted the error here - the name for the DaoEndPoint doesn't match. Unfortunately the dev who created it didn't, and had also been locally debugging against the live services, which caused the test deployment to, yup, point to live. We luckily picked it up pretty quickly, but I'm sure you can see the potential for extreme pain here!

I got thinking about your intent when creating transform files and it seems to me that if you put in a transform that you intend to transform something. So it would be nice if the transform (and therefore the deploy) failed if there was a DaoEndPoint transform but no matching DaoEndPoint item in the main .config file.

So I'm sort of canvasing for people's opinions, is this something that would be useful? Is it plain overkill? Am I totally missing the point?

Also, is there anything out there that does this? I'm happy to dig about and develop a solution, but I'd be happier if someone had done the legwork for me ;)

like image 218
Chris Surfleet Avatar asked Mar 15 '12 16:03

Chris Surfleet


People also ask

How do I enable 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).

What is Web config transform?

A Web. 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.

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.

What is Xdt transformation?

XDT is a simple and straight forward method of transforming the web. config during publishing/packaging. Transformation actions are specified using XML attributes defined in the XML-Document-Transform namespace, that is mapped to the xdt prefix.


1 Answers

See Sayed Ibrahim Hashimi's great answer to this question, which involves creating a custom class that inherits from Microsoft.Web.Publishing.Tasks.Transform. You could use the same technique but inherit from the Locator class instead, and then throw an exception when you are unable to match a target node.

I actually tested this myself and was able to have the exception thrown during publishing. However, my custom locator class (MyMatch) didn't actually do anything besides throw the exception. It might be a fair bit of work to override methods to mimic the Match class (which you can't inherit from) and then figure out the appropriate place to do a final check for a failure to match.

Anyway, I definitely think it would be useful to at least have an option that you could set where publishing would fail or give you a warning when your transform had no effect.

like image 128
regularmike Avatar answered Oct 03 '22 14:10

regularmike