在我的项目中,我将Autofac(MVC + WebApi)与Serilog一起使用。我有谷歌,但很幸运。在我的家庭控制器中,我得到“没有为此对象定义无参数构造函数”。任何建议
请帮助我查找问题。我对这两种技术都是新手
public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureLogging();
var builder = new ContainerBuilder();
var config = new HttpConfiguration();
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// Web API configuration and services - Dependency Injection
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterSource(new ViewRegistrationSource());
builder.RegisterType<Log.Logger>.As<ILogger>();
builder.RegisterInstance(Log.Logger).AsImplementedInterfaces();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
}
private static void ConfigureLogging()
{
bool serilogEnabled;
bool.TryParse(WebConfigurationManager.AppSettings["SERILOG:SerilogEnabled"], out serilogEnabled);
if (serilogEnabled)
{
int retainedFileCountLimit;
if (!int.TryParse(WebConfigurationManager.AppSettings["SERILOG:RetainedFileCountLimit"], out retainedFileCountLimit))
retainedFileCountLimit = 10;
var logFilePath = WebConfigurationManager.AppSettings["SERILOG:LogFilePath"];
if (!logFilePath.EndsWith("\\"))
logFilePath += "\\";
var host = Environment.MachineName;
var serilogUrl = WebConfigurationManager.AppSettings["SERILOG:SeqUrl"] ?? "http://localhost:15900";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.WithProperty("Host", host)
.Enrich.FromLogContext()
.WriteTo.RollingFile((logFilePath + host + "-abc-log-{Date}.txt"), retainedFileCountLimit: retainedFileCountLimit)
.WriteTo.Seq(serilogUrl)
.CreateLogger();
}
Logger = Log.Logger;
}
}
public class HomeController : Controller
{
protected readonly ILogger Logger;
public HomeController(ILogger logger)
{
Logger = logger;
}
}
错误
[MissingMethodException:没有为此定义的无参数构造函数 对象。] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型, 布尔publicOnly,布尔noCheck,布尔&canBeCached, RuntimeMethodHandleInternal&ctor,Boolean&bNeedSecurityCheck)+0
System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔 skipCheckThis,布尔值fillCache,StackCrawlMark和stackMark)+119
System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly, 布尔skipCheckThis,布尔fillCache,StackCrawlMark和stackMark) +232 System.Activator.CreateInstance(类型,布尔型非公共)+83 System.Activator.CreateInstance(类型类型)+11 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext) requestContext,类型为controllerType)+55[InvalidOperationException:尝试创建一个 类型的控制器 'DET.PRISM.FISO.Web.App.Controllers.HomeController'。确保 控制器具有无参数的公共构造函数。]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext,类型controllerType)+178
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext,类型controllerType)+76
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,字符串controllerName)+88
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController&控制器,IControllerFactory&工厂) +194 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback回调,对象状态)+48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext 上下文,AsyncCallback cb,对象extraData)+16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep步骤)+48 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,布尔值和已同步完成)+159