Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify Machine Name in Web.Config Transform

I am using Web.config transforms to successfully create debug and release versions of the my web.config - this is working correctly.

I am interested to know whether there is a 'machine name' property to specify the current machine name which I can use in a debug URL, rather than hard-coding a specific machine name (using localhost isn't an option in the case), e.g.

<add name="XrmService" connectionString="http://$(ComputerName):5555/Service.svc" />

Are there any properties available using Web.config transforms? Similar to MSBuild's $(ComputerName) property?

like image 248
Nick Heppleston Avatar asked Aug 12 '13 15:08

Nick Heppleston


People also ask

How are web configuration transforms generated?

These transformations occur for either of the following web.config generation scenarios: Generated automatically by the Microsoft.NET.Sdk.Web SDK. Build configuration transforms are run first. Include a web. {CONFIGURATION}.config file for each build configuration (Debug|Release) requiring a web.config transformation.

How do I automate the process of changing web config settings?

There are two ways to automate the process of changing Web.config file settings: Web.config transformations and Web Deploy parameters. A Web.config transformation file contains XML markup that specifies how to change the Web.config file when it is deployed.

How to add a config transform to the project file?

On the Web.config, right-click and select "Add Config Transform". This adds web.config transformations to the project file. If inside web.config, you have a value named "ApplicationConnectionString", you can transform the value of this key depends on the environment you need.

How do I create transformation files for custom build configurations?

You can create transformation files for custom build configurations by right-clicking the Web.config file and choosing Add Config Transforms from the context menu. For this tutorial you don't need to do that, and the menu option is disabled, because you haven't created any custom build configurations.


Video Answer


2 Answers

I faced a similar issue, what I ended up doing is :

1) Added the following build target to the project file. (Which is an MSBuild script effectively)

<Target Name="AfterBuild">
     <TransformXml Source="Web.config" Condition="Exists('Web.$(Computername).config') " Transform="Web.$(Computername).config" Destination="Web.config" />
</Target>

2) Added a Web.MyMachineName.config config transform file to the project. In your case it should look like this:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
        <add name="XrmService"
             connectionString="http://MyMachineName:5555/Service.svc"
             xdt:Transform="SetAttributes"
             xdt:Locator="Match(name)"/>
    </connectionStrings>
</configuration>

This has the benefit of running different transformations based on the machine name, without creating a separate build configuration. You can configure it to be debug only by specifying Condition="'$(Configuration)' == 'Debug'".

like image 96
Herr Kater Avatar answered Oct 01 '22 02:10

Herr Kater


There is an Environment Variable that you can use. It is $(COMPUTERNAME).

Open a command window, type "set" (without the double quotes) and press Enter. You will see this Environment Variable somewhere at the top of the screen.

like image 25
TNV Avatar answered Oct 01 '22 01:10

TNV