小编Cyr*_*and的帖子

Autofac没有使用'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'找到的构造函数

使用可用的服务和参数可以调用在'LMS.Services.Security.EncryptionService'类型上找到'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'的构造函数:无法解析参数'LMS.Models.SecuritySettings securitySettings'的构造函数'Void .ctor(LMS.Models.SecuritySettings)'

这是代码文件

服务类

public class EncryptionService : IEncryptionService
{
    private readonly SecuritySettings _securitySettings;
    public EncryptionService(SecuritySettings securitySettings)
    {
        this._securitySettings = securitySettings;
    }
}
Run Code Online (Sandbox Code Playgroud)

引导程序

private static void SetAutofacContainer()
{
    var builder = new ContainerBuilder();
    builder.RegisterControllers(Assembly.GetExecutingAssembly());
    builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
    builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerRequest();

    builder.RegisterAssemblyTypes(typeof(CourseRepository).Assembly)
           .Where(t => t.Name.EndsWith("Repository"))
           .AsImplementedInterfaces()
           .InstancePerRequest();

    builder.RegisterAssemblyTypes(typeof(CourseService).Assembly)
           .Where(t => t.Name.EndsWith("Service"))
           .AsImplementedInterfaces()
           .InstancePerRequest();

    builder.RegisterFilterProvider();
    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
Run Code Online (Sandbox Code Playgroud)

它早先工作了.但是当我介绍EncryptionService实现时,我收到了上述错误.以下是其他工作代码实现

public class CourseService : ICourseService
{
    #region Fields

    private readonly IRepository<Course> _courseRepository;
    private readonly IUnitOfWork _unitOfWork;

    #endregion

    #region ctor …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection ioc-container inversion-of-control autofac

15
推荐指数
1
解决办法
3万
查看次数

在解析期间确定依赖项的目标类型

似乎无法确定解析依赖项的类型:

containerBuilder.Register(context =>
{
   // What is the type for which this component is resolved?
   var type = default(Type); // TBD
   return context.Resolve<ILoggerFactory>().CreateLogger(type);
});

Run Code Online (Sandbox Code Playgroud)

此处的目标是创建 .NET Core 记录器,该记录器具有适用于它的类型的正确类别。

Autofac 文档中的示例描述了如何使用中间件组件实现这一点,我成功了。但似乎为每个注册添加一个管道会影响性能,而且我还没有发现一种方法来只将管道应用于依赖 ILogger 的组件的注册。

动机:显而易见的选择似乎是将依赖项更改为类型ILogger<T>,其中 T 是应用此依赖项的类型,如下所示:

public class Component
{
    public Component(ILogger<Component> logger)...
}
Run Code Online (Sandbox Code Playgroud)

但是经验告诉我,很多开发者草草复制粘贴组件,忘记更改类型参数,导致日志混乱。在当前代码中,我们仍然使用 Common.Logging,我们的组件只需要一个非泛型 ILog:

public class Component
{
    public Component(ILog log)...
}
Run Code Online (Sandbox Code Playgroud)

在我们之前的 DI 容器 Castle.Windsor 中,它会像这样简单:

public class LoggerSubDependencyResolver : ISubDependencyResolver
{
    public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
    {
        return …
Run Code Online (Sandbox Code Playgroud)

c# autofac

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

是否可以从Autofac容器构建器中删除现有注册?

这些方面的东西:

builder.RegisterType<MyType>().As<IType>();
builder.RegisterType<MyType2>().As<IType>();
builder.DeRegisterType<MyType>().As<IType>()

var container = builder.Build();
var types = container.Resolve<IEnumerable<IType>>();
Assert.IsTrue(types.Count == 1);
Assert.IsTrue(types[0].GetType == typeof(MyType2));
Run Code Online (Sandbox Code Playgroud)

场景:我经历了一堆程序集,当我注册类型时,我想确保我只有一个给定类型的实现.我需要在创建容器之前执行此操作.我可以自己跟踪,但如果Autofac可以帮助我,那将会很好.

c# autofac

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

.NET Core IOC RegisterAssemblyTypes Equivalant

我正在使用.NET核心IOC内置的标准.但我需要一些像AutoFac一样的功能.我希望/需要限制此项目中第三方依赖项的数量.所以希望我能在标准.NET Core IOC中使用类似于AutoFac方法的东西.

builder.RegisterAssemblyTypes(assemblyhere).AsImplementedInterfaces();
Run Code Online (Sandbox Code Playgroud)

这可能吗?

asp.net autofac asp.net-core-mvc asp.net-core

12
推荐指数
2
解决办法
1856
查看次数

Firefox 中的 SEC_ERROR_INADEQUATE_KEY_USAGE 对于 HTTPS=true 和 HOST=xxx 的 React 应用程序

当我指定自定义和SEC_ERROR_INADEQUATE_KEY_USAGE创建的反应应用程序时,我在 Firefox 中遇到错误create-react-appHOSTSSL=true

要重现该问题:

  1. 创建一个新的反应应用程序

    npx create-react-app testssl 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加一个.env文件

    HTTPS=true
    HOST=test.local
    
    Run Code Online (Sandbox Code Playgroud)
  3. 确保test.local映射到127.0.0.1您的主机文件中

    # "C:\Windows\System32\drivers\etc\hosts"
    127.0.0.1 test.local
    
    Run Code Online (Sandbox Code Playgroud)
  4. 启动应用程序

    npm run start
    
    Run Code Online (Sandbox Code Playgroud)

在 chrome 中我有一个security error但我可以绕过它

铬错误

在 Firefox 中,我有一个SEC_ERROR_INADEQUATE_KEY_USAGE,但我找不到绕过它的方法:

火狐错误

Firefox 有没有办法绕过这个错误?

我在windows环境下出现这个错误,不知道linux下是否如此。

https firefox ssl-certificate reactjs create-react-app

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

IoC容器的协变服务解析

我正在为我的应用程序使用依赖注入模式而没有任何IoC容器.现在我决定使用一些IoC容器,因为我的Composition Root由数千行代码组成,但我没能使它与我的类一起使用,它们主动使用方差.例如以下界面

public interface IQuery<in TIn, out TOut>
{
    IReadOnlyCollection<TOut> Get(TIn key);
}
Run Code Online (Sandbox Code Playgroud)

和服务

public class FakeRepository : IQuery<object, string>
{
    public IReadOnlyCollection<string> Get(object key)
    {
        return new[] { key.ToString() };
    }
}
Run Code Online (Sandbox Code Playgroud)

纯DI工作正常

IQuery<string, object> service = new FakeRepository();
Run Code Online (Sandbox Code Playgroud)

但是Autofac和DryIoc都无法解决它.

service = autofacContainer.Resolve<IQuery<string, object>>(); // exception
service = dryIocContainer.Resolve<IQuery<string, object>>(); // exception
Run Code Online (Sandbox Code Playgroud)

我需要一些额外的设置吗?有没有其他IoC容器支持这个?我问得太多了吗?

完整代码:https://dotnetfiddle.net/vlw17R

c# ioc-container autofac dryioc

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

PreserveExistingDefaults 的用途?

在 autofac 注册期间为什么以及何时应该使用 PreserveExistingDefaults?

我已阅读它的使用形式 Autoface 文档: http://docs.autofac.org/en/latest/register/registration.html

但我的问题是,什么时候我们会用单个接口注册多个实现。

任何人都可以举一些实时的例子吗?

.net dependency-injection autofac

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

Autofac等待模块可用

因为无法保证解决模块的顺序,所以我遇到了一些问题:

我有一个注册到一个模块ScheduleServiceScheduleService是负责在设定的时间间隔等的触发事件

我可以加载不同的IScheduable项目,我这样做使用XML Configuration.我遇到的问题是,IScheduable物品需要IScheduleService准备好才能注册它自己.

所以在我的<autofac><modules>身上

<module type="Namespace.ScheduleServiceModule, Namespace" />
Run Code Online (Sandbox Code Playgroud)

然后我的想法是我可以装入尽可能多的不同ISchedulable物品

<module type="SomeNamespace.ScheudleItem1, SomeNamespace />
<module type="SomeNamespace.ScheudleItem2, SomeNamespace />
<module type="SomeNamespace.ScheudleItem3, SomeNamespace />
<module type="SomeNamespace.ScheudleItem4, SomeNamespace />
Run Code Online (Sandbox Code Playgroud)

目前这是我在那些scheduleitem模块中的方式:

protected override void Load(ContainerBuilder builder)
{
    builder.RegisterCallback(registry =>
    {
        var scheduleService = new TypedService(typeof(IScheduleService)); 
        var registrations = registry.RegistrationsFor(scheduleService);
        if (registrations != null && registrations.Any())
        {
            IComponentRegistration componentRegistration = registrations.First();
            componentRegistration.Activated += (sender, args) =>
            {
                IScheduleService scheduleService …
Run Code Online (Sandbox Code Playgroud)

autofac

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

Autofac 枚举不适用于多个类型或模块的注册

autofac文档状态:

当 Autofac 注入类型为构造函数的参数时,IEnumerable<ITask>它不会查找提供IEnumerable<ITask>. 相反,容器将找到 ITask 的所有实现并注入所有这些实现。

但实际上,它添加每个已注册类型的次数与已注册的次数相同。因此,如果您按以下方式注册一个课程两次:

builder.RegisterType<A>(); 
builder.RegisterType<A>(); 
Run Code Online (Sandbox Code Playgroud)

然后你在枚举中得到两个项目!!在单个模块中,这不是问题,因为您显然会注意仅注册一次您的类型。但是如果你有一个被多个模块注册的共享模块(典型的菱形模块依赖关系图),那么你在枚举中得到的项与共享模块已被其他人注册的一样多......

这是一个错误吗?有没有办法强制枚举为每个实现提供单个项目,如文档中所述,不再有?

c# autofac autofac-module

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

如何获取 dotnet core 3 的路由属性?

我已将 dotnet core 2.2 升级到 3.preview 7。
因此之后,我无法获取自定义属性。
context.Resource在版本 2.2 中是类型 of AuthorizationFilterContext,但在版本 3 中是类型 of Microsoft.AspNetCore.Http.Endpoint

现在我无法从端点获取属性。

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Gamma.Core.Security
{
    public abstract class AttributeAuthorizationHandler<TRequirement,     TAttribute>
    : AuthorizationHandler<TRequirement> where TRequirement
    : IAuthorizationRequirement where TAttribute : Attribute
    {
        Microsoft.AspNetCore.Http.IHttpContextAccessor _httpContextAccessor = null;
        public AttributeAuthorizationHandler(Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement)
        {
            var attributes = new …
Run Code Online (Sandbox Code Playgroud)

c# authorization routedata .net-core .net-core-3.0

5
推荐指数
2
解决办法
3751
查看次数