我正在尝试在我的Asp.Net Core项目上安装和配置Autofac。我在我的Asp.Net Core项目CustomerOrder.Web中引用了一个名为CustomerOrder.Data的项目。我能够看到项目成功引用,并且可以使用using关键字访问命名空间。虽然命名空间中的一个类名变为红色。例如。命名空间 使用CustomerOrder.Data.Infrastructure;读取命名空间中的数据。因此,它无法识别我在我的Web项目中引用的接口和类。这是代码
CustomerOrder.Data项目中的Project.Json文件
{
"version": "1.0.0-*",
"dependencies": {
"CustomerOrder.Model": "1.0.0-*",
"Microsoft.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore.Relational": "1.0.1",
"Microsoft.Extensions.Caching.Abstractions": "1.1.0",
"NETStandard.Library": "1.6.1"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
对于CustomerOrder.Data项目中的DBFactory类
namespace CustomerOrder.Data.Infrastructure
{
public class DbFactory : Disposable, IDbFactory
{
CustomerOrderEntities _dbContext;
private DbContextOptions<CustomerOrderEntities> _options;
public CustomerOrderEntities Init()
{
_options = new DbContextOptions<CustomerOrderEntities>();
return _dbContext ?? (_dbContext = new CustomerOrderEntities(_options));
}
protected override void DisposeCore()
{
_dbContext?.Dispose();
}
}
}
CustomerOrder.Web项目中的Startup.cs文件
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
// Create the container builder.
var builder = new ContainerBuilder();
// Register dependencies, populate the services from
// the collection, and build the container. If you want
// to dispose of the container at the end of the app,
// be sure to keep a reference to it as a property or field.
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>();
builder.RegisterType<DbFactory>().As<IDbFactory>();
builder.RegisterType<CustomerRepository>().As<ICustomerRepository>();
builder.RegisterType<ProductRepository>().As<ICustomerRepository>();
builder.RegisterType<OrderRepository>().As<IOrderRepository>();
builder.Populate(services);
this.ApplicationContainer = builder.Build();
// Create the IServiceProvider based on the container.
return new AutofacServiceProvider(this.ApplicationContainer);
}
}
CustomerOrder.Web项目中的Project.Json文件
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0",
"type": "platform"
},
"Microsoft.AspNetCore.AngularServices": "1.0.0-beta-000019",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.1.0-preview4-final",
"type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.CommandLine": "1.1.0",
"Microsoft.Extensions.Logging": "1.1.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Autofac.Extensions.DependencyInjection": "4.0.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore.Relational": "1.1.0",
"CustomerOrder.Data": "1.0.0-*",
"CustomerOrder.Model": "1.0.0-*",
"CustomerOrder.Service": "1.0.0-*"
},
"tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
"Microsoft.DotNet.Watcher.Tools": "1.1.0-preview4-final"
},
"frameworks": {
"netcoreapp1.6": {
"imports": [
"dnxcore50",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"appsettings.json",
"ClientApp/dist",
"node_modules",
"Views",
"web.config",
"wwwroot"
]
},
"scripts": {
"prepublish": [
"npm install",
"node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod",
"node node_modules/webpack/bin/webpack.js --env.prod"
],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
},
"tooling": {
"defaultNamespace": "CustomerOrder.Web"
}
}
由于某种原因,项目无法理解UnitOfWork,IUnitOfWork,DbFactory,IDbFactory等
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>();
builder.RegisterType<DbFactory>().As<IDbFactory>();
builder.RegisterType<CustomerRepository>().As<ICustomerRepository>();
builder.RegisterType<ProductRepository>().As<ICustomerRepository>();
builder.RegisterType<OrderRepository>().As<IOrderRepository>();
核心版本似乎存在一些问题。以前有人遇到过这个吗?
答案 0 :(得分:0)
我找到了解决这个问题的方法。 Resharper是罪魁祸首。我禁用了resharper,现在一切正常。