Ninject with MVC3 RTM - asp.net

I've upgraded MVC3 from RC2 to RTM. We were using Ninject 2.1.0.76, but things stopped working once I upgraded. So I used the NuGet manager to get the latest Ninject, Ninject.MVC3 and Ninject.Web.Mvc libraries (2.1.0.91, 1.0.0.0 and 2.1.0.39 respectively). Now, it creates an AppStart_NinjectMVC3 file.
I removed NinjectHttpApplication from my global.asax and made it back into a regular HttpApplication. When I tried to build, I get;
"Exception has been thrown by the target of an invocation"
Looking further, if I disable the following line;
DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
The build goes through. But I'm pretty sure I don't want to do this.
Any ideas?
----- UPDATE ---------
I created a new MVC3 project, added the reference to Ninject.MVC3 and this builds and runs fine. I compared web.config, don't see any differences that relate to Ninject or MVC in the two. A fresh project doesn't add the Ninject.Web.Mvc library, so I removed it and commented out all code relating to that, and still, the error occurs.

Since ASP.NET MVC 3 Beta the IServiceLocator interface is replaced by IDependencyResolver. I'm not sure Ninject.MVC3 already has a release where they have implemented this interface.
Judging from the line DependencyResolver.SetResolver(new NinjectServiceLocator(kernel)) it appears they have not.
Here's a simple implementation of this interface for Ninject.
UPDATE: The Ninject.Web.Mvc library has a NinjectDependencyResolver class that extends from the IDependencyResolver interface. I think you should use this one (I do and everything works fine).

Download the dlls from here
https://github.com/ninject/ninject/archives/master
https://github.com/ninject/ninject.web.mvc/archives/master
Further more do not use Ninject.MVC3 all you need ist Ninject.Web.Mvc
I also have an article documenting same here

Related

Specflow with Autofac - Unable to load TechTak.Specflow.Infrastructre.IBindingInstanceResolver Version=2.2.0.0

I have a project that uses Autofac for constructor level injection. For every service instance, repositories are injected to constructor.
Now to start using SpecFlow for the project I am not able to instantiate the service for method calls. I have already added Specflow.Autofac plugin but now I am getting following error. I have checked all referenced assemblies versions and those are correct.
I think you are using SpecFlow.Autofac. Is this correct?
If so, it doesn't work with SpecFlow 2.2 at the moment.
For details have a look at this issue: https://github.com/techtalk/SpecFlow/issues/959

VS2017 Could not load file or assembly Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll or one of its dependencies

When trying to open an older solution in VS2017 there is an old Unit Test project that is giving me a problem when building.
I keep getting the following error when building this test project:
Could not load file or assembly 'file:///C:\Projects\MyProj\Test\DAL\UnitTestProj\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll' or one of its dependencies. The system cannot find the file specified.
I checked the project's references and it appears to be referencing Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll. Additionally there are no code errors. How could I ever figure out if it is one of its dependencies that it can't find?
I had a similar issue (with the additional message The "BuildShadowTask" task failed unexpectedly) with a project originally developed with VS2010, and got to spend the last few hours learning about yet another legacy facet of the build process.
There is a good chance that you are dealing with private accessor files (.accessor), which were deprecated in VS2012 (original source). This was foreshadowed in an announcement from the VS2010 team that they were no longer working on these features.
There is also a chance you're just dealing with erroneous refs to the wrong version of UnitTestFramework, but a NuGet restore should fix this. If not, see this GitHub thread for a possible fix (manually change the ref to the public folder), or move to the new MSTest.TestAdapter and MSTest.TestFramework packages (see MSDN support thread).
Solutions
A. Edit the unit test .csproj and change the item Include references from Shadow => None:
<Shadow Include="Test References\namespace.accessor" /> to
<None Include="Test References\namespace.accessor" />
B. Better yet, simply delete all the .accessor files from the unit test project's Test References folder.
Ideally, you would also rewrite your unit tests to remove references to private methods, either by re-architecting to separate concerns or by changing properties to internal and using "friend" with the InternalsVisibleToAttribute.
For those who need to continue supporting testing of private methods for some reason, the same post provides the following suggestions to the logical question "What is available for me then?":
For those who wish to continue testing internal APIs, you have three options:
Use the Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject class to assist in accessing internal and private APIs in your code. This is found in the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll assembly.
Create a reflection framework that would be able to reflect off your code to access internal or private APIs.
If the code you are trying to access is internal, you may be able to access your APIs using the InternalsVisibleToAttribute so your test code can have access to the internal APIs.
However, there is not any good replacement for Code Generation for the new features added by the lanugage teams. You may create the TestMethod stubs and then remove the internal code. You only need to keep the stub itself.
Further reading / sources that helped me piece this together:
VS 2005 ASP.NET explanation of accessors
2008 blog article explaining how to work around this for build servers
MSDN forum thread with discussion on accessor purposes, implementations, and workarounds. Start about 1/3 down.
MSDN BaseShadow docs
MSDN PrivateObject class
Right click the project references folder. Add reference > Assemblies > extensions. Check Microsoft.VisualStudio.QualityTools.UnitTestFramework 10.1, and uncheck any older version.
This is related to Visual studio Enterprise 2015, add new load test was failing: and spiting as "Unable to find assembly 'Microsoft.VisualStudio.QualityTools.LoadTest, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Due to Assembly installed in public assemblies shows as version 10.0.0.0 which is missed in GAC,
GAC had only 10.1.0.0. Once GAC updated with 10.0.0.0 and restart VS 2015. should resolve the issue similar to this.
Some more detail for better reasoning, System Assembly path and project path
DLL path
......\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
.CSProj reference version
I had a same issue while I was upgrading project to .Net4.8 in Visual studio 2022 earlier we were using Visual studio 2017.
Error:
The "BuildShadowTask" task could not be loaded from the assembly ***\Microsoft.VisualStudio.TestTools.BuildShadowsTask.dll. Could not load file or assembly 'file:///***Microsoft.VisualStudio.TestTools.BuildShadowsTask.dll' or one of its dependencies.
Solution : I removed ".accessor" files from project as that is being used for accessing private methods(most probably accessor is depricated). Then we used "PrivateObject" class for accessing private members in UnitTest.
Later we updated Unit Test case. Code references could be found from below articles.
Unit test private methods?
Unit Testing: Exposing Private Members
I had a similar issue (compile project in server Jenkins)
Solution:
Include VS.QualityTools.UnitTestFramework to reference project, whit Pakage Manager:
PM>NuGet\Install-Package VS.QualityTools.UnitTestFramework -Version 15.0.27323.2
https://www.nuget.org/packages/VS.QualityTools.UnitTestFramework
Try to fully uninstall Visual Studio 2017 (not repair). Then download the latest version and install it. Remember to check if MSBuild is added to installation files. Remember to delete folder inside Documents: Documents\Visual Studio 2017. In my case, this simple solution fixed all errors.

Ninject failing to load assembly

I have a system which was using Ninject and loading assemblies using the file pattern kernel load method. Things were working fine with this setup.
A new requirement for the application was to bundle everything into a single exe. We used the technique described here for that. Thus to register everything with Ninject we needed to load the assemblies and call the kernel load for each assembly. However, this would not work. Ninject would always fail even though the binding was registered. I saw it in the debugger as being registered and it worked in the immediate window, but not in the code.
The only solution we found was to manually register each NinjectModule.
Is it possible to use Ninject when the assembly is bundled inside of another one?
The only solution was to manually pass in references to the NinjectModule.

MVC2 with Ninject.Web.Mvc 2.2 and Ninject 2.2 runtime version 4.0 always asks for parameterless constructor for controllers

I have a previous project running Ninject 2.0 runtime version 2.0 and now I am using Ninject in a new project and using the new Ninject, ninject web.mvc version 2.2 for runtime version 4.0.
Every single time I get the error no parameterless constructor
Invalid Operation exception
An error occurred when trying to create a controller of type HomeController'. Make sure that the controller has a parameterless public constructor.
What am I missing. All the bindings are registered.
Do I need to now define interfaces for Controllers as well such as HomeController as IHomeController as I have seen in some examples, Or do I get back to using the older version
There is one version that does not show activation exceptions properly but show this exception instead. Most likely the problem is a duplicated binding.
In addition to what Remo Gloor said, you might want to check that MVC is set up to use Ninject correctly. I was doing some things manually on an older version of the MVC plugin and ended up needing to just bite the bullet and make Global extend the NinjectHttpApplication class, which I had previously been avoiding.
The error you're getting is the error you would get if MVC tries using its built-in controller factory to produce controllers. So you may want to create a custom method binding on your controller class and put a breakpoint inside to make sure it's even being invoked.
You may also want to switch to version 2.3. You can pick up the latest builds of Ninject and all its extensions here.
I have seen this issue mentioned couple of times on forums where there is no direct answer, here is the solution to the above problem, i.e., working with latest ninject
Download the latest Ninject from github.
The ninject I got for MVC2 is named as Ninject.Web.Mvc2-2.2.0.0-release-net-4.0 (runtime version 4)
Now during adding reference add Ninject.Web.Mvc.dll(check the version is same as above by right click properties in VS)
Now Add Ninject.dll from the lib folder in same parent folder (check the version as above)
Now Add CommonServiceLocator.NinjectAdapter.dll from the extensions folder in lib parent folder (check the version as above.)
The missing link in all these have been the commonserviceLocator.dll and the correct version should match. This should be tried if you are sure your bindings are correct as mine were and check to see if your project work with older version.
Thanks to everyone, and good luck :)

Webactivator doesn't run on IIS 7

I have several web applications that make use of packages using WebActivator. On my local machine with IIS 7.5 Express, everything works fine whether I test in Release or Debug configurations. However, on my production machine with IIS 7.5, WebActivator doesn't actually run, so all the modules fail to load and I have to add the code back into the Global.asax.cs file.
I'm stumped as to where to even begin looking - Googled and searched StackOverflow but haven't ran into anyone having similar issues. Is there anything explicit that needs to be configured to allow it to run?
Edit - Added quick sample of activator that logs to Windows. The function contents, when added to the Global.asax.cs file runs fine on the production server, but never logs from the activator.
using System.Web.Mvc;
using System;
[assembly: WebActivator.PreApplicationStartMethod(typeof(Admin.App_Start.WebActivatorTestStart), "Start")]
namespace Admin.App_Start
{
public static class WebActivatorTestStart {
public static void Start() {
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = ".NET Runtime";
log.WriteEntry("WebActivator Start", System.Diagnostics.EventLogEntryType.Information);
}
}
}
Well, I can't say for sure what I did to fix things, but it's working now.
A bit of history - I manage a number different large applications all using some common libraries. I have my common web library and that's where I used to have the IOC setup with Ninject and WebActivator. This base library had the App_Start folder in it. Maybe this was the reason? Dunno. Never got WebActivator to work with this setup so I just used the NinjectHttpApplication to handle registration and startup stuff. However, the base library still had a dependency on WebActivator (just no App_Start folder).
So now I'm working on refactoring some of the applications and the base libraries - clean up a bunch of code smell from the past few months. One step was to move all the IoC up to the actual web application - make the base libraries less monolithic. The base library no longer has any dependency on WebActivator.
And now it works. There also half a hundred other small changes I've made to the base library, so I apologize to others for not being more systematic about it.

Resources