0

I read a lot of similar questions out there, but I can't solve my particular problem... In my MVC3 project I use an external library. I can use this library everywhere, but not in my razor views.
So, reading some similar question on SO, I found out that I should register this library into the <system.web><compilation><assemblies> section. Trying to do this, I ended up with a portion of my web.config like this

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    ... <!-- default assembly registration, like System.Web.something -->
    <add assembly="MailBee.Net.dll, Version=7.1.4.348, Culture=neutral, PublicKeyToken=cd85b70fb26f9fc1" />
  </assemblies>
</compilation>

But still don't work... or to be more precise, this broke up all the project at runtime. If I launch the project, it crashes telling me Impossibile to load assembly 'MailBee.Net.dll, Version=7.1.4.348, Culture=neutral, PublicKeyToken=cd85b70fb26f9fc1' or one of its dependency
The dll for sure is in the /bin folder of the web application and, deleting the declaration in the web.config file, I cau use it in all the project but in the views page. Any idea?

themarcuz
  • 2,573
  • 6
  • 36
  • 53
  • does the external assembly have any dependencies that you haven't added to your MVC project? – James Nail Feb 22 '12 at 16:53
  • @JamesNail: As I was asking to Erik, this should make the library impossible to use everywhere in the project (for example in my controllers), not just in the razor view... – themarcuz Feb 22 '12 at 17:03

2 Answers2

0

There are a few possible problems:

  1. MailBee.Net.dll has a dependancey/requirement on another dll that is not in your solution.
  2. MailBee.Net.dll is not the same x86/x64 version as your project/hosting solution (visual studio/iis express)

Additionally, in your web.config file located in the Views directory you should add something like the following:

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, 
                     System.Web.Mvc, 
                     Version=3.0.0.0, 
                     Culture=neutral, 
                     PublicKeyToken=31BF3856AD364E35" />
   <pages pageBaseType="System.Web.Mvc.WebViewPage" />
     <namespaces>
       <add namespace="<NamespaceYouNeedInYourViews>" />
     </namespaces>
   </pages>
</system.web.webPages.razor>
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • for you first 2 point: doesn't this should bring all the code not to work with that library? I mean, if I don't try to use this library on my razor view, everything works fine, and I use it extensively on my controller classes for example. – themarcuz Feb 22 '12 at 16:59
  • As for the second suggestion, this should be done only to avoid the `using` statement on each page needing that namespace, am I right? – themarcuz Feb 22 '12 at 17:01
  • If it is working fine in your controller, then I would not add it to the assemblies, I would add the namespaces you need to the web.config. – Erik Philips Feb 22 '12 at 17:01
  • Yes it avoids requiring to use `using` statemtn on every razor view. – Erik Philips Feb 22 '12 at 17:02
  • tried avoiding declaring in the assemblies and put it on the namespaces declaration... nothing changes. Moreover I didn't have the intellisense working for objects of that class, in the razor view, while work as expected in the controllers – themarcuz Feb 22 '12 at 17:07
  • When you added the DLL to your project, you referenced it in the standard way by *Add Reference*? – Erik Philips Feb 22 '12 at 17:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8080/discussion-between-erik-philips-and-themarcuz) – Erik Philips Feb 22 '12 at 17:15
  • Yes, I did it in the standard way – themarcuz Feb 22 '12 at 17:15
0

I got it! It's not a problem of dependencies, nor a problem of declaring the assembly or the namespace somewhere... it's just that that library, for some reason, is not copied into the bin folder when building the project! Or better, the reason is that the property "Copy local" on the referenced library is set to false, but I have no idea why: every other third party library I tried with haven't this behaviour...

themarcuz
  • 2,573
  • 6
  • 36
  • 53