$(ProjectDir) etc. are not being replaced in my Visual Studio C# project's app.config file.
I'm sure this is simple and I'm just searching for the wrong things, but does anyone know?
EDIT:
I want to have this work in my app.config:
<add key="path" value="$(ProjectDir)..\blabla">
Things like $(ProjectDir) are build-time (or rather, design-time) environment variables for use within Visual Studio and the MSBuild build system. They have nothing to do with the applications you're building. Your app.config file is your program's runtime configuration file.
If you want to have text inside your app.config file vary when your project is built then you should look at T4 Templates: http://msdn.microsoft.com/en-us/library/bb126445.aspx - that you can use VS's environment variables from within them (see http://msdn.microsoft.com/en-us/library/vstudio/ee848143(v=vs.100).aspx under "Access to environment").
But in your case, it looks like you could just use relative paths in your application, why do you need to hardcode paths in your config file?
You don't need to use $(ProjectDir) in your app.config. The app.config file is already in the project directory so you can just use a relative path from there.
<add key="path" value="blabla">
In visual Studio 2010, I want to make all of the projects in one solution have the same configuration such as the include directory,the lib directory? I know there is a thing called property manager that can do this, but it makes other solutions have the same configuration.
Is there some ways to deal with it:Only let the projects in the same solution have the same configuration and don't affect other solutions?
What you can do is make your own .props file. If you open the .vcxproj file with a text editor, you can find some lines down the file something like this:
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
That imports (or includes or calls) some other .props file. Look for that file and use a text editor to open it, here it is in C:\Program Files\MSBuild\Microsoft.Cpp\v4.0. This gives you and idea of how .props files are structured.
Now, you can make your own .props file and in it set the include and lib paths like you want them (maybe set additional settings as well) and finally you have to add an <Import.../> line in your .vcxproj files manually. Don't worry, VS will not remove it, even if you make changes to the project.
Helpful web sites for .props programming:
Visual C++ Team Blog: http://blogs.msdn.com/b/vcblog/ (look around there, it's a treasure trove)
A guide to .vcxproj and .props file structure: http://blogs.msdn.com/b/visualstudio/archive/2010/05/14/a-guide-to-vcxproj-and-props-file-structure.aspx
In our Visual Studio 2010 project we reference - among the others - the SmartAssembly.Attributes.dll and it's properly copied to the Bin/Debug or Bin/Release folder of our local dev machines. On the build server this is true for all other references but not for 'SmartAssembly.Attributes.dll'. The build succeeds.
What should I check?
Thanks.
Open your .csproj file (or .vbproj file), and look for the assembly reference. Then make sure the hint path is still valid in your build server. Sometimes VS2010 will add an absolute hint path, instead of using one relative to the .csproj file itself, so the drive letter may be invalid in another machine.
For example, the reference may look similar to the one below (I made up all of the XML below to illustrate only and it's NOT valid), and the absolute hint path may be invalid in the build server (e.g. there's no drive letter d there):
<Reference
Include="SmartAssembly.Attributes, Version=8.0.0.0,
Culture=neutral, PublicKeyToken=b03f1f7f1ad5da3a,
processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<Private>true<Private>
<!-- The HintPath below should exist and be valid in your build server -->
<HintPath>d:\temp\SmartAssembly.Attributes.dll<HintPath>
</Reference>
You can change the HintPath to make it relative to the .csproj file and therefore more general. For example:
<HintPath>..\libs\SmartAssembly.Attributes.dll<HintPath>
Another problem may be that <Private>true<Private> isn't there. This attribute mapped to the CopyLocal property in Visual Studio, so if it's missing from your .csproj, the DLL won't get copied to bin\Debug or bin\Release by MSBuild. See http://bronumski.blogspot.com/2009/06/project-reference-fun-and-games.html
I'm having a bit of trouble with my web.debug.config and web.release.config files.
To be more specific, my webpage will only use the default web.config file, and completely ignore the debug and release files.
This happens not only when I run the project locally, but also when I publish it to an IIS server.
I am using Visual Studio 2010 Ultimate (no Service Pack).
Here are the config files:
web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyConn" connectionString="SomeConnectionString"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
web.debug.config and web.release.config:
<?xml version="1.0"?>
<configuration xmlns:xtd="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MyConn" connectionString="SomeOtherConnectionString"
providerName="System.Data.SqlClient"
xtd:Transform="Replace"
xtd:Locator="Match(name)"/>
</connectionStrings>
</configuration>
As I mentioned before, the website uses the connection string from the web.config file, and not from the web.release.config.
Another interesting point is that in the physical folder to which I published the project, all three config files exist; each exactly the same as they appear in the VS solution.
Suggestions, anyone?
You need to run the transformations. It will not work if you just put the 3 web.config in your folder. You can run this and it will transform your web.config
MSBuild.exe Project.csproj /T:TransformWebConfig /P:Configuration=Release
You will then have a folder in your obj folder created at build that will be called TransformWebConfig. The transformed web.config will be in this.
You can also take a look at this post, he creates a build target to achieve this automatically.
http://vishaljoshi.blogspot.com/2010/05/applying-xdt-magic-to-appconfig.html
I figured out what I have been doing wrong.
Instead of a Web Application, I was using a Web Site Project.
(The difference between the two is that a Project doesn't actually contain a .proj file. How ironic.)
Now that I've realized the actual problem, it turns out that I'm not the first with it...
Here's a link to a previous post with a workaround solution:
How do I do Web.config Transformations with Visual Studio Website Projects?
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.