在 .net 核心项目中引用项目后无法读取外部程序集
Not able to read the external assembly after project referenced in .net core project
我正在尝试在我的 Asp.Net 核心项目上安装和配置 Autofac。我在名为 CustomerOrder.Web 的 Asp.Net 核心项目中引用了一个名为 CustomerOrder.Data 的项目。我能够看到项目被成功引用,并且可以使用 using 关键字访问命名空间。尽管命名空间中的 class 名称之一变为红色。例如。命名空间
使用 CustomerOrder.Data.基础设施;命名空间中的数据被读取。因此它无法识别我在我的 Web 项目中引用的接口和 classes。这是代码
Project.Json 文件在 CustomerOrder.Data 项目中
{
"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 class
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();
}
}
}
Startup.cs 文件在 CustomerOrder.Web 项目中
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);
}
}
Project.Json CustomerOrder.Web 项目中的文件
{
"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>();
核心版本似乎有问题。有人遇到过这个吗?
我找到了解决这个问题的方法。 Resharper 是罪魁祸首。我禁用了 resharper,现在一切正常。
我正在尝试在我的 Asp.Net 核心项目上安装和配置 Autofac。我在名为 CustomerOrder.Web 的 Asp.Net 核心项目中引用了一个名为 CustomerOrder.Data 的项目。我能够看到项目被成功引用,并且可以使用 using 关键字访问命名空间。尽管命名空间中的 class 名称之一变为红色。例如。命名空间 使用 CustomerOrder.Data.基础设施;命名空间中的数据被读取。因此它无法识别我在我的 Web 项目中引用的接口和 classes。这是代码
Project.Json 文件在 CustomerOrder.Data 项目中
{
"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 class
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();
}
}
}
Startup.cs 文件在 CustomerOrder.Web 项目中
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);
}
}
Project.Json CustomerOrder.Web 项目中的文件
{
"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>();
核心版本似乎有问题。有人遇到过这个吗?
我找到了解决这个问题的方法。 Resharper 是罪魁祸首。我禁用了 resharper,现在一切正常。