IWindsorContainer RegisterControllers Missing Method AddLifestyle error - asp.net-mvc-4

I recently updated a 2.5.2 project to 3.2.1 / MVC 4 recompiling the application after a few code fixes, I am getting a Register Controller issue at run-time. Here is the code:
public static void Register(string key, Type interfaceType, Type implementationType)
{
//IoC.Container.AddComponent(key, interfaceType, implementationType);
// IoC.Container.AddComponent is obsolete. Replaced by:
IoC.Container.Register(Component.For(interfaceType).ImplementedBy(implementationType).Named(key));
}
public static void RegisterControllers(params Assembly[] assemblies)
{
//IoC.Container.RegisterControllers(assemblies);
foreach (Assembly assembly in assemblies)
{
IoC.Container.RegisterControllers(assembly.GetExportedTypes());
}
}
public static void RegisterAllFromAssemblies(string baseAssembly, string relatedAssembly)
{
// AllTypes is obsolete
//
//IoC.Container.Register(AllTypes.
// FromAssemblyNamed(baseAssembly).
// WithService.
// FirstNonGenericCoreInterface(relatedAssembly));
//Update for Castle-Windor 3.2
IoC.Container.Register(Classes.FromAssemblyNamed(baseAssembly)
.Pick().WithServiceFirstInterface()
.LifestylePerWebRequest());
}
}
}
The MissingMethod Exception screen shows after execution of IoC.Container.RegisterControllers(assembly.GetExportedTypes());
"Method not found: 'Castle.Windsor.IWindsorContainer Castle.Windsor.IWindsorContainer.AddComponentLifeStyle(System.String, System.Type, Castle.Core.LifestyleType)'."
Pointing me in the right direction would be helpful

I found this unanswered question meanwhile upgrading a Sharp Architecture based project from SharpArch 1.6 to SharpArch 3.0 and thought a final answer to this might be useful. I got the same problem about
"Method not found: 'Castle.Windsor.IWindsorContainer
Castle.Windsor.IWindsorContainer.AddComponentLifeStyle(System.String,
System.Type, Castle.Core.LifestyleType)'."
and implemented a solution following the suggestion about IWindsorInstaller from
Charleh.
Basically, I created an IWindsorInstaller implementation moving there all my needed IoC dependencies registrations
public class ComponentsInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
AddApplicationServicesTo(container);
//eventually other services to install
}
private void AddApplicationServicesTo(IWindsorContainer container)
{
container.Register(Classes.FromAssemblyNamed("myAssemblyName1").Pick().WithService.FirstInterface());
container.Register(Classes.FromAssemblyNamed("myAssemblyName2").Pick().WithService.FirstInterface());
}
}
and called that installer from Global.asax Application_Start with
IWindsorContainer container = new WindsorContainer();
container.Install(FromAssembly.This());

Related

Simplified approach to IOptions<T>

I am trying to get a .NET Framework class library in line with an ASP.NET Core 2.1 application while using builtin DI mechanism. Now, I created a config class and added appropriate section to appsettings.json:
services.Configure<MyConfig>(Configuration.GetSection("MyConfiguration"));
services.AddScoped<MyService>();
In class lib:
public class MyService
{
private readonly MyConfig _config;
public MyService(IOptions<MyConfig> config)
{
_config = config.Value;
}
}
However, in order to build this classlib I have to add Microsoft.Extensions.Options NuGet package. The problem is that package carries a hell of a lot of dependencies which seem rather excessive to add just for the sake of one interface.
So, the question ultimately is, "is there another approach I can take to configure a DI service located in .NET Framework class library which is not dependency heavy?
Check this article written by Filip Wojcieszyn.
https://www.strathweb.com/2016/09/strongly-typed-configuration-in-asp-net-core-without-ioptionst/
You add extension method:
public static class ServiceCollectionExtensions
{
public static TConfig ConfigurePOCO<TConfig>(this IServiceCollection services, IConfiguration configuration) where TConfig : class, new()
{
if (services == null) throw new ArgumentNullException(nameof(services));
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
var config = new TConfig();
configuration.Bind(config);
services.AddSingleton(config);
return config;
}
}
Apply it in configuration:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.ConfigurePOCO<MySettings>(Configuration.GetSection("MySettings"));
}
And then use it:
public class DummyService
{
public DummyService(MySettings settings)
{
//do stuff
}
}
I bumped into this problem a little while ago, if you can even call it a problem really. I think we all tend to get a little shell-shocked when we see a dependency list like that. But as #Tseng mentioned, it's really not a big deal to include a bunch of extra tiny assemblies (they'll be included in the bin already anyways by virtue of a reference in another project). But I will admit it's annoying to have to include them just for the options interface.
How I solved it was by resolving the service dependency in startup.cs and adjust the service's constructor accordingly:
services.AddTransient<MyService>(Configuration.GetConfiguration("MyConfiguration"));
If you don't care about whatever IOptions provides you, why not just inject IConfiguration into your service?
public class MyService
{
private readonly IConfiguration _config;
public MyService(IConfiguration config)
{
_config = config;
}
public void DoSomething()
{
var value = _config["SomeKey"];
// doing something
}
}

Fody MethodDecorator not working

I am trying to create a method decorator using Fody but it gives me the following error:
I have taken specific care to not wrap my IMethodDecorator inside any namespace as has been mentioned in a lot of places online. Following is the sample code I am trying in a console app.
IMethodDecorator
using System;
using System.Reflection;
public interface IMethodDecorator
{
void OnEntry(MethodBase method);
void OnExit(MethodBase method);
void OnException(MethodBase method, Exception exception);
}
MethodDecoratorAttribute
using System;
using System.Diagnostics;
using System.Reflection;
using FODYPOC;
// Atribute should be "registered" by adding as module or assembly custom attribute
[module: MethodDecorator]
namespace FODYPOC
{
// Any attribute which provides OnEntry/OnExit/OnException with proper args
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)]
public class MethodDecoratorAttribute : Attribute, IMethodDecorator
{
// instance, method and args can be captured here and stored in attribute instance fields
// for future usage in OnEntry/OnExit/OnException
public MethodDecoratorAttribute() { }
public void OnEntry(MethodBase method)
{
Console.WriteLine();
}
public void OnExit(MethodBase method)
{
Console.WriteLine();
}
public void OnException(MethodBase method, Exception exception)
{
Console.WriteLine();
}
}
public class Sample
{
[MethodDecorator]
public void Method()
{
Debug.WriteLine("Your Code");
}
}
}
Can someone point me in the right direction. It looks pretty simple to implement and I know I am making a very silly mistake somewhere.
Apparently the latest version of MethodDecorator.Fody (Version 0.9.0.6 currently) was not working. Downgrading the version to version 0.8.1.1 fixed the issue for me.
After a little more investigation, it appears that the interface method signatures were different in the two versions. So when I had the new package, it was not expecting MethodBase as a parameter and due to not finding anything that matches the interface it expects, it was throwing the error.

Implement FluentSecurity 2.0.0 with Ninject MVC

Error activating ISecurityContext using binding from ISecurityContext to SecurityContext.
I'm getting the above error with FluentSecurity 2.0.0 when I'm trying to configure it with Ninject.Web.Mvc3 in an ASP.NET MVC 4 web application.
I think the internal IoC of FluentSecurity and the Ninject IoC may be clashing. Or I may be incorrectly setting up the DependencyResolver in the SecurityConfigurator.
I need to set it up with IoC as I need to get the UserRoles through an injected class.
public static class SecurityConfig
{
public static ISecurityConfiguration Configure()
{
SecurityConfigurator.Configure(
configuration =>
{
configuration.ResolveServicesUsing(
DependencyResolver.Current.GetServices,
DependencyResolver.Current.GetService);
configuration.DefaultPolicyViolationHandlerIs(() => new DefaultPolicyViolationHandler());
configuration.GetAuthenticationStatusFrom(
() => HttpContext.Current.User.Identity.IsAuthenticated);
configuration.GetRolesFrom(
() =>
((IPersonManager)DependencyResolver
.Current
.GetService(typeof(IPersonManager)))
.GetCurrentUserRoles());
configuration.ForAllControllers().DenyAnonymousAccess();
configuration.For<AdminController>().RequireAnyRole(Role.Administrator);
});
return SecurityConfiguration.Current;
}
}
Where am I going wrong? Is there another way I could achieve this?
I faced the same situation. It happened because Ninject throws an exception when cannot resolve a dependency. I solved it implementing my own ISecurityServiceLocator
public class FluentSecurityServiceLocator : ISecurityServiceLocator
{
public static IKernel Kernel { get; set; }
public object Resolve(Type typeToResolve)
{
return Kernel.TryGet(typeToResolve);
}
public IEnumerable<object> ResolveAll(Type typeToResolve)
{
if (!Kernel.GetBindings(typeToResolve).Any())
{
return new List<object>();
}
return Kernel.GetAll(typeToResolve);
}
}
I passed the kernel instance in my ninject configuration class
FluentSecurityServiceLocator.Kernel = kernel;
Hope this helps!
I'm not really familiar with Ninject but are you sure that DependencyResolver.Current.GetServices and DependencyResolver.Current.GetService won't throw an exception when FluentSecurity asks for something (like ISecurityContext) that is not registered with Ninject?
In structuremap there is a method called TryGetInstance that won't throw an exception when asking for something that is not registered in the container. You can read more on how FluentSecurity and IoC works here:
https://github.com/kristofferahl/FluentSecurity/wiki/IoC-container-integration

SignalR Dependency Resolver not working from Javascript Hubs

I've recently upgraded to the new version of the excellent SignalR library, and moved all my Dependency Injection from StructureMap to Ninject, as Ninject seemed to be better supported.
I've got the dependency injection working fine for Server-side notifications using the "Broadcasting over a Hub from outside of a Hub" described here: https://github.com/SignalR/SignalR/wiki/Hubs.
The problem I'm getting is that all SignalR messages originating from the Javascript hub don't seem to be triggering the dependency injection.
I'm also using MVC4 WebAPI which also takes some shoe-horning to get dependency injection working.
Here's my Hub:
public class PresenceHub : Hub, IPresenceHub
{
private readonly IUserRepository _userRepository;
private readonly IFormsAuthenticationProvider _formsAuthenticationProvider;
public PresenceHub(IFormsAuthenticationProvider formsAuthenticationProvider, IUserRepository userRepository)
{
_userRepository = userRepository;
_formsAuthenticationProvider = formsAuthenticationProvider;
}
public void PresenceChange(string presence)
{
var user = _userRepository.FindById(_formsAuthenticationProvider.GetUserId());
var rosterEntry = Mapper.Map<User, RosterEntryDto>(user);
rosterEntry.Presence = presence;
Clients.updatePresence(rosterEntry);
}
}
Here's my Ninject Bootstrapper:
Namespace SLx.Web.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
public static void Stop()
{
bootstrapper.ShutDown();
}
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
// SignalR Ninject Resolver
GlobalHost.DependencyResolver = new SignalR.Ninject.NinjectDependencyResolver(kernel);
// WebApi Ninject Resolver
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
RegisterServices(kernel);
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
}
}
I'm notifying clients on the serverside via a PresenceProxy defined as follows:
public class PresenceHubProxy : IPresenceHubProxy
{
private readonly IHubContext _hubContext;
public PresenceHubProxy()
{
_hubContext = GlobalHost.ConnectionManager.GetHubContext<PresenceHub>();
}
public void NotifyLogin(RosterEntryDto user)
{
_hubContext.Clients.updatePresence(user);
}
public void NotifyLogout(RosterEntryDto user)
{
_hubContext.Clients.updatePresence(user);
}
}
The Proxy works fine, injected into Controllers or their dependencies, and can send messages to the clients.
When the clients try to call SignalR via Javascript I get the following error:
No parameterless constructor defined for this object.
It looks like Ninject is not being invoked because the dependencies are not being injected into the constructor. What do I need to do to get Dependency Injection working for Javascript calls too?
Update --
Following advice from DFowler, I've replaced the Resolver in PostApplicationStart. Debugging I can see in the Immediate Window that SignalR.GlobalHost.Resolver is of type NinjectDependencyResolver but its still not working I get the same error - no paramaterless constructor.
I've then removed the NinjectDependencyResolver NuGet Library and added the source file to my solution and am using that for debugging purposes. Debugging on GetService and GetServices shows that neither method is ever called in NinjectDependencyResolver.
Any Ideas?
Problem was I hadn't called RouteTable.Routes.MapHubs:
GlobalHost.DependencyResolver = new SignalRNinjectResolver(NinjectWebCommon.Kernel);
RouteTable.Routes.MapHubs(new SignalRNinjectResolver(NinjectWebCommon.Kernel));
From the docs https://github.com/SignalR/SignalR/wiki/Extensibility:
NOTE: DO NOT override the global resolver in PreApplicationStart, it will not work, or it'll work only sometimes. Do it in PostApplicationStart (using WebActivator) or in Global.asax.

Why does the generated NinjectMVC3.cs from NuPack not compile? (or what happened to MvcServiceLocator in ASP.NET MVC 3 Beta? )

Using the NuPack addin and installing the NInject MVC 3 package results in the following compile error in the generated NinjectMVC3.cs file.
The name 'MvcServiceLocator' does not exist in the current context
The sample video David Ebbo posted shows it working just fine at 09:43.
Here is the currently generated class:
public class NinjectMVC3 {
public static void RegisterServices(IKernel kernel) {
//kernel.Bind<IThingRepository>().To<SqlThingRepository>();
}
public static void SetupDependencyInjection() {
// Create Ninject DI Kernel
IKernel kernel = new StandardKernel();
// Register services with our Ninject DI Container
RegisterServices(kernel);
// Tell ASP.NET MVC 3 to use our Ninject DI Container
MvcServiceLocator.SetCurrent(new NinjectServiceLocator(kernel));
}
}
Basically, MvcServiceLocator has gone away. Whenever the video was made there was a mismatch in versions, I guess.
There are excellent explanations available here and here.
The two steps that will make Ninject work are as follows. Replace NinjectMVC3 with the following (I also changed the name which isn't necessary):
public class NinjectResolver : IDependencyResolver
{
private static IKernel kernel;
public NinjectResolver()
{
kernel = new StandardKernel();
RegisterServices(kernel);
}
public static void RegisterServices(IKernel kernel)
{
//kernel.Bind<IThingRepository>().To<SqlThingRepository>();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
}
and add the following line to App_Start() in gloabl.asax.cs
DependencyResolver.SetResolver(new NinjectResolver());
I have fixed the package and uploaded it to the feed. It would be great if you had a chance to try it and verify that it works now. I upped the version of Ninject.MVC3 from 0.1 to 0.2 :)
I've just installed Ninject.MVC3 0.3. I'm using ASP.NET MVC 3 Beta.
I've added this code into my Global.asax.cs file:
public static void RegisterServices(IKernel kernel)
{
kernel.Bind<IProductRepository>().To<SqlProductRepository>();
}
public void SetupDependencyInjection()
{
IKernel kernel = new StandardKernel();
RegisterServices(kernel);
DependencyResolver.SetResolver(new Ninject.Mvc3.NinjectServiceLocator(kernel));
}
And I've added a call to SetupDependencyInjection() into Application_Start() function so it looks like this:
protected void Application_Start()
{
SetupDependencyInjection();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
The IProductRepository and SqlProductRepository are classes that I made in my Models folder and I've added a constructor dependency to my HomeController. Here's the code:
private IProductRepository products;
public HomeController(IProductRepository productRepository)
{
products = productRepository;
}
I've added some breakpoints and ran the application and it works like a charm. Hope this helps.