简体   繁体   中英

Injected object is always null in custom authorize attribute

Following is my MEF Dependency Resolver

public class MEFDependencyResolver : IDependencyResolver, System.Web.Http.Dependencies.IDependencyResolver
{
    private readonly CompositionContainer _container;

    public MEFDependencyResolver(CompositionContainer container)
    {
        if (container == null) throw new ArgumentNullException("container");

        _container = container;
    }

    public object GetService(Type serviceType)
    {
        if (serviceType == null) throw new ArgumentNullException("serviceType");

        var name = AttributedModelServices.GetContractName(serviceType);

        return Enumerable.Any(_container.Catalog.Parts.SelectMany(part => part.ExportDefinitions), e => e.ContractName == name) ? _container.GetExportedValue<object>(name) : null;
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        if (serviceType == null) throw new ArgumentNullException("serviceType");

        var name = AttributedModelServices.GetContractName(serviceType);

        return _container.GetExportedValues<object>(name);
    }

    public System.Web.Http.Dependencies.IDependencyScope BeginScope()
    {
        return this;
    }

    public void Dispose()
    {
    }

}

Following is my class which I want to inject.

 [Export(typeof(IClientService))]
public class ClientService : IClientService { }

Following is my custom authorize attribute.

public class CustomAuthorize : AuthorizeAttribute
{
    [Import]
    protected IClientService ClientService { get; set; }

    public string Status { get; set; }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var isAuthorized = base.AuthorizeCore(httpContext);
        if (!isAuthorized)
        {
            return false;
        }

        return IsControllerAccessible(httpContext.User); 
    }

    private bool IsControllerAccessible(IPrincipal user)
    {
        var client = ClientService.GetClient();//ClientService object is null here.            

        return true;
    }
}

The object ClientService which is of type IClientService is always null. It seems that the dependency resolver, MEF in this case is not able to resolve the dependency for custom attributes.

Whats the actual problem here?

Guide me.

I faced the same issue earlier and I ended up using the below:

private IClientService _ClientService ;
    public IClientService ClientServiceObj
    {
        get
        {
            return _ClientService ??
                   (_ClientService = DependencyResolver.Current.GetService<IClientService>());
        }
        set { _ClientService = value; }
    }

And then using ClientServiceObj to ClientService functions. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM