相关疑难解决方法(0)

MVC WebApi不使用AutofacWebApiDependencyResolver

我有一个混合的MVC 4应用程序,其中一些控制器是常规实现,Controller一些控制器是实现ApiController.我也在使用Autofac进行DI,但似乎WebApi控制器"激活器"机器(缺少更具体的术语)没有使用Autofac解析器(AutofacWebApiDependencyResolver),这导致在我提出请求时抛出异常对我的一个api控制器.这是错误:

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>
        Type 'MyApp.Areas.Api.Controllers.MyController' does not have a default constructor
    </ExceptionMessage>
    <ExceptionType>System.ArgumentException</ExceptionType>
    <StackTrace>
        at System.Linq.Expressions.Expression.New(Type type) 
        at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) 
        at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) 
        at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    </StackTrace>
</Error>
Run Code Online (Sandbox Code Playgroud)

这是我如何设置它:

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
Run Code Online (Sandbox Code Playgroud)

我对Autofac.WebApi集成的理解是,上面是让WebApi使用Autofac解析器的唯一要求,那么可能会发生什么?

旁注:我能想到的唯一一个愚蠢的部分可能就是我在MVC区域中有WebApi控制器,这是不受支持的DefaultHttpControllerSelector,因此实现了一个自定义控制器(相对于Andrew Malkov) .但是,我并不认为这会对解析器有任何影响,因为选择器只返回一个稍后在激活中使用的类型.

asp.net-mvc autofac asp.net-web-api

10
推荐指数
1
解决办法
3595
查看次数

AutoFac+WebAPI 不处理对象

我有以下代码来构建依赖项:

private static void InitializeContainer(HttpConfiguration config)
{
    var builder = new ContainerBuilder();
    var controllers = AssemblyUtils.GetAssemblies(true);

    controllers.Add(Assembly.GetExecutingAssembly());

    builder.RegisterApiControllers(controllers.ToArray()).PropertiesAutowired();

    builder
        .RegisterAssemblyTypes(
            new List<Assembly>(AssemblyUtils.GetAssemblies(false))
            {
                Assembly.GetExecutingAssembly()
            }.ToArray())
        .Where(t =>
            t.GetCustomAttributes(typeof(IocContainerMarkerAttribute), false).Any() ||
            t.IsSubclassOf(typeof(HandlerAspectAttribute)) ||
            typeof(ICoreContract).IsAssignableFrom(t))
        .AsImplementedInterfaces().InstancePerDependency()
        .PropertiesAutowired().OwnedByLifetimeScope();

    var container = builder.Build();

    GlobalConfiguration.Configuration.DependencyResolver =
       new AutofacWebApiDependencyResolver(container);

    config.DependencyResolver =
        GlobalConfiguration.Configuration.DependencyResolver;
}
Run Code Online (Sandbox Code Playgroud)

处理程序:

public class ArchieveDocumentCommandHandler 
    : IHandler<ArchieveDocumentCommand>, IDisposable
{
    public IServiceMessageDispatcher Dispatcher { get; set; }
    public IDocumentRepository DocumentRepository { get; set; }
    public IFileSystemProvider FileSystemProvider { get; set; }
    public ICoreSettingRepository CoreSettingRepository { …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection autofac asp.net-web-api2

1
推荐指数
1
解决办法
1599
查看次数