I came across an interesting compiler error message when working on an MVC3 Razor view the other day. I needed to bind model data from a type in the .NET System.DirectoryServices.AccountManagement namespace but received the following error when the View rendered.
Compiler Error Message: CS0012: The type 'System.DirectoryServices.AccountManagement.ContextType' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.DirectoryServices.AccountManagement, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
I was confused by the message at first since I had a reference to System.DirectoryServices.AccountManagement in the project. In fact, a couple of model classes and controllers referenced the namespace. When I tried to add a @using directive to the view, the namespace could not be resolved. Hmmm…head scratching ensued.
This post helped break things open for me by suggesting to copy the assembly locally by setting the “Copy Local” property of the System.DirectoryServices.AccountManagement assembly to True. This did indeed resolve the issue and I didn’t even have to add a @using directive to the Razor view. Let me know your thoughts on this approach to resolving the issue.