By default when publishing a Web project using MSBuild/Visual Studio the config transforms are applied.
I would like to include the config transforms within the output.
Input
web.config
web.Debug.config
web.Release.config
Default Output
web.config
Desired output
web.config
web.Debug.config
web.Release.config
Update the files Build Action to Content using Visual Studio (e.g. right click, properties)
The Publish tasks will still transform the files, so we need to tell MSBuild, that we do not want to transform those files when publishing.
This can be achieved by passing the following parameters into MSBuild:
/p:ProfileTransformWebConfigEnabled=false /p:MarkWebConfigAssistFilesAsExclude=false
If you are working within Visual Studio, you test this behavior by adding these properties to a folder publish profile PublishProfile.xml
<!-- Disable Web.config Transforms -->
<ProfileTransformWebConfigEnabled>false</ProfileTransformWebConfigEnabled>
<MarkWebConfigAssistFilesAsExclude>false</MarkWebConfigAssistFilesAsExclude>
Include web.release.config in Web Deploy output
By default, when publishing a website, VS does not package web.debug.config and web.release.config but only the web.config.
To achieve what you want, you can add a custom target into publishprofile.pubxml to include these extra files.
Please try this:
<Target Name="CustomCollectFiles">
<ItemGroup>
<AdditionFiles Include="xxxxxxxxxxx\Web.Debug.config;xxxxxxxxx\Web.Release.config">
</AdditionFiles>
<FilesForPackagingFromProject Include="%(AdditionFiles.Identity)">
<DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForMsdeployDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
And then you will find these files in the Publish folder when you finishing Publish step.
Hope it could help you.
I was using Azure Dev Ops Server, and I wanted to run the Release transform on build, but also have the possibility to run additional transforms per pipeline target. In my case to change the SessionDb connection string
I added /p:MarkWebConfigAssistFilesAsExclude=false to the build parameters
I set my web.Prod.config to <CopyToOutputDirectory>Always</CopyToOutputDirectory>
I was still getting a NullReference exception when doing the transform. I had to remove
<compilation xdt:Transform="RemoveAttributes(debug)" />
from the prod config transform because that property was removed with the release config transform.
I have a C# web project. I opened the configuration manager, for "Active solution configuration" I chose
In the new window I typed the name (Dev), and didn't copy any settings. I checked off "Create new project configurations".
I right-clicked on my Web.config and chose "Add Config Transform". Web.Dev.config appeared indented under web.config as expected.
My web.config contains erroneous database values:
<connectionStrings>
<add name="PdxContext" connectionString="Data Source=badServer; Initial Catalog=badDB; Integrated Security=true; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
My Web.Dev.config, in part, contains valid DB values:
<connectionStrings>
<add name="PdxContext" connectionString="Data Source=.\sql2012; Initial Catalog=test; Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
...but when I select the Dev configuration and hit run, the site is run using the web.config file and not web.dev.config (it tries to connect to a non-existent database named "badDB" on host "badServer").
If I right-click on web.dev.config and choose "Preview Transform" the Transformed Web.config correctly shows that it will use the connectionStrings value from web.dev.config.
Oh, and in the Configuration Manager for Active Solution Configuration for "Dev" the configuration column has "Dev".
Any thoughts?
The solution can be found here:
https://stackoverflow.com/a/3994081
If you're using Visual Studio 2013 also see Matti Price's reply to that post, "For anyone struggling with this in later versions of VS, I had to change <Target Name="CopyWebTemplateConfig" BeforeTargets="Build"> To <Target Name="CopyWebTemplateConfig" BeforeTargets="PrepareForBuild"> And it all worked after that."
When I create a new web site project (not web application) in VS 2010, it seems to have some dlls already referenced. If I try to add System, System.Core, System.Data, I get the error stating that "The Web site is already referencing the assembly 'System.Core'" etc. When I try to use a datatype from one of these assemblies, it seems to be recognized and I get intellisense and all. But I cannot see these dlls added in the Web.Config nor when I open the references tabs in the properties of the website. Is there any new secret place that these "Default" references are being added to?
Background Info:
The main reason I am trying to figure out these references is that I have migrated a project from VS 2008 to 2010 and I am getting build errors that System.Data.DatasetExtensions need to be refered. However, the reference is present in the web.config and I can also see the reference in the project properties. I also checked that it has the same version and same public key token as the build error. Below if the build error:
error BC30007: Reference required to assembly 'System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' containing the base class 'System.Data.TypedTableBase`1'. Add one to your project.
Here is the reference in the web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" strict="false" explicit="true">
<assemblies>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<authentication mode="Windows"/>
</system.web>
</configuration>
I would really appreciate if someone can help me figure this out.
Thanks.
Notes:
1. I do not have the option of moving to a Web Application project.
2. In VS 2008, references from GAC were stored in Web.Config, Project references were stored in the solution file and other dll references were stored as .refresh files in the bin folder. I cannot find these dlls in any of the above places.
I had the exact same problem, and I eventually figured out the cause to mine was that I had copied a DLL to the project's BIN folder. Doing this causes Visual Studio to consider the DLL referenced. If you move the DLL outside of that folder, you'll be able to add the reference from within Visual Studio.
In the team I'm working in we have a big product with many WCF web services and some web sites which use the services. We are just about to upgrade to VS 2010 and I'm looking at if we should start using the new config transform functions in VS 2010.
We have several different environments which need different web.configs (database connection strings, WCF addresses and so on). Often when debugging something high up such as the web frontend it is useful to configure it to directly connect with the TEST or QA backend / databases. On each developer's local machine the IIS is configured directly to the source folder of each WCF/web project, and when running locally it is a simple matter of Ctrl-Shift-B or F5 to debug something.
One would think that it would be possible to build/F5 with TEST or QA as configuration mode and get the TEST/QA config, but I don't see how. Is it not supported, or maybe we need to change how we work with things?
Our other option is to instead use a simple replace-script as a prebuild event that creates the web.config from a template and a key-file depending on configuration mode. With this method you would get TEST config if you compile in TEST and so on but it feels a bit bad to roll our own solution when there is a function built into Visual Studio.
You can achieve the effect you're looking for by using the BeforeBuild and AfterBuild targets available in the .csproj file. The VS.NET IDE will execute these targets when doing a Build or a Rebuild, so you can use them to execute the web.config transforms. Since you'll need to do a web.config transform and then overwrite the actual web.config file, you'll need to rely on a new file called web.default.config to store the base web.config data.
I tried this out in a test project, here were the changes I made to the .csproj file:
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<ProjectExtensions>
...
</ProjectExtensions>
<Target Name="BeforeBuild">
<Copy SourceFiles="$(ProjectDir)web.default.config" DestinationFiles="$(ProjectDir)web.config" />
</Target>
<Target Name="AfterBuild" Condition="$(FirstRun) != 'false'">
<MSBuild Projects="$(MSBuildProjectFile)" Targets="TransformWebConfig" Properties="FirstRun=false;" />
<Sleep Milliseconds="2000" />
<Copy SourceFiles="$(ProjectDir)obj\$(ConfigurationName)\TransformWebConfig\transformed\web.config"
DestinationFiles="$(ProjectDir)web.config" />
</Target>
I had to manually add these to the .csproj file (I used Notepad++). As far as I can tell there is no way to add these instructions through the VS.NET IDE. You need to supply the conditional on the AfterBuild to keep from having a circular reference, as the call to MSBuild will rerun the build to generate the web.config transform.
Basically what we're doing is copying the web.default.config file (our base template) over the existing web.config before we start to build, and then we use MSBuild to generate a web.config for whatever configuration we're building. After the transform is complete, we use a Copy task to take the transformed file and copy it over to the web.config file in the web root. One issue I occasionally ran into was a file in use error when trying to overwrite the web.config after the transform was complete. Adding a Sleep task (from MSBuildCommunityTasks) after the MSBuild task took care of that issue.
I only tested this approach using the built in ASP.NET server, not IIS, so YMMV but I feel like this is a workable solution.
The FirstRun idea came from this post.
Every time i build, or publish, a web-site, Visual Studio attempts to check out the web.config file so that it can add numerous assemblies that are not required.
In other words:
web.config before:
<configuration>
<system.web>
<compilation>
<assemblies>
</assemblies>
</compilation>
</system.web>
</configuration>
web.config after:
<configuration>
<system.web>
<compilation>
<assemblies>
<add assembly="Microsoft.ReportViewer.Common... />
<add assembly="Microsoft.ReportViewer.WinForms... />
<add assembly="System.DirectoryServices... />
<add assembly="System.Windows.Forms... />
<add assembly="ADODB... />
<add assembly="System.Management... />
<add assembly="System.Data.OracleClient... />
<add assembly="Microsoft.Build.Utilities... />
<add assembly="Microsoft.ReportViewer.ProcessingObjectModel... />
<add assembly="System.Design... />
<add assembly="Microsoft.Build.Framework... />
</assemblies>
</compilation>
</system.web>
</configuration>
None of these assemblies are required, and most don't exist on the target test, or production, servers.
i keep deleting them every time i build, but it's getting real annoying real fast.
Right now my workaround is to leave web.config read-only - so Visual Studio cannot add assemblies to it.
Update
Screenshots as proof:
Project Property Pages before:
Web.Config before:
Project Property Pages after:
Web.config after:
Update Two
It should be pointed out explicitly that the web-site works without these extraneous references being added. My interim solution is to keep web.config read-only, and hit Cancel whenever Visual Studio complains that it's read-only as it tries to modify it. If i can just stop Visual Studio from trying to modify it in the first place...
Update Three
It looks like it's not possible. Someone can feel free to give the correct answer, "You cannot stop Visual Studio from adding assemblies to your web.config." and i'll mark it.
The only reason i'm keeping the question up is that hopefully someone knows the super-secret option, or registry key, or project or solution setting, to tell Visual Studio to stop thinking.
Update Four
i didn't accept the accepted answer, and i'd unaccept it if i could. i'm still hoping for the panacea. But right now i'm leaning towards:
Answer: cannot be done (manu08)
Workaround: filtered GAC assemblies registry key (Nebakanezer)
How do i stop Visual Studio from adding assemblies to my web.config?
References
ASP Net - Visual Studio keeps adding Oracle assemblies to web.config
Why are the Visual Studio Add-In Assemblies being added to my web.config?
Visual Studio Adds Assembly Reference To web.config
removing VsWebSite.Interop Assembly from Web.Config
Visual Studio 2005 automatically adding references to web.config on build
Maybe the "Avatar DotNet Library" is referencing those assemblies by itself.
The references of a referenced assembly are needed to correctly deploy a project.
Otherwise, how could the referenced assembly work?
Note that it's possible that your referenced assembly does not use its own references, although they exists.
Edit: You can use the great tool ".Net Reflector" to check this.
I used VS2005 to edit a .net 1.1 (VS2003) .aspx and saved it, then the web.config will mysteriously have the net. 2.0 assemblies added:
If I used VS2008 or VS2010, this does not happen. So I believe this is is a bug in the VS2005 IDE.
I had this problem with Visual Studio 2005 (but I'm happy to report that the solution works for VS 2008, see bolded text below). There is a registry section that VS checks before it adds assemblies to the web.config file.
Here is the key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Projects\{E24C65DC-7377-472B-9ABA-BC803B73C61A}\FilteredGACReferences
So, let's say you don't want Visual Studio to add the Microsoft.VisualStudio.Designer.Interfaces assembly to your web.config. Add the following entry to your registry and you are set.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Projects\{E24C65DC-7377-472B-9ABA-BC803B73C61A}\FilteredGACReferences\Microsoft.VisualStudio.Designer.Interfaces
It worked perfectly for me. And yes, the rest of your team will have to do the same, but at least you don't have to manually remove the entries every time :)
To make it work for VS 2008 - just change the 8.0 in the registry path to 9.0
Convert you "Web Site" project to a "Web Application" project.
A "Web Site" does not have a project file, so it contains all assembly references in the web.config. A "Web Project" has a project file, and all references are stored in the project file.
Remove the References.
If it is a web app: you can see the References under Solution Explorer.
If it's a website: right-click the project on Solution Explorer then select Property Pages. Manage them there.
HTH
If a shared assembly references them, then they will be added to the calling project as well.
Since the Avatar library makes these other references, Visual Studio adds these references to the main project as well. Otherwise, a call into the Avatar library could fail since the reference it needs is missing.
Well this might seem like a hack but given your requirements another option would be to load the Avatar assembly dynamically using Assembly.Load or LoadFrom at runtime. This would keep a reference out of the main project and should then prevent the extra reference lines in the web.config. This would only really be practical though if you were only using a small number of classes from the Avatar project. I would make a third project that both projects referenced that held interfaces that one or more Avatar classes implemented in order for the main project to maintain strict typing when handling Avatar instances. I admit this could be a lot more work that previously submitted answers. If your interested in this method search google for creating plugins in .Net
As long as you are using a website, rather than a webapp, I don't know of any way to stop Visual Studio from adding assemblies to your web.config. This same problem of sorts happens for my company's solutions as well.
You cannot stop Visual Studio from adding assemblies to your web.config.
Sorry, you cannot stop Visual Studio from adding assemblies to your web.config, but all is not lost.
I have hit this in the past; someone had added some references (including WinForms) to a low level data access assembly. The web-site used the low level data access assembly and therefore had WinForms etc added to the web.config file.
The solution was to move his code into the correct assembly and remove the incorrect reference.
If you can not sort how the assembly that has the unwanted references and you know you are not calling code that depends on the unwanted references. Then you can (none of these are nice)
Write a custom install action that automates the removal of these unwanted assembly references from the web.config
Write a custom MSBUILD action to remove then at the time of the build
Use a different hand-written web.config file when the application is installed.
It can take ages to find how why Visual Studio is adding a reference to the web.config file. You have to hand-check EVERY assembly that is used directly or indirection by the web site.
I know and appreciate why Microsoft invented Web Sites in ASP.NET 2.0, but sometimes they just plain suck. If it is practical for you, convert your site to a Web Application Project, and problems like this will go away.
If that is not practical for you, try to re-factor as much code as possible into a separate class library project. Any references you can move out of the web site and into the class library will cut down on web.config changes.
EDIT: To clarify, in a Web Site, the aspnet compiler compiles everything (markup, code-behind, the lot), so all assembly references must go into web.config. However, in a Web Application Project, the C# or VB compiler compiles the code-behind files into a separate DLL, which is then referenced by the aspnet compiler when it compiles the markup. In this scenario, assemblies that are only referenced in code-behind files will go into the code-behind DLL and not touch web.config at all. Only assemblies that are directly referenced in markup will go into web.config.
I don't believe you can prevent Visual Studio from automatically adding references to assemblies that are referenced by others.
One solution is to create a Web Setup project with a custom action that automates
the removal of these unwanted assembly references from the web.config.
Those are all the assemblies required by your project, in some shape or manor and the aid the compilation that ASP.NET does on your pages at runtime. They are probably being imported in by either by code you are using in your project or another library that is using them.
But according to the documentation. These are the assemblies defined in your global web.config which can be found in C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG:
<assemblies>
<add assembly="mscorlib" />
<add assembly="System, ..." />
<add assembly="System.Configuration, ..." />
<add assembly="System.Web, ..." />
<add assembly="System.Data, ..." />
<add assembly="System.Web.Services, ..." />
<add assembly="System.Xml, ..." />
<add assembly="System.Drawing, ..." />
<add assembly="System.EnterpriseServices, ..." />
<add assembly="System.Web.Mobile, ..." />
<add assembly="*" />
</assemblies>
If you look there is an assembly="*" reference being added. And if you read the documentation about this command it says:
Optionally, you can specify the
asterisk (*) wildcard character to add
every assembly within the private
assembly cache for the application,
which is located either in the \bin
subdirectory of an application or in
the.NET Framework installation
directory
(%systemroot%\Microsoft.NET\Framework\version).
This means that any assembly in your /bin directory or in the .NET Framework installation directory is going to be included already.
What this tells me about your problem is that those assemblies that are being included are already referenced in some way to your project. And they are probably coming from the Avatar Dot Net Library or some controls on your page. Check the "References" folder in your Visual Studio project on the Avatar Library for these references you don't want. Because that is where the build process gets these libraries from.
So in other words if you don't want them to be included scrub your referenced projects of all references of these libraries.
Alternatively you can use a MSBuild XML parser to drop that section of the web.config each time you run your build process. Personally I use a task called XmlUpdate to modify certain parts of my web.config to get it production ready. If you would like to do the same it is part of the MSBuild Community Tasks.
If you are running on a vista or server 08 machine, you can use the appcmd command line utility to remove it after rebuilding rather than manually removing it.
http://technet.microsoft.com/en-us/library/cc772200(WS.10).aspx
http://learn.iis.net/page.aspx/114/getting-started-with-appcmdexe/
see http://msdn.microsoft.com/en-us/library/ms178728.aspx
there it's explained that what you see in Property Page is not all, implicit references exist also in Machine.config file and are added at compile time. Hope this helps.
I would start by checking the "using" statments in your code files as well as any references in your .aspx, .ascx files. It sounds like you've referenced some of these (I know some are added by default from the Add New Item templates.