ninject.mvc3 not loading modules - ninject

I'm using ninject mvc3 via nuget in a MVC4 webapi application. No changes made to global.asax I have created a ninjectmode and placed it in the bin folder. I'm noticing that the line
' bootstrapper.Initialize( CreateKernel)' in NinjectWebCommon.cs is throwing an exception 'Sequence contains no elements'.
If I delete the the dll containing my ninject module the error disappears.
What am I doing wrong? I would like go be able to use NinjectModules to separate concerns in my application.

Figured it out. Apparently, when you add Ninject.MVC3 to a class library project via Nuget, it adds an "App_Start" folder similar to how it does for a web application project. This was causing the above error. I removed the App_Start folder and life is good once again.

Related

(System.IO.FileNotFoundException) .Net core Web API cannot find child dependency (Dependencies of dependency) when added DLL by "Add Reference"

I am getting System.IO.FileNotFoundException in my .Net Core Web API. So I've set up the below project to demonstrate the problem.
I created a.Net Standard library named DemoLibrary and added QRCoder dependency via NuGet.
Disclaimer: Reason for choosing the QRCoder is that the Web API doesn't use it by default. I don't use it in my project. In fact, I'm getting this exception for EntityFrameworkCore.
I created a new .Net Core Web API DemoWebAPI which has no other dependencies.
Then added the DemoLibrary to DemoWebAPI via Add Reference -> Browse -> DemoLibrary.dll.
This is my solution:
The DemoMethod method in Calculate class just creates the object of QRCodeGenerator.
public class Calculate
{
public static string DemoMethod()
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
return "";
}
}
And my ValuesController in DemoWebAPI just calls the method:
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2", DemoLibrary.Calculate.DemoMethod() };
}
Now, when I run the DemoWebAPI project I get below exception upon the call to the DemoMethod:
System.IO.FileNotFoundException: 'Could not load file or assembly 'QRCoder, Version=1.3.5.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'
I understand the fact that I have to copy the QRCoder.dll file somewhere. But I fail to understand where to put it. I've already tried putting it in "bin/debug/netcoreapp2.2" of the DemoWebAPI and "bin/debug/netstandard2.0" of the DemoLibrary.
But I couldn't get it working.
Request: Please post your answer as descriptive as you can because I am new to .Net Core.
Edit:
I am aware of the NuGet servers. I have read topics like hosting a NuGet server in IIS and Azure. The reason behind DLL reference is I want to use my DLLs in two projects one of them is a .net core API and the other is .net framework class library which is compiled by NMAKE. I couldn't find any way to restore NuGet packages in the .MAK files.
It looks like you've merely added the DLL for DemoLibrary to your DemoWebApi project. That's not how you should be adding references. Since these are in the same solution, you should add a project reference. That will fix your issue.
Now, let me explain what's actually going on here. Your DemoLibrary has a dependency on QRCoder. It's a NuGet reference, which means that package will be restored (i.e. downloaded) and included in your DemoLibrary build output. However, it will be included as one or more DLLs along side your DemoLibrary.dll. When you then just reference DemoLibrary.dll, you're missing all these other DLLs that are part of DemoLibrary and thus, things don't work properly.
Now, when it comes to a project reference, things are little more complex. A project reference essentially wraps the referenced project into your other project. You can think of it as sort of a sub project. For all intents and purposes, it's like any dependency of the sub project becomes a dependency of the main project. That means that DemoWebAPI now technically has a NuGet package reference to QRCoder even though there's no explicit package reference in its project file. The dependency comes from your DemoLibary project. As such, with a project reference, all the necessary dependencies will be included, because it's as if the main project included those itself, by way of the sub project.
For what it's worth, you should virtually never include a DLL as a reference directly. That used to be required, but the concept of NuGet packages has all but eliminated the practice. Even if DemoLibrary was not in the same solution as DemoWebAPI (meaning you could no longer do a project reference), the correct way to use it would be to turn DemoLibary into a NuGet package, and then reference it in DemoWebAPI via a package reference, like any other NuGet package. You do not simply add the DLL.

asp.net 5 project targeting dnx46 wont build

I have a solution which contains an asp.net 5 app, a code library, and a test library. The libraries are also the new project.json style. I am not really sure how to refer to these libraries to distinguish them from the existing library project structure.
Anyway, all three projects were targeting dnx451/net451. I decided to try targeting dnx46/net46. The libraries build, but the asp.net dnx app fails. The error is:
The design time host build failed with the following error: Object reference not set to an instance of an object.
It is referencing the file Microsoft.DNX.targets at line 166.
There is another error that just says, "Object reference not set to an instance of the object." It says line 1, but doesn't reference a file.
To test I created a new solution with and empty asp.net 5 project and 2 class libraries. I changed dnx451 to dnx46, and net451 to net46, and it compiles fine. I am not sure what in my existing project is causing this error, or where to start looking.
By the way, dnx46/net46 are the only frameworks targeted. I have removed the core frameworks.
What a load of trial and error. Turns out the issue was the razor precompilation module as described here, http://davidzych.com/view-compilation-in-aspnet-5-with-the-razorprecompilemodule/.
You can read the github issue here, https://github.com/aspnet/dnx/issues/3003.
I first tried to comment out the precompilation class. However just that fact that the file was there, made dnx throw the error. I removed the precomilation file and now it compiles fine.
EDIT:
Alternatively you can add dnx451 back into your project, along with dnx46 and then the precomilation module will work fine.

App_Code folder created automatically in published website

I have a MVC4 App created in VS 2010 with Umbraco 6 too and I've created a web deploy project which is used by my Team City CI server to deploy my website to a CI environment for testing.
On the CI server the first time I load the homepage (or any page) it loads perfectly fine. However the act of loading a page creates a App_Code folder on my CI server, and after that I get the message "The directory '/App_Code/' is not allowed because the application is precompiled". Removing the App_Code folder means that it once again works for one page load and the folder is created again.
Removing the PrecompiledApp.config file causes my site to not load with a YSOD stating "Object reference not set to an instance of an object." at the following point in the stack trace "Umbraco.Web.UmbracoModule.BeginRequest(HttpContextBase httpContext) +25"
To be clear, I don't have an App_Code folder in my project, and I don't want or need one. All I want is for it to stop creating one automatically upon page load! I've used Umbraco in VS and deployed in the same way many times before, just not with Umbraco 6 and in an MVC project.
Any ideas why App_Code is being automatically created and what I can do to stop it?
Many Thanks
Richard
I seem to use Umbraco in a similar way as you do, wrapping it as a MVC 4 project. Hence it becomes a "VS Web Application" instead of a "VS Web Site".
What is important to remember is that Umbraco originally wasn't made to be run as an application and a lot of the functionality in Umbraco is first and foremost directed to using App_Code.
The internal classes AssemblyExtensions, PluginManager, TypeHelper and the public class TypeFinder in Umbraco.Core have methods that are dependent on that the App_Code folder is there. Even if you don't need an App_Code in your solution Umbraco does, if you don't want to see it simply hide it from your solution. If you really don't want it remove all references to it in the source and create your own compilation of Umbraco.
== EDIT BELOW ==
After reading your comment and post again I created a small solution for your problem. The fact that Umbraco creates the App_Code is still because of the framework initialization that won't work without App_Code existing. But recompiling and creating an own dist of Umbraco will as OP points out create some extra maintenance when upgrading and so on.
It's not the ideal but most clean way to handle this matter to allow Umbraco to create the App_Code folder but also remove the same when the application have initialized. I'd use an IApplicationEventHandler. The sample code works on my box.
using Umbraco.Core;
using Umbraco.Core.IO;
namespace YourNamespace.EventHandlers
{
public class AppCodeEvents : IApplicationEventHandler
{
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{ }
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{ }
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
if (System.IO.Directory.Exists(IOHelper.MapPath(IOHelper.ResolveUrl("~/App_Code"))))
{
System.IO.Directory.Delete(IOHelper.MapPath(IOHelper.ResolveUrl("~/App_Code")));
}
}
}
}
If you are precompiling your site using a web deploy project, I assume you have all your references separated out from the project - which is a good thing. So, I think the simple answer here is don't precompile the site, just allow the web application to be built so that it pulls in the references and deploy the built project.
Personally, I find the best way to work with Umbraco v6 is via NuGet. Create an empty MVC4 project and use NuGet to add Umbraco v6. This will automatically sort out all the references for you. This is because those fantastic guys at Umbraco created two Nuget packages, one with the project files and the other with the core DLLs.
This means that when the site is built, the references are pulled in and updating the site is easy. It is just a matter of updating via NuGet.
This was happening me as well, turned out it was because a 'precompiledApp.config' file had somehow made its way onto the production server... not sure how that happened but once I deleted and recycled web app this stopped happening.

MVC4 Project and ImageResizer

Problem: MVC 4 test application with ImageResizer on any page access responds with "Could not load type 'ImageResizer.InterceptModule'"
To test and understand how ImageResizer works a MVC4 project was created in VS 2012, a single controller was created to display a HelloWorld page. Works as expected.
Next step was to add ImageResizer from NuGet. The ImageResizer Web.Config Installation for MVC was installed. This package added three additional packages:
ImageResizer Web.Config Installation
ImageResizer.MVC - MVC friendly utilitite
ImageResizer
Attempting to run the application results in a yellow screen of death with the "Could not load type 'ImageResizer.InterceptModule.'
Attempt to resolve with no success include:
Verify DLLs exist.
Remove and reinstall packages.
Copy ImageResizer dll from a working webforms application
Item 1 on the ImageResizer troubleshooting guide doesn't appear to be the issue "1.Your website has a 'sub-site' (Application Folder) inside it. Application Folders inherit all Web.config settings from their parent sites, yet expect to have their own copies of all the dlls referenced by those settings in their own /bin folder. You can resolve this problem by (a) changing the app folder to a virtual folder, (b) adding a copy of ImageResizer.dll and plugins into the /bin folder inside that application also, or (c) using statements in the child Web.config to cancel out the inherited statements from the parent Web.config. Option (c) will disable image resizing within the sub-application."
Yes there is a web.config file in the views directory. Tried creating and copying image resizer dlls to a bin directory in view - no success. Tried adding a remove to the web.config in the views directory. Again no success.
Problem resolved - self inflicted.
It seemed like a good idea to name the test project imageresizer. Which of course created a dll name imageresizer which walked all over the real imageresizer dll.

Custom class in DotNetNuke Module

I know this can be done as there are other modules out there that have this, but I'm just not getting it to work.
I have created a custom module for a DotNetNuke site. I want to be able to create a class object within the module to hold the information about that object. I can create the object and everything complies. But when I go to use the object in the code-behind it states that the object is not defined. I'm not really sure where to go from here.
This is the beginning of the View.ascx.vb :
Namespace Modules.VacationForms
Public MustInherit Class View
Inherits PortalModuleBase
This is the object class beginning:
Namespace Modules.VacationForms
Public MustInherit Class Vacation
I'm really not sure why this is not working. I did download another module code to compare and as far as I can tell everything is the same. Any help is appreciated.
Are you using a Web Site Project or a Web Application Project? The Web Application project will allow you to compile all of your code together (the only issue here might be the the Root Namespace setting in your project, but, assuming both classes are in the same project, that shouldn't be it). If you're in the Web Site project (e.g. developing directly in the DNN solution), then your code won't get compiled in the traditional sense, but will be on-demand compiled by DNN. It only does that for code behind files associated with requested controls/pages (e.g. your View.ascx.vb) and code files in the App_Code folder. I would guess that your hangup is that your Vacation class' code file isn't in the App_Code folder.
It looks you are not using the Web Application Project for module development. Easiest thing to do is install module development templates (from dotnetnuke.codeplex.com download starterkit package of your dnn version).
If your module is too simple and you don't want to do that, OR you don't want to install the templates in your pc, you can do following:
If you are using a vs version that is not using WAP by default, get the installation from web.
create a new folder for your dnn module in DesktopModules directory in root
add a new WAP project in that folder.
remove web.config from that folder, go to properties and point build output director to your dnn site's bin directory (../../bin will work most of the time)
Once you are done with that, all your code will start working as expected.
Good thing about this is, all your .vb and .ascx.vb files will be compiled in a single dll that you can distribute as a package easily.
Hope this helps