我netstandard2.1
在netcoreapp3.0
Web应用程序中使用库。在中添加服务时Startup
,出现以下错误:
'无法从程序集'Microsoft.AspNetCore.Mvc.Formatters.Json,版本= 3.0.0.0加载类型'Microsoft.AspNetCore.Mvc.MvcJsonOptions'
我还在Microsoft.AspNetCore.Mvc
类库中使用了2.2.0包中的某些功能。
这是我的图书馆.csproj
,
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
</ItemGroup>
</Project>
这是ServiceExtensions
我图书馆的课,
public static class ServiceExtensions
{
public static IMvcBuilder AddMyLibrary(this IMvcBuilder builder)
{
builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
builder.Services.ConfigureOptions<ConfigureLibraryOptions>();
return builder;
}
}
这是我的ConfigureLibraryOptions
课,
public class ConfigureLibraryOptions : IConfigureOptions<MvcOptions>
{
public void Configure(MvcOptions options)
{
options.ModelBinderProviders.Insert(0, new CustomBinderProvider());
}
}
这是ConfigureServices
来自Startup
,
services.AddControllersWithViews().AddMyLibrary();
请帮助我为什么会出现此错误,并协助解决该问题?
netstandard2.1
参考Microsoft.AspNetCore.Mvc
2.2.0的类库netcoreapp3.0
?相反,我应该将更netstandard2.1
改为netcoreapp3.0
并添加FrameworkReference
到Microsoft.AspNetCore.App
?