Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio copys .config file to bin on build, but MSBuild does not

I have a WebAPI project in Visual Studio 2013. If I build the project in Visual Studio, in the bin/ directory I see a file called MyProject.dll.config, which represents the web.config file at build time.

However, if I execute MSBuild from the command line, the .config file is missing, but all other files are present.

> msbuild.exe /t:build /v:q /p:Configuration=Debug /nologo \
  D:\Workspace\MyProject\src\MyProject.sln

What gives? Why isn't the .config copied?

like image 888
Matt Avatar asked Jan 12 '14 02:01

Matt


2 Answers

For deploying a web project or a web api project, the fact that there's no $(TargetName)$(TargetExt).config isn't a big deal. At run-time, IIS will use Web.config to figure out everything it needs for your assembly.

BUT!

If you're using a Web App or Web Api project as the basis for testing* then you can hit some snags. In particular, when it comes to assembly binding redirects (as is the case with something within the bowels of MVC which still relies on Newtonsoft.Json 4.5.0 when the current version at time of writing is 7.0.0). A colleague had a similar issue with another assembly his test project was depending on.

Now when you run your tests through Visual Studio (eg, via Resharper), they all work just fine. However, when your tests get to the CI server and they are run by nunit-console, you'll see assembly load errors. Not pretty. This is because of the described behaviour where VS is sneakily copying the .config file to the correct output and msbuild isn't. However, you can work around this with a post-build build event:

copy $(ProjectDir)Web.Config $(TargetDir)$(TargetName)$(TargetExt).config

This has resolved my issues with redirects. I hope it helps someone else.

  • You may ask "Why use a Web App or Web API project as your test project?". A Web* project is a lot more comfortable to deal with as a base for a test project which deals with .net assemblies and JavaScript tests as JavaScript is properly recognised (syntax highlighting) and there's a Scripts folder which has the quick "Add -> Javascript File" menu item for itself and descendant folders, so I prefer to use this instead of a plain Class Library project.
like image 123
daf Avatar answered Nov 08 '22 04:11

daf


When I create a WebAPI project the web.config Copy to Output Directory is set to Do Not Copy by default. Did you select the Web.config in Solution Explorer and set this to a copy action?

I'm at a loss to explain why it seems to copy for you with the IDE build but NOT the msbuild cmd you show, this is not the behavior I see with a fresh WebAPI project in 2013.

like image 1
JohnZaj Avatar answered Nov 08 '22 04:11

JohnZaj