WebApi 2 -cant确定DataProtection.IDataProtector依赖注入与structuremap

时间:2015-04-26 08:32:24

标签: asp.net-web-api dependency-injection asp.net-mvc-5 structuremap

我使用this beriliant项目作为我的MVC项目的基础。 但是当我使用WebAPI进行项目时,CREATE TABLE #Temp (No INT IDENTITY(1,1), BirthDate varchar(100), Name varchar(250)) INSERT INTO #Temp (Name) SELECT BirthDate, 'Dummy Name' FROM TableB WHERE Name IS NULL SELECT BirthDate, Name FROM FROM TableB WHERE Name IS NOT NULL UNION SELECT BirthDate, Name + CAST(No VARCHAR(100)) FROM FROM #Temp 注入存在问题。 我重新设计了基础并上传here,并添加了一个控制台项目,用于测试使用WebAPI进行授权。

这是结构图初始化:

IDataProtector

在WebApiConfig类DI中是这样的:

            private static readonly Lazy<Container> _containerBuilder =
        new Lazy<Container>(initStructureMap, LazyThreadSafetyMode.ExecutionAndPublication);

    public static IContainer Container
    {
        get { return _containerBuilder.Value; }
    }
return new Container(ioc =>
        {
            ioc.For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use(() => new DbContext());

            ioc.For<IDataSerializer<AuthenticationTicket>>().Use<TicketSerializer>();
            ioc.For<ISecureDataFormat<AuthenticationTicket>>().Use<SecureDataFormat<AuthenticationTicket>>();

        });

在我的启动中,我使用IAppBuilder创建dataprotector:

            var container = StructuremapMvc.Container;
        GlobalConfiguration.Configuration.Services.Replace(
            typeof(IHttpControllerActivator), new  StructureMapHttpControllerActivator(container));

WebApiConfig和IDataProtection在WebApi中不起作用后启动。我的ServiceLayer在单独的项目中,DataProtection需要注入那里。

2 个答案:

答案 0 :(得分:2)

您需要在项目中添加两个类:

1-StructureMapDependencyScope类

using StructureMap;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;

namespace Project.Helpers
{
    public class StructureMapDependencyScope : IDependencyScope
    {
        protected readonly IContainer Container;

        public StructureMapDependencyScope(IContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            this.Container = container;
        }

        public void Dispose()
        {
            this.Container.Dispose();
        }

        public object GetService(Type serviceType)
        {
            if (serviceType == null)
            {
                return null;
            }

            try
            {
                return serviceType.IsAbstract || serviceType.IsInterface
                           ? this.Container.TryGetInstance(serviceType)
                           : this.Container.GetInstance(serviceType);
            }
            catch
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return this.Container.GetAllInstances(serviceType).Cast<object>();
        }
    }
}

2-StructureMapDependencyResolver Class

using StructureMap;
using System.Web.Http.Dependencies;

namespace Project.Helpers
{
    public class StructureMapDependencyResolver : StructureMapDependencyScope, IDependencyResolver
    {
        public StructureMapDependencyResolver(IContainer container)
            : base(container)
        {
        }
        public IDependencyScope BeginScope()
        {
            IContainer child = this.Container.GetNestedContainer();
            return new StructureMapDependencyResolver(child);
        }
    }
}

最后用 WebApiConfig类中的代码替换此代码:

// IoC Config
var container = SmObjectFactory.Container;

// Web API configuration and services
config.DependencyResolver = new StructureMapDependencyResolver(container);

通过StructureMap here详细了解ASP.NET MVC 4和Web API中的依赖注入。

答案 1 :(得分:0)

您需要在容器中明确注册string的实现。

示例配置可能如下所示:

IDataProtector

请记住,此特定配置可能不符合您的确切需求。

希望这有帮助!