Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

IoC Autofac setup #88

Closed
martosource opened this issue May 18, 2015 · 4 comments
Closed

IoC Autofac setup #88

martosource opened this issue May 18, 2015 · 4 comments

Comments

@martosource
Copy link

I have tried to setup FluentSecurity with Autofac.

I have got it to a point where is it complaining that it cannot resolve the constructor for SecurityContext.

Is there an example for using FluentSecurity with Autofac?

@tiesont
Copy link

tiesont commented May 18, 2015

I'm assuming you followed http://www.fluentsecurity.net/wiki/IoC-container-integration ? SecurityContext is setup to be a singleton, so you can't construct one directly.

@martosource
Copy link
Author

On review Autofac and Common ServiceLocator extensions are throwing an exception as I would like to keep the FluentSecurity defaults.

Using the following code:

config.ResolveServicesUsing(
                    type =>
                    {
                        object obj; 

                        container.TryResolve(type, out obj);

                        return new List<object>{obj};
                    },
                    type =>
                    {
                        object obj;
                        container.TryResolve(type, out obj);

                        return obj;});

Not sure if it is the best way, but it is working.

@kristofferahl
Copy link
Owner

@trustorm I haven't used autofac so I can't tell you the best way. However, you do not need to specify the second argument of ResolveServicesUsing as only the first one is required. Also, you should make sure you return an empty list of objects if TryResolve results in null.

@tiesont
Copy link

tiesont commented May 18, 2015

Another option, if you've configured a custom DependencyResolver (by calling System.Web.Mvc.DependencyResolver.SetResolver in, say, the Global.asax file), is to implement the FluentSecurity.ISecurityServiceLocator interface like so:

public class FluentSecurityServiceLocator : ISecurityServiceLocator
{
    public object Resolve(Type typeToResolve)
    {
        return DependencyResolver.Current.GetService(typeToResolve);
    }

    public IEnumerable<object> ResolveAll(Type typeToResolve)
    {
        return DependencyResolver.Current.GetServices(typeToResolve).Cast<object>();
    }
}

Then you can override the internal service locator Fluent Security provides as shown in the wiki I linked to previously:

SecurityConfigurator.Configure(configuration =>
{
    configuration.ResolveServicesUsing(new FluentSecurityServiceLocator());
});

I use Castle Windsor, though, so not sure how well this integrates with Autofac.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants