简体   繁体   English

Webapi环境中的Sharprepository Autofac InstancePerApiRequest无法正常工作

[英]Sharprepository Autofac InstancePerApiRequest in Webapi environment not working

Does anyone have a working example of sharprepository intergration with autofac using InstancePerApiRequest for DbContext? 有没有人使用InstancePerApiRequest for DbContext与autofac进行Sharprepository集成的工作示例?

I am registering my dbcontext thusly: 我这样注册我的dbcontext:

builder.RegisterType<AuditTestEntities>().As<DbContext>().InstancePerApiRequest();

If I remove the InstancePerApiRequest, sharprepository is able to get a dbcontext. 如果删除InstancePerApiRequest,sharprepository可以获取dbcontext。 But with the InstancePerApiRequest, I get the error message pasted below. 但是,通过InstancePerApiRequest,我收到了下面粘贴的错误消息。 Basically the crux of the error is, I suspect, the way sharprepository makes the call: 我怀疑,错误的症结在于,sharprepository进行调用的方式:

No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. 从请求实例的范围中看不到带有匹配“ AutofacWebRequest”标签的范围。 This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself. 通常,这表明SingleInstance()组件正在请求按HTTP请求注册的组件(或类似方案)。在Web集成下,始终从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime请求依赖项,而不是从容器本身请求依赖项。

The full error stack: 完整的错误堆栈:

iisexpress.exe Error: 0 : Operation=DefaultHttpControllerActivator.Create, Exception=System.InvalidOperationException: An error occurred when trying to create a controller of type 'AccountController'. iisexpress.exe错误:0:Operation = DefaultHttpControllerActivator.Create,Exception = System.InvalidOperationException:尝试创建“ AccountController”类型的控制器时发生错误。 Make sure that the controller has a parameterless public constructor. 确保控制器具有无参数的公共构造函数。 ---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor()' on type 'AccountRepository'. ---> Autofac.Core.DependencyResolutionException:调用类型为“ AccountRepository”的构造函数“ Void .ctor()”时引发了异常。 ---> Could not resolve type 'System.Data.Entity.DbContext' using the 'AutofacDependencyResolver'. --->无法使用“ AutofacDependencyResolver”解析类型“ System.Data.Entity.DbContext”。 Make sure you have configured your Ioc container for this type. 确保已为此类型配置了Ioc容器。 View the InnerException for more details. 查看InnerException以获取更多详细信息。 (See inner exception for details.) ---> SharpRepository.Repository.Ioc.RepositoryDependencyResolverException: Could not resolve type 'System.Data.Entity.DbContext' using the 'AutofacDependencyResolver'. (有关详细信息,请参阅内部异常。)---> SharpRepository.Repository.Ioc.RepositoryDe​​pendencyResolverException:无法使用“ AutofacDependencyResolver”解析类型“ System.Data.Entity.DbContext”。 Make sure you have configured your Ioc container for this type. 确保已为此类型配置了Ioc容器。 View the InnerException for more details. 查看InnerException以获取更多详细信息。 ---> Autofac.Core.DependencyResolutionException: No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. ---> Autofac.Core.DependencyResolutionException:从请求实例的范围中看不到带有匹配“ AutofacWebRequest”标签的范围。 This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself. 通常,这表明SingleInstance()组件正在请求按HTTP请求注册的组件(或类似方案)。在Web集成下,始终从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime请求依赖项,而不是从容器本身请求依赖项。

Okay found the issue. 好的,找到了问题。 There is a problem with using the SharpRepository AutofacDependencyResolver when using the MVC or Web API integration and trying to use the scope InstancePerApiRequest or InstancePerHttpRequest. 使用MVC或Web API集成并尝试使用范围InstancePerApiRequest或InstancePerHttpRequest时,使用SharpRepository AutofacDependencyResolver存在问题。 Autofac expects those items to be resolved from the System.Web.DependencyResolver.Current instead of from the Autofac IContainer directly as the AutofacDependencyResolver is currently doing. Autofac希望这些项目将从System.Web.DependencyResolver.Current解析,而不是像AutofacDependencyResolver当前所做的那样直接从Autofac IContainer解析。

Here is how you can fix the issue right now until we make an overload for AutofacDependencyResolver that fixes the issue. 在我们为AutofacDependencyResolver进行重载来解决该问题之前,您可以立即采用以下方法解决该问题。

You will need to create your own dependency resolver within your project like this one: 您将需要在您的项目中创建自己的依赖项解析器,如下所示:

public class CustomAutofacDependencyResolver : BaseRepositoryDependencyResolver
{
    private readonly IDependencyResolver _resolver;

    public CustomAutofacDependencyResolver(IDependencyResolver resolver)
    {
        _resolver = resolver;
    }

    protected override T ResolveInstance<T>()
    {
        return _resolver.GetService<T>();
    }

    protected override object ResolveInstance(Type type)
    {
        return _resolver.GetService(type);
    }
}

And then register it with SharpRepository so it will use it to resolve the DbContext and then it will work as expected. 然后在SharpRepository中注册它,以便它将使用它来解析DbContext,然后它将按预期工作。

RepositoryDependencyResolver.SetDependencyResolver(new CustomAutofacDependencyResolver(DependencyResolver.Current));

** Update** I was testing with MVC and able to replicate the error and fix it but that doesn't work with Web API. **更新**我正在使用MVC进行测试,并且能够复制并修复该错误,但不适用于Web API。 I am used to using StructureMap where it works fine using the GlobalConfiguration.Configuration.DependencyResolver. 我习惯使用GlobalMap.Configuration.DependencyResolver在正常运行的StructureMap。

It seems the issue is that Autofac needs a IDependencyScope that you can access from the HttpRequestMessage but I'm not seeing a way to get to that outside of the ApiController. 看来问题在于Autofac需要一个IDependencyScope,您可以从HttpRequestMessage访问该IDependencyScope,但是我没有找到在ApiController之外访问该方法的方法。 This describes the issue and the reason: https://groups.google.com/forum/#!msg/autofac/b3HCmNE_S2M/oMmwFE5uD80J 这说明了问题及其原因: https : //groups.google.com/forum/#! msg/ autofac/ b3HCmNE_S2M/ oMmwFE5uD80J

Unfortunately right now I'm at a bit of a loss on the best way to handle this. 不幸的是,现在我对处理此问题的最佳方法有些茫然。 But I'll keep thinking about it. 但我会继续考虑。

So, I was able to get mine working by changing the lifetime scope to InstancePerLifetimeScope . 因此,我可以通过将生存期范围更改为InstancePerLifetimeScope来使我的工作正常。 I don't know whether this has any unforeseen consequences or not. 我不知道这是否会带来不可预见的后果。 Everything appears to be working fine for me so far. 到目前为止,一切似乎对我来说都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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