I have some general questions about web.config and how it works regarding assembly reference. I’ve been playing around with the new Razor view engine and had some trouble getting it up and running.
My problem started with a general
The type or namespace name ‘XXXXX’
does not exist in the namespace
‘XXXXX’ (are you missing an assembly
reference?)
Now I figured this was just a simple Add Reference to the project and I’d be on my way. But even after I added a reference to the missing assembly, my project kept breaking.
I found the solution, and I had to add an assembly reference within the web.config. Once I did this everything worked fine.
First, I want to understand why it took adding a reference to web.config to fix this problem. Why wasn’t a project reference just good enough?
Second, when it comes to adding references in web.config, I would like to understand the syntax. When I see markup like this
<add assembly="System.Web.WebPages" />
it seems very clear to me that I’m adding an assembly named System.Web.WebPages. But the full syntax in my web.config is
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
Version seems self explanatory, but what is culture and PublicKeyToken used for? Are they required? What happens if I don’t know either of them, can I just put anything in?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Answering your first question:
There’s another SO question here with a useful answer:
What is difference between web.config assemblies element and project file Reference element
Essentially it is to do with differences between Web Projects and non-web projects in Visual Studio (as I understand it).
Answering your second question:
This took quite a bit of digging around but here you go:
Culture – most assemblies should be culture neutral (assuming this is set in the application). However if you do have culture sensitive assemblies then this is where you specify the culture of the assembly.
PublicKeyToken – this is the public key from the public/private key pair that was used to sign the assembly. Having this enables .NET to verify that the assembly it is loading is the correct one.
More useful information here:
http://en.wikipedia.org/wiki/.NET_assembly
Method 2
An old question, already answered, but I came across it and wanted to add:
The importance of a Public Key Token is that it is absolutely required for a strongly-named assembly. In fact, that’s how you get a PKT: Sign the assembly (generate a strong-name key). And you can’t store an assembly in the GAC without signing it.
So it’s more than just a matter of identifying the assembly… it’s a cryptographic way for .NET to validate the signature of an assembly, and to ensure that it hasn’t been tampered with.
Method 3
Everyone seems to be giving the ‘run-around’ with this answer. It’s simple:
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
</configuration>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0