问题标签 [autofac-configuration]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
126 浏览

c# - Autofac ApiController 过滤具有属性的方法

目标是使用 Autofac 将少量 Attr 转换为 DI 过滤器.AsWebApiAuthorizationFilterFor<ApiController>(c => c.GetType()

感觉我很亲密,但很可能只是做错了,发现了一堆关于 peram 注射器的帖子,但这不是我想要在这里做的。

当前失败的尝试

目标:为所有 api 控制器和/或具有特定属性的控制器方法添加此过滤器

根据提供的答案创建的解决方案:

  1. 创建自定义IAutofacAuthorizationFilter并将 ATTR 中的当前过滤器逻辑移动到该接口的 auth 方法。
  2. 为 autofac 添加了呼叫启用过滤器builder.RegisterWebApiFilterProvider(config);
  3. 注册我的过滤器

    /li>
  4. 在控制器或方法上添加对原始属性的检查,如果两者都是 null 我返回。

    /li>

到目前为止,这似乎可以解决问题并满足我的要求。

0 投票
0 回答
22 浏览

asp.net - 在 autofac 注册控制器中查看注册 api url 的方法

在尝试调试 api 端点命中时,我通过邮递员获得的唯一信息是服务器上不存在带有消息文件或目录的 404(顺便说一下 IIS)。

我希望有一种方法可以通过 Autofac 注册控制器查看注册的端点。它是 ContainerBuilder 的一部分,代码如下:

0 投票
2 回答
148 浏览

c# - Mvc 4 to 5 upgrade issue - how to resolve Autofac security rules violation

I upgraded my asp.net web app (targetFramework 4.5.2) from mvc 4 to 5 according to this link. The solution compiles without errors however at runtime I get:

The inheritance security rules were violated when the member "Autofac.Integration.Mvc.AutofacDependencyResolver.GetService (System.Type)" was overwritten. The security access of the overriding method must match the security access of the overwritten method.

I searched the forum but none of the related posts could help me to pin down the origin of the error. Trying to upgrade Autofac.Mcv4 to 5 (as suggested in the posts) failed (-> Autofac.Mcv5 does not target frameWork 4.5.2). Changing the targetFramework to 4.6 and 4.6.1 also did not allow to install Autofac.Mcv5.

Below I have listed the relevant packages installed in the main project:

Also note, that the solution has several projects (two have references to Autofac). Any help to pin down the origin of or to solve the error would be appreciated.

0 投票
1 回答
185 浏览

c# - AutofacWebApiDependencyResolver 方法不存在

我正在关注本教程Youtube DI进行依赖注入。这对我来说很清楚,但是它没有显示如何使用 n 层架构在 Web API 上实现。我还包括了本教程:C-Sharp AutoFac Web API 教程

这是我的代码的样子:

我在这里遇到的问题是缺少 AutofacWebApiDependencyResolver。

这是我放置代码以调用 AutofacWebapiConfig 类的地方

这是一个全新的 web api 核心,具有 3 个类库,做一个 n 层架构。任何帮助,将不胜感激。是否在正确的位置调用它,我是否需要创建第三类。

我还在 app_start 文件夹中添加了一个分类引导程序,并修改了 Global.asax 以添加一行 Bootstrapper.Run();

0 投票
2 回答
86 浏览

c# - Autofac 程序集扫描注册期间未找到程序集错误

我计划在我的项目中自动连接接口和存储库,包括 BL 和 DAL。但我遇到了一个我不知道如何解决的问题。

这是代码

这是我的项目文件夹结构

在此处输入图像描述

并得到这个错误

在此处输入图像描述

我做错了什么。任何帮助,将不胜感激。

0 投票
2 回答
2113 浏览

asp.net-web-api - 在 ASP.NET CORE 3.1 Web API 中配置 Autofac DI 容器和来自控制器的消费者服务

我需要在 ASP.NET CORE 3.1 Web API 应用程序中配置 Autofac DI 容器,并从 Web API 控制器中的容器调用注册类。我安装 Autofac.Extensions.DependencyInjection (6.0.0) 并尝试在我的 Startup.cs 类中注册容器,但我无法使用服务。另外,我需要在 ConfigureServices(IServiceCollection services) 类中配置容器吗?调试器在命中点 builder.RegisterModule(new IoCConfigurator()); 后没有命中 IoCConfigurator() 类;

程序.cs

启动.cs

IoCConfigurator.cs

INotification 接口和类

用户和用户服务类

我想对 UserService 类进行分类并从 DI 容器获取引用的 API 控制器

0 投票
1 回答
1378 浏览

asp.net-core - .NET Core 3.0 中 Autofac 模块的依赖注入

我对创建和注册模块很陌生,但我试图更好地理解整个过程。目前我正在尝试将依赖项注入到 autofac 模块中,但我认为问题在于 autofac 在依赖项注入发生之前注册了它的模块。这是我的安全模块:

这是我在 program.cs 中注册模块的部分:

这是我收到的错误。有没有办法指定这个模块应该在以后注册?还是我错过了重要的信息?

DependencyResolutionException:无法使用可用的服务和参数调用类型“inQuba.CX.Web.Api.AutofacIntegration.SecurityModule”上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数:无法解析参数“Microsoft。构造函数“Void .ctor(Microsoft.Extensions.Configuration.IConfiguration)”的 Extensions.Configuration.IConfiguration 配置。

0 投票
1 回答
120 浏览

autofac - 在 .NETCore3.1 中用 Autofac 替换 CastleWindsor

我在我的 ASP.NETCore2.2 WebAPI 项目中使用 CastleWindsor 并且工作正常。我现在正在迁移到 ASP.NETCore3.1,它看起来不像 CastleWindor 对此有官方支持,所以我决定迁移到 Autofac 进行最小的更改,但在解决依赖关系时遇到了一些问题。

在我的项目中,我在应用程序的不同层之间保持非常松散的耦合,即业务层、数据层和翻译层。所有这些层都在它们自己的程序集中。然后在我的主项目中,我有一个文件夹说“依赖项”,它将保存不同层的所有 DLL。此外,我有一个单独的项目,其中列出了由不同层实现的所有接口,这些接口需要由 IoC 容器解决。

具有所有接口的项目如下所示:

实施项目如下所示:

在我迁移的 .netcore3.1 项目中使用 Autofac,Startup.cs 如下所示:

DependencyResolver 是一个继承自 Autofac.Module 的类,它再次位于不同项目的单独程序集中,如下所示:

我收到此错误并且无法修复它:“:”尝试激活“POC.Person.Controllers”时无法解析“Shared.Interfaces.IBusinessLayer`1[Models.Person]”类型的服务.PersonController'。","

在我的控制器内部,我进行了如下注入:

Program.cs 看起来像这样:

看起来 autofac 涉及泛型,注册和解析类型不是那么简单吗?

0 投票
1 回答
45 浏览

c# - 哪个生命周期最适合在 ASP.Net MVC 中使用带有 Autofac 的实体框架?

我是 Autofac 的新手,我将它用作依赖容器。我想注册我的数据库上下文。这就是我所做的

但是在 .net Core 中,我需要将我的 DBContext 注册为Scoped. 在此处注册我的实体是否正确,SingleInstance或者我应该更改它?

0 投票
1 回答
60 浏览

c# - 使用显式实例解析类型

我使用 AutoFac。我必须使用从另一个服务获得的显式实例来解析类型。

例如:我有一个类型的实例,Client它是从某个地方(而不是容器)获得的。

我想配置 Autofac 容器,以便始终在Client解析类型对象时,它应该返回我的实例。

问题是我当时没有这个实例,当我用Containerbuilder- 配置容器时,我不能使用例如LambdaRegistration.

是否有其他解决方案可以解决我的问题?