Friday, September 10, 2010

The referenced assembly "log4net" could not be resolved because it has a dependency on System.Web

Here is a riddle, what is wrong with this brand new .NET 4.0 console application that only references the log4net 1.2.10.0 dll?

error CS0246: The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "log4net" 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.

Answer

The projects target framework is set to the client profile version of the .NET framework, which excludes (typically) server side assemblies such as System.Web.

Changing the Target framework under the Application project properties resolves the issue.

See Also:

6 comments:

  1. The answer doesn't quite explain why this should be necessary...

    ReplyDelete
  2. The linked blog explains the difference between the full and client profile install of the .NET framework.

    "there are now more than one installation profile for .NET Runtime Libraries. One is Full and one is Client. The idea behind this is that if you are building a program that does not need to reference server side assemblies (System.Web.* as an example) then why install them? You can now target a Full or Client profile. The Client profiles will NOT have the server assemblies installed." - David Sandor

    ReplyDelete
  3. Still, why does log4net need System.Web in the first place?

    ReplyDelete
    Replies
    1. I ran the log4net.dll through a reflection tool. The dependency comes from the log4net.Appender.AspNetTraceAppender which accesses the HttpContext from the Web namespace.

      Delete
  4. Thanks Daniel. I was able to fix the issue quickly with your posting.

    ReplyDelete
  5. My target framework was already targeting the full .NET 4.0, I had to set the log4net dll reference to "Copy Local=True" to make it work.

    S.Audet
    MyDevPartner.com

    ReplyDelete