ASP.NET Web.config transformation won't work in installer - asp.net

I am working on an ASP.NET project in Visual Studio .NET 2010 and attempting to make an MSI installer using a Web Setup Project. I added the Primary output from the project (which seems to pull in the relevant dependencies) and the Content Files from the project (which pulls in the Web.config and the .svc files).
The issue is that rather than applying the XDT transform and creating the Web.config using the Web.Release.config, it just copies the Web.config, the Web.Release.config, and the Web.Debug.config into the installer without doing any transformation at all.
How do I get it to apply the Web.config transformation before creating the installer?

I found a workaround that works for me:
Create needed configurations (Dev,QA,Production etc.) and associated web config transformations.
Use notepad or other text editor and include following in your web application project file (.csproj file) before tag (near the end of the project file):
<Target Name="AfterBuild">
<TransformXml Source="Web.config" Transform="$(ProjectConfigTransformFileName)" Destination="Web.Transformed.config" />
</Target>
Do not include Web.Transformed.config in the web application project - if you do visual studio will alert you about the changes after every build which is pretty annoying.
In the web setup project:
select Content files - > Exclude Filter and add Web.config (and all other Web.*.config files containing transformation rules).
In the web setup project:
select file system editor icon -> web application folder -> Add File
and select Web.Transformed.config in the root of your web application project folder.
In the same screen: right click Web.Transformed.config and rename it to Web.config
Now you are able to generate .msi files with selected configuration and root web.config file is transformed!
Please note that this does not affect web.config files in the sub folders.

The answer ended up being located on another SO post which I missed when I was searching through before asking because it wasn't exactly what I wanted:
MSBuild Script and VS2010 publish apply Web.config Transform
A co-worker suggested using that to place the transformed Web.config in the project's bin directory and a Web Setup Project configured to grab the Web.config out of the bin directory and put it in the installer. This ended up being the most workable solution without installing any add-ons into Visual Studio.

You have to make a deployment project (I think this is a separate download), then your Web Setup project take the precompiled output of the deployment project as it input. One neat thing is that you can have it change a section of your Web.config when it builds.

Related

Web Deploy publishing snk file

I am using Visual Studio 2017 to develop and publish an ASP.NET web app using Web Deploy against IIS. My web app assembly is signed, so my project contains a .snk file in the root folder. When I deploy the web app to the server, the .snk file is sent over as well.
I imagine this is not a good idea, as the .snk contains a private key that must be kept secure. I don't like the idea of having copies of my .snk file scattered over various web servers. As far as I understand, Visual Studio should use this file to sign my assembly when the solution is built, and not deploy it to the server.
Am I correct? If so, how can I stop Visual Studio from deploying this file to the server?
I had a similar issue when publishing in Visual Studio 2019 but found that the SNK file had been added to the web project with a build action of "Content".
Simply editing the SNK file properties and changing the "Build Action" from "Content" to "None" prevented it from including the Strong Name Key file in the published folder.
Unless specified otherwise, an SNK file is not served by IIS as a valid MIME type. When someone types the url to the file in the browser they will get a 404.
If you do not want it send to the server in the first place you can also delete it from Visual Studio Explorer, but still keep the file in the folder (note that VS will delete the file also, so you need to copy it to the folder again manually in Windows Explorer).
A cleaner solution would be to create a wpp.targets file and specify the files/folders for exclusion in the publish.
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/advanced-enterprise-web-deployment/excluding-files-and-folders-from-deployment
In your case the file would look something like this: MyProject.wpp.targets
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ExcludeFromPackageFiles Include="MyFile.snk">
<FromTarget>MyProject.wpp.targets</FromTarget>
</ExcludeFromPackageFiles>
</ItemGroup>
</Project>

Relocating config files of depending assemblies

My application uses NLog as a logging framework hidden behind a facade in MyLogging project. The project is referenced by a number of web sites to not make them directly dependent on NLog. After compilation NLog.config from the logging project ends up in bin folders of each site and NLog manages to find it automatically during startup. That is understandable and pretty much OK. However, editing any file within bin folder forces ASP.NET to restart the working process so I'd like to have NLog.config one folder up beside regular web.config.
Things I'm trying to achieve\avoid:
Keep NLog configuration in a single file `cause it's the same for all of my web sites
Not to embed it into sites web.config (to not trigger ASP.NET auto-restart)
Just moving the file after building projects with msbuild command is not an option because the sites are deployed to Azure (moved files won't be packaged during publishing process)
I'd like to avoid messing with post-deployment scripts and hard coding things like E:\siteroot\0
The issue is not actually NLog specific as I'd have same trouble with standard app.config files. What are my options here?
For ASP.NET applications, it's recommend to set:
"Build Action" to "content". Otherwise it won't be copied to the root folder (folder with web.config)
"copy to Output Directory" to "none". Otherwise it will be copied to your bin folder.
See:

Trimming the output of publishing

I've got a basic ASP.NET Web Application with the following publish settings:
Publish to File System.
Delete all existing files prior to publish - Ticked
Precompile during publishing - Unticked
Exclude files from the App_Data folder - Ticked
While the publish does work as expected, it seems to publish quite a bit of extra baggage.
\bin
Web.config
Web.Debug.config
Web.Release.config
WebServer.dll
\Properties
AssemblyInfo.cs
\PublishProfiles
Release.pubxml
MyWebForm.aspx
MyWebForm.aspx.cs
MyWebForm.aspx.designer.cs
Web.config
Web.Debug.config
Web.Release.config
WebServer.csproj
WebServer.csproj.user
Out of all this, I'm able to remove everything but the following:
\bin
WebServer.dll
MyWebForm.aspx
MyWebForm.aspx.cs
MyWebForm.aspx.designer.cs
Web.config
So, my question is two-fold:
Firstly, why does the publish option, publish various things such as *.csproj files, or the *.config files in the bin directory etc. as the site appears to function perfectly well without this baggage. What is the purpose of these files being made public?
Secondly, is there a way to configure the publish operation to just publish the minimum required files for the project?
I'm not sure as to the reason behind the publish option is publishing the unrequired files, but it seems there is a very easy way to stop it. In the properties of the project under the option "Package/Publish Web" there is a group "Items to deploy", where I am able to select "Only the files required to run the project".

Web Application won't publish without .cs files

I have an asp.net web application project that I am publishing via Build > Publish within visual studio 2013. I am publishing to the file system, using the precompile option selected. My project has "Only files needed to run this application" selected in the Package/Publish Web settings screen. However, regardless of what I do, the .cs files (code-behind) and designer.cs files get copied to the output folder during publishing.
This project was created by using the File > New Project > ASP.NET Web Application functionality in visual studio. Then files from a website project were added to the application, and the "convert to web application" command was run on it from the build menu. The conversion to a web app seems to have worked fine, but I am unable to publish without the .cs files being included.
What else needs to be done in order to get a web application to publish without the code-behind files included? Any ideas on what I can look into?
Could there be a setting at the solution level that is causing this? The new web application was added to a pre-existing solution with about a dozen other projects.
Another thing to note is that when I create a new web app with visual studio in a new project, and publish w/ the same settings, it does NOT include the .cs files.
I've also tried deleting and then creating a new publish profile.
I just fixed this by deleting my old publish profile and creating a new one.
The new one appeared to have all the same settings as the old one but when I published it didn't copy the .cs files.
Edit: This answer is a lie. There was a difference between the two profiles. The new profile was in Release configuration.
Edit 2: There's a setting in the project settings which determines what gets published. Select "only files needed to run this application" from the following page.
Turns out that I added this line to my csproj file earlier when I was trying to get my project to build on our build server. Removing it fixed the problem:
<Target Name="GatherAllFilesToPublish"></Target>

How to replace web.config with transformed for debug file when running WAP in Visual Studio?

I want to replace web.config with transformed for debug config file when running in Visual Studio( I am NOT interesting in publishing project to some output directory). I want to keep original web.config in the root of web application in TFS, but on runtime (on IIS or Cassini) I want to use transformed debug version.
The best approach that I found so far (in Use Visual Studio web.config transform for debugging and ASP.NET Web Projects: web.debug.config & web.release.config ) is to use web.template.config as a master file, and web.config as derived transformed file.
I am not fully happy with it, because developers use to consider web.config as a master file.
I have an idea to output transformed file to ASP.Net temporary files directory, that ASP.NET used for cached files.
Does ASP.NET use web.config file from temporary cache folfder?
Will ASP.NET allow to have web.config file in cache directory different to web.config in original folder?
I found a couple properties, related to ASP.Net temporary files directory.
MSBuild tempDirectory property of the CompilationSection can be used to replace root folder of ASP.Net temporary files.
ClientBuildManager.CodeGenDir Property gives access to specific folder where current cache is located.
However I could not find, how to access path of the folder from MSBuild and will it allow me to output transformed web.config to cached folder.
Any suggestions/considerations will be appreciated.
The debug and release templates of the config file are used when you deploy the application. Can you use the publish option to publish the application to another virtual directory on your machine for debugging purposes? Then, you would have the transformed config file.

Resources