我知道在 ASP.NET Core 中你可以使用GetAutofacRoot
:
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
// If, for some reason, you need a reference to the built container, you
// can use the convenience extension method GetAutofacRoot.
this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
}
...但这在使用通用主机时似乎不可行。我正在尝试注册一个 ServiceLocator:
// Perform registrations and build the container.
var container = builder.Build();
// Set the service locator to an AutofacServiceLocator.
var csl = new AutofacServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => csl);