Using transformed Web.config with IIS Express during debug - visual-studio

I have a Visual Studio application that has multiple Solution Configurations. There is a Web.config transform file for each configuration. For example, Web.Debug.config, Web.Release.config, etc.
We also have a couple of developers working on this project that have nonstandard SQL Express instance names due to the way they installed SQL Express and rather than having them continually editing Web.Debug.config to run in their environment I have setup a Solution Configuration for each of them and added the following to the very bottom of the .csproj file. This code does work in that it triggers the creation of Web.config and MyWebApp.dll.config in the VS /obj/Debug-DeveloperName/ folder.
The transformed .config files are perfect, but IIS Express still uses the root Web.config (not transformed).
Is there a way to get IIS Express to use these transformed Web.config files while debugging locally?
<UsingTask
TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target
Name="AfterCompile"
Condition="exists('Web.$(Configuration).config')">
<!-- Generate transformed config in intermediate directory -->
<TransformXml
Source="Web.config"
Destination="$(IntermediateOutputPath)$(TargetFileName).config"
Transform="Web.$(Configuration).config"
/>
</Target>
Using the web application's Web.Debug.Config works for most of us, but not all.
There must be a way of getting IIS Express to use the transformed Web.Debug-DeveloperName.config during local debug?
Does the transformed Web.config have to be copied into a different folder?

I faced this problem before and I found a solution. Unfortunately, the solution is not based on forcing IIS to use different name of the config, but if you follow steps below, you will just select the configuration and run you app (which is ewhat you need I think). The Web.config transform will occur before build and it will replace the original Web.config by the transformad one. Then, when the deployment (to local IIS Express) begins, it will already use the transformed one.
Here is step by step how I did this in one project (VS2012):
Right click on the project and select Unload
Right click on it again and select Edit
Go to the bottom of the file and append the follwing to the right over the "</Project>" tag (it will be last item)
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">
<TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>
The condition there is to prevent duplicate transformation when publishing.
Save the file, right click on it and select Reload
Now, everytime you run a build, your Web.config will be transformed according to selected configuration.

Related

Where are ASP.NET temporary files stored in Kestrel?

On our dot net core project we are using ViewComponents.
When we change the View referenced by one of our ViewComponents, when "dotnet run watch" is running the end result does not update until we restart dotnet. Creating a new view with the same code which our view component uses, causes it to update and works. This is very frustrating when trying to debug an error only to remember you need to restart dotnet
Can someone please tell me how we can force recompile or ignore certain directories from this cache?
This is happening on .netcoreapp 1.1 on OSX
dotnet-watch keeps an in-memory list of files that it is watching. You can inspect this list by running dotnet watch --list.
By default, dotnet-watch only restarts the process when Compile and EmbeddedResource items change. This normally only includes *.cs and *.resx files, but may include other files depending on your project.
To exclude a folder, you can set "Watch=false" on these items.
<ItemGroup>
<Compile Update="ignored_dir\**\*.cs" Watch="false" />
</ItemGroup>
You can explicitly add new filetypes to watch by adding <Watch> items to your *.csproj file. Examples:
<ItemGroup>
<Watch Include="*.js" />
<Watch Include="somefile.txt" />
<Watch Include="subdir\**\*" />
</ItemGroup>
Just for clarity's sake, dotnet-watch is independent of Kestrel.
For more details, see https://github.com/aspnet/DotNetTools/tree/1.0.1/src/Microsoft.DotNet.Watcher.Tools#msbuild

Modifying web.config for development and production environments

I have a webApi project and after deployment to Dev environment I need to edit web.config and change connection to database from production configuration to development configuration and back if I need deployment to prod.
How can I automatically set webconfig for selected dev or prod in the release or debug I use in my project?
To specify the changes that you want to make in Web.config files, you use transform files. A transform file is associated with a build configuration.
If you want to create a transform file for a custom build configuration that does not exist, create the build configuration first by using Configuration Manager.
You can open Configuration Manager by selecting it from the Build menu.
In Solution Explorer, expand the application Web.config file.
If any transform files have already been created, the Web.config file is displayed in Solution Explorer with a symbol indicating that it can be expanded, and the transform files are shown when you expand the Web.config file.
The build configuration that a transform is for is indicated by a string in the file name. For example, a transform file for the Debug build configuration is named Web.Debug.config.
If no transform file exists for the build configuration that you want to specify settings for, in Solution Explorer, right-click the Web.config file and then click Add Config Transforms.
Open the transform file for the build configuration that you want to work with.
Edit the transform file to specify the changes that should be made to the deployed Web.config file when you deploy by using that build configuration.
The default transform file includes comments that show how to code some common transforms.
The following example shows how to use the Match locator and the SetAttributes transform attribute. The Match locator attribute identifies the add element in the connectionStrings section as the element to change. The SetAttributes transform attribute specifies that this element's connectionString attribute should be changed to "ReleaseSQLServer".
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MyDB"
connectionString="ReleaseSQLServer"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
Save and close the transform file.
When you deploy the Web application by using the selected build configuration and by using either a deployment package or one-click publish, the Web.config file is transformed according to your specifications.

Visual Studio 2013 Website Project - Change Configuration in Configuration Manager

I have Website Project in Visual Studio 2013. I have two Publishing Profiles, one for staging and the other for production. In Configuration Manager, I can create new Active Solution Configurations but cannot add new Configurations to my drop-down, Debug is the only option.
So now when I attempt to Publish using my production profile, the web.config is getting transformed by both the Web.Debug.Config and my web.Production.Config.
How do I add new Configurations to the dropdown?
How do I add new Configurations to the dropdown?
You can't. At least not for the purpose you need to use these new configurations for.
I'll make the assumption that you really need to know this:
How can I publish a web site without the debug flag being applied to the web.config by the debug transform?
This is already happening, just in a confusing way.
When you publish the site using the Publish Web Site wizard it applies the appropriate transforms to the web.config. In your case, Web.Debug.Config and Web.Production.Config.
And now for the confusing part:
By default, web.debug.config actually removes the debug attribute from the web.config.
<compilation xdt:Transform="RemoveAttributes(debug)" />
This happens for legacy reasons to do with the missing Web Site project file and the underlying build tools.
If you want to stop the debug transformation being applied and you have no custom configuration in it, you can delete web.debug.config. Just be sure to add
<compilation xdt:Transform="RemoveAttributes(debug)" />
inside the <system.web> element of each of your environment specific transforms (web.Production.Config, etc).
My recommendation is:
Leave web.debug.config untouched. In its default state.
Create a publish profile for each environment.
Add a transform for each publish profile (web.<configuration>.config)
Put any environment specific configuration into the appropriate transform file.
To get my publish settings working the way I wanted I had to:
Return web.debug.config back to it's default contents (I had some transforms in here that I was using for my staging environment)
For every publish profile in /App_Data/PublishProfiles/, right click and select Add Config Transform to create a new config file at the root of the site (note that it will not be nested under the default web.config unless the profile is titled debug or release, kinda odd if you ask me)
Add any relevant transforms to each of these new configs
Since all publish profiles in a website project seem to be stuck on a debug configuration, there was no way for me to avoid web.debug.config transforming my default web.config. With this solution, we simply don't use web.config for publish specific transforms.
I'd like to credit Chris O'Neill for his helpful, if verbose, answer that clued me into this solution.
You can modify/create the configuration of the project FSUResearch manually. The steps are:
In solution explorer:
Right click on the project -> Unload project -> Right click on FSUResearch (unavailable) -> Click on Edit FSUResearch.csproj
In this file you will see many 'PropertyGroup' tags. These PropertyGroups consists of different combinations of configuration and platform. So create a new PropertyGroup and give your own project configurations. Make sure to give the project configuration name that is already a solution configuration name (Understand build configurations).
For example:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Production|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Save the changes and then right click on the .csproj file again and reload the project.

Unwanted changes in .csproj file on build

I'm trying to auto-detect web.configs as part of a transform pre-build event in one of my web application project files, this code goes up one folder level from my project file and gets all web.configs in every directory and sub directory:
<ItemGroup>
<WebConfigsRelativePath Include ="..\**\Web.config"/>
</ItemGroup>
This works great but everytime I build and exit Visual Studio, I get a prompt asking me if I want to save changes made to my solution file. If I select yes, and open the project file, the above code is changed to the locations of each web.config
<ItemGroup>
<WebConfigsRelativePath Include="..\Web\Decade\Web.config" />
<WebConfigsRelativePath Include="..\Web\Matrix\RiskAnalysis\Web.config" />
<WebConfigsRelativePath Include="..\Web\Service\Web.config" />
<WebConfigsRelativePath Include="..\Web\Web.config" />
</ItemGroup>
This would be fine but the whole reason I'm auto-detecting web.configs pre-build is so I can add and remove web.configs as I please without having to hardcode their locations, and everytime I exit VS, the locations will be hardcoded in the project file....
Does anyone know why this ItemGroup changes every time I exit Visual Studio?
If I take an existing web project but use the <Content /> rather than the custom <WebConfigsRelativePath /> in your sample, then I see the expected behavior.
Try using this:
<Content Include="..\**\Web.config">
<SubType>Designer</SubType>
</Content>
Edit:
If you have special handling for the WebConfigsRelativePath item group, post that in an update to your question.
While I can't explain why VS decides to output a list of files retrieved by the wildcard each time my solution is built, I can show you how I got around this issue:
<PropertyGroup>
<WebConfigsSearchString>..\**\Web.config</WebConfigsSearchString>
</PropertyGroup>
<ItemGroup>
<WebConfigsRelativePath Include ="$(WebConfigsSearchString)"/>
</ItemGroup>
By defining the search string in a property (which always stays static) and referencing the property in the item group's list of files to include, the item group code is never modified but the web.config search is carried out each time a build is run

Windows Azure - Virtual Application

I have one web application and virtual application in Azure. They are separate projects in VS2010 and I have added them to one solution with one Azure deployment project.
I have created multiple web.configs to control parameters, ie web.debug.config etc.
When I publish to azure I specify debug which works for the main application but the virtual application is pointing to the web.config not the variants. How do I correct this please?
Many Thanks,
Steve.
Solution
The solution that worked for me from http://blog.hill-it.be/2011/03/07/no-web-config-transformation-in-local-azure/:
To fix this, unload your Azure project, open the project file for edit and add the tag [under the corresponding PropertyGroup]:
<Project ...>
<PropertyGroup>
...
<packagewebrole>true</packagewebrole>
Workaround
There's also a workaround, it will work for a local development, but not for continuous integration (or at least you will have to find another trick).
In your service definition file add a new Site section:
<Site name="AnotherSite" physicalDirectory="c:\AnotherSite">
<Bindings>
<Binding name="APIEndpoint" endpointName="AnotherSiteEndpoint" />
</Bindings>
</Site>
Add a new endpoint to match the code above (any port works):
<InputEndpoint name="AnotherSiteEndpoint" protocol="http" port="623" />
Go to your web-project --> right-click --> Publish... --> FTP --> Location = "c:\AnotherSite"
Press F5 and go to http://127.0.0.1:623
It should work.
Web application always uses web.config. If you want to use debug / release versions you have to create web.config Transformation. In this case web.config is transformed at build time using selected build target.

Resources