Force C# to load an assembly that is referenced only in a cshtml file
Asked Answered
M

1

6

How to force C# run-time to load an assembly that is used only in a .cshtml file which is a Razor template?

I am using the JSON serialization functionality from Newtonsoft, the assembly is referenced by the project, but when the Razor engine compiles the template I get an error that the Newtonsoft namespace is not known. I also see that the dll is actually not loaded (I guess it makes sense since it is not referenced anywhere in the C# code). If I add a dummy line somewhere in my C# code that references any function from the assembly it is loaded and Razor is happy. Is there a way to specify that the assembly should be loaded even though it is not referenced? I really don't want to leave the dummy line in the code.

Michaels answered 5/9, 2016 at 16:3 Comment(1)
How to add assembly in web.config fileCapernaum
R
3

You are right, the compiler deletes the project reference during compilation/optimization, because it detects that it's not really being used by the code.

The official way to tell the runtime to load the assembly anyway by configuration, is to add a reference in your web.config file inside the <compilation> section

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="Newtonsoft.Json" />
  </assemblies>
</compilation>

That being said, sometimes the web.config is not available, or I want to avoid bloating it with more and more lines, so I personally prefer the hack of making a one line reference in the code: (yes, a dirty bad nasty & silly hack, but I can't find much harm being done).

class LoadNewtonSoftHack
{
    private void DoNothing()
    {
        var o = Newtonsoft.Json.Linq.JValue.Parse("{}");
    }
}

A third alternative would be to force the assembly load when the web app starts: (but ensure that your build copies the .dll in your bin\ folder).

Assembly assembly = Assembly.LoadFrom("Newtonsoft.Json.dll");
Redtop answered 5/9, 2016 at 16:13 Comment(8)
The web.config method would be preferable IMO.Capernaum
Unless precompilation of views is turned on, the views aren't compiled until run-time anyway. Using the web.config in the way described here is how you tell the run-time compilation to reference assemblies.Capernaum
Your points are valid @spender, but I think this is a situation where more than one solution is applicable. I insist you add a new answer to this question.Redtop
I completely agree, but simply repeating the info that is in the other question/answer mostly leaves me wanting to press the dupe button (which would be very final because of my gold badge). I think linking to this info in the comments is good enough... there's a reference to it in the comments to both the question and your answer.Capernaum
To be fair, the answer would be a duplicate, but the question is not. I just added a 3rd option explaining how to add it into the web.config.Redtop
@spender: comments fade away. It's not enough to link in comments. If it's relevant it should go into the answer.Pericranium
I started a conversation on meta about this. meta.#334483Capernaum
Since I didn't even have a web.config file I chose the third approach and it worked fine, thanks:)Michaels

© 2022 - 2024 — McMap. All rights reserved.