Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not reference other projects

I want to create 3 projects in a Visual Studio Solution:

  • Windows Service (references Business Layer, Common Layer, log4net.dll)
  • Business Layer (references Common Layer, log4net.dll)
  • Common Layer (references log4net.dll)

Steps:

  1. I create the Business Layer and the Common layer using the wizard, without doing any modifications. I set up the references and everything is working.

  2. Then I create add a new Windows Service project to the solution. I notice that instead of Any CPU it is added with target platform x86. I reference the 3 things and the code is colored correctly, pre-compilation errors disappear: this means that the references are recognized.

  3. When I build the solution however the Windows Service project has compilations errors: can not find any of the referenced assemblies.

What is happening here? Is the x86 configuration causing the "The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)" errors?

I forced the Windows Service project to use ANY CPU configuration. I double checked every reference, they are correct. Still I cannot reference anything.

System specs:

  • Windows 7 x64 SP1 Visual Studio 2010
  • Ultimate
like image 705
Germstorm Avatar asked Jan 19 '23 07:01

Germstorm


2 Answers

Well, you already found the answer. Log4net has a dependency on System.Web.dll, an assembly that is not available in the client profile. The probable core reason for this dependency is log4net.Appender.AspNetTraceAppender class, it uses the HttpContext class and that requires System.Web.

Right now, that's something you need to know from the documentation of the library or by finding it out the hard way. Building produces a warning but it only a warning and not an error, easy to miss:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "C:\projects\WindowsFormsApplication2\ClassLibrary1\bin\Debug\ClassLibrary1.dll" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.

Followed by errors from any statements in your code that try to reference the classes in the assembly that contains the log4net reference.

C:\projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs(12,9): error CS0246: The type or namespace name 'ClassLibrary1' could not be found (are you missing a using directive or an assembly reference?)

You're not missing either and there can be a lot of errors if you don't build incrementally while writing code. You focus on the errors and won't realize the warning is the true source of the errors. Another nasty trap is that IntelliSense is happy at first, it does resolve the assembly reference properly. There is some evidence that the warning isn't always produced, purely by the fact that a lot of programmers seem to miss it. I haven't yet found a scenario where that's the case though. Not sure.

like image 113
Hans Passant Avatar answered Jan 21 '23 22:01

Hans Passant


After loosing a few handful of my hair I found this Stackoverflow answer which solved the issue. But why would Visual Studio add a Windows Service with x86 as target platform and .Net Framework 4 Client Profile as target framework?

like image 38
Germstorm Avatar answered Jan 21 '23 21:01

Germstorm