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

当我尝试使用Xml配置设置参数时,出现以下错误:

没有在类型上使用'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'找到的构造函数
可以使用可用的方法调用“ LM.AM.Core.Services.EmailService”
服务和参数:无法解析参数'System.String
构造函数'Void .ctor(System.String)'的testSmtp'。

以下是相关文件:

web.config

  <configSections>
    <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration" />
  </configSections>

  <autofac>
    <components>
      <component type="LM.AM.Core.Services.EmailService , LM.AM.Core" service="LM.AM.Core.Infrastructure.Services.IEmailService , LM.AM.Core.Infrastructure">
        <parameters>
          <parameter name="testSmtp" value="abc" />
        </parameters>
      </component>
    </components>
  </autofac>

服务等级

public class EmailService : IEmailService
{
    public string _testSmtp;

    public EmailService (string testSmtp)
    {
        _testSmtp = testSmtp;
    }
}

注册

builder.RegisterType<EmailService>().As<IEmailService>().SingleInstance();

Global.asax

var builder = new ContainerBuilder();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));

builder.RegisterModule<Core.ModuleInstaller>();

builder.RegisterControllers(typeof(MvcApplication).Assembly);
AutofacContainer.Container = builder.Build();

var emailSvc = AutofacContainer.Container.Resolve<IEmailService>();

我已经检查了容器是否知道xml参数,并且已尽可能接近Wiki,但是由于某种原因,该参数无法在唯一的构造函数上解析,因此我收到了以上错误。

这应该很简单。谁能提供一些有关我的建议
可以尝试使它正常工作吗?

参考方案

您已将EmailService重新注册了两次。

一次在web.config中,一次与

builder.RegisterType<EmailService>().As<IEmailService>().SingleInstance();

如果Core.ModuleInstaller中有上述行,则它将覆盖web.config配置。并且因为在这里您没有指定参数Autofac引发异常。

因此,要解决此问题,只需从EmailService模块中删除Core.ModuleInstaller注册。

如果您在多个地方使用Core.ModuleInstaller,并且需要在那里注册EmailService,则需要更改模块加载的顺序:

var builder = new ContainerBuilder();
builder.RegisterModule<Core.ModuleInstaller>();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));

或告诉Autofac不要覆盖EmailService的注册(如果PreserveExistingDefaults已经存在):

builder.RegisterType<EmailService>().As<IEmailService>()
       .SingleInstance().PreserveExistingDefaults();

如何从'pandas.core.frame.DataFrame'中消除第一列 - python

我有以以下格式输出的代码。我应该如何删除第一列并可以将第二行的元素存储在列表中?输出类型为'pandas.core.frame.DataFrame'格式 speed lat lng 1 19.130506 12.616756 7.460664 2 63.595894 52.616838 7.460691 3 40.740044 72.616913 7.460…

将字符串分配给numpy.zeros数组[重复] - python

This question already has answers here: Weird behaviour initializing a numpy array of string data                                                                    (4个答案)         …

TypeError:'str'对象不支持项目分配,带有json文件的python - python

以下是我的代码import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data['john']['use…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…