标签: .net-standard

如何使用 Microsoft.Extensions.Configuration 将值保存到 JSON 配置文件中?

按照此示例,我尝试使用Microsoft.Extensions.Configuration API 中的IConfiguration接口检索并保存启动应用程序值。但是,我找不到将这些值更改为设置 JSON 文件的方法。

作为示例,请考虑以下appSettings.json配置文件:

"Setting":
{
  "Startup":
   {
      "IsFirstStart": true
   }
}
Run Code Online (Sandbox Code Playgroud)

A 负责提取的关联类:

public class AppHandler
{
#region fields

  // MS heper contains configuration parameters from JSON file
  private IConfiguration _appConfiguration = null;

  // MS config builder
  private IConfigurationBuilder _appBuilder = null;

  // Is it software first use
  private bool _isFirstStart;

#endregion fields

#region Constructor

private AppHandler()
{
  _appBuilder = new ConfigurationBuilder()
     .SetBasePath(Directory.GetCurrentDirectory())
     .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
     .AddJsonFile($"appsettings.dev.json", optional: …
Run Code Online (Sandbox Code Playgroud)

.net c# json config .net-standard

5
推荐指数
1
解决办法
5135
查看次数

.Net Framework 和 .Net Core 使用的 .Net Standard 2.0 dll 中的 NuGet 包

设置

假设我有一个 .Net Standard 2.0 类库项目,我向其中添加了一个与 .Net Standard 2.0 兼容的 Nu NuGet 包。

然后,我从 .Net Framework 控制台项目和 .Net Core 控制台项目引用该类库项目。

用图片重述一下:

通过 DLL 的 NuGet

问题

每个控制台应用程序如何处理为其应用程序类型获取正确的 NuGet 代码?

笔记

注意:我使用 Microsoft.Extensions.DependencyInjection 进行了尝试,它在 .Net Core 3.1 控制台应用程序中工作正常,但在 .Net Framework 4.7.2 控制台应用程序中抛出“文件未找到”异常(寻找依赖注入DLL)。这让我相信 .Net Standard 2.0 NuGets 确实是 .Net Core NuGets...

注意事项:我试图了解这里发生的情况,而不是解决“找不到文件”问题。(通过引用 .Net Framework 4.7.2 控制台应用程序中的 Microsoft.Extensions.DependencyInjection NuGet 可以轻松修复此问题)。

c# nuget .net-standard .net-standard-2.0

5
推荐指数
1
解决办法
2181
查看次数

带有 netstandard2.0 引用的 .net 4.8 项目将无法通过 msbuild 构建

该解决方案在 VS2019 中构建得很好,但是当我们的构建代理上的 msbuild 获取我们的解决方案时,它会抱怨对我们的几个 dll 的引用。
我刚刚添加了对 NuGet 包的引用,该包引入了 netstandard 2.0 依赖项,因此这似乎是问题所在。
我注意到,如果我进入构建服务器并在 VS FIRST 中构建解决方案......那么相同的 msbuild 命令行随后将会成功。因此,在正常构建情况下,这些 netstandard dll 不会被构建,以便它们可以包含在其他项目编译中。

F:\Agents\MyAgent_work\18\s\src\WS_Reports.metaproj :警告 MSB3268:无法解析主要引用“F:\Agents\MyAgent_work\18\b\My.Project\My.Project.dll”,因为它间接依赖于框架程序集“netstandard,Version=2.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”,在当前目标框架中无法解析。“.NETFramework,版本=v4.8”。要解决此问题,请删除引用“F:\Agents\MyAgent_work\18\b\My.Project\My.Project.dll”,或将应用程序重新定位到包含“netstandard, Version=2.0.0.0,文化=中性,PublicKeyToken=cc7b13ffcd2ddd51”。

这是 msbuild 抛出错误的项目的解决方案文件声明...可以看到对错误中显示的 dll 的项目引用(上面的示例)。请注意,这个项目和我将新的 netstandard2.0 包添加到目标框架 v4.8 的项目

Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "WS.Reports", "WebServices\WS.Reports\", "{C47BAAE6-0708-4D43-83B8-92405B31A3E0}"
    ProjectSection(WebsiteProperties) = preProject
        SccProjectName = "SAK"
        SccAuxPath = "SAK"
        SccLocalPath = "SAK"
        SccProvider = "SAK"
        TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.8"
        ProjectReferences = "{2222173A-112A-44F8-A614-C7CCBDACEFE8}|My.Project.Bus.dll;{222924FE-877B-4E18-B960-94DED3DB6DF2}|MyProject.Protocols.Shared.dll;{222F5846-55E6-4541-B43B-E49674963E02}|MyProject.AuthProtocols.dll;{2224BC03-6C7E-4E15-B158-D9BD03D0E8B3}|My.Project.dll;{222BAF47-1C9F-4A56-9152-B4240A188612}|My.Project.Images.dll;{222FE788-27B1-4B03-B052-BCFFF251C15A}|My.Project.Legacy.dll;"
        Debug.AspNetCompiler.VirtualPath = "/WS.Reports"
        Debug.AspNetCompiler.PhysicalPath = "WebServices\WS.Reports\"
        Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\WS.Reports\"
        Debug.AspNetCompiler.Updateable = "true"
        Debug.AspNetCompiler.ForceOverwrite = "true"
        Debug.AspNetCompiler.FixedNames = "false"
        Debug.AspNetCompiler.Debug …
Run Code Online (Sandbox Code Playgroud)

msbuild .net-standard

5
推荐指数
1
解决办法
2564
查看次数

避免 .Net Standard 中的非跨平台依赖关系

我将现有库从 .Net Framework 4.7.2 移植到 .Net Standard 2.0,以使其跨平台,但仍保留与 .Net Framework 应用程序的兼容性。

我不明白如何避免非跨平台依赖关系

我的库使用动态类型Settings.Designer.cs。

为了在 .Net Standard 中支持它,我必须添加对NuGet包的引用,这反过来又添加了对其他一些包(如 )的依赖项,即使我不使用任何与安全相关的代码。Microsoft.CSharpSystem.Configuration.ConfigurationManagerSystem.Security.Principal.Windows

最后两个显然是Microsoft.Windows.Compatibility包的一部分。

NuGet 依赖项

  1. 如何查明这些依赖项是否跨平台(即 Windows 平台扩展的一部分)?

当我转到.NET API 浏览器时,我看到它Microsoft.Csharp不在 .Net Standard 2.0 中,但它在 .Net Platform Extensions 2.1 及更高版本中。这是否意味着这个包不是跨平台的?是否有其他服务可以检查 NuGet 包的平台支持?

.NEt API 结果

  1. 当我在 .Net Core 2.1 上使用我的库创建一个简单的测试应用程序并将其发布到 Linux 平台时dotnet publish -c Release --self-contained true --runtime linux-x64,该应用程序实际上在 Linux 计算机上运行良好。这是否意味着我的库是完全跨平台的,我可以在生产中安全地使用它,或者我System.PlatformNotSupportedException将来可能仍然会在 Linux 上遇到?

c# cross-platform .net-standard

5
推荐指数
0
解决办法
134
查看次数

Visual Studio 2019 16.11 - 无法在 netstandard 2.1 项目中更新或创建 WCF 服务引用

我不确定它什么时候消失,但我无法再更新我的服务引用,这是全新的,我像一个月前一样在以前的 VS 版本中更新了它们,没有任何问题......

诺卡夫

现在cf2

它位于 Xamarin Android 项目的解决方案中,服务引用位于 .netstandard 2.1 项目中。

另外,如果我尝试从服务引用管理器手动添加它,则会弹出奇怪的错误,并且它们没有意义,因为使用 dotnet-svcutil 正在工作:

wcf43

c# visual-studio asp.net-core .net-standard

5
推荐指数
1
解决办法
2453
查看次数

在 .NET 7 中使用 ExecutionContext.SuppressFlow() 时出现异常

从HttpClient执行SendAsync Method时,我的 WebApi 客户端项目丢失当前用户的WindowsIdentity

\n

HttpContext.User返回应用程序池标识。不是模拟的WindowsIdentity

\n

客户端项目是.NET Standard,因此可以在.NET Framework和.NET Core中使用。

\n

使用 RestSharp 时,一切都可以正常工作,并且不需要ExecutionContext.SuppressFlow() 。(我可以\xc2\xb4t使用RestSharp,因为客户端是由NSwag生成的。)

\n

.NET框架:

\n

使用ExecutionContext.SuppressFlow()时,身份将正确委托给服务器。

\n

.NET 核心:

\n

当使用.NET 7时,将抛出以下异常:

\n
\n

无法在空上下文上调用 Set

\n
\n

例外:

\n
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\nat System.Security.Principal.WindowsIdentity.RunImpersonatedInternal(SafeAccessTokenHandle token, Action action)\nat System.Security.Principal.WindowsIdentity.GetName()\nat System.Net.Http.CurrentUserIdentityProvider.GetIdentity()\nat System.Net.Http.HttpConnectionPoolManager.GetConnectionKey(HttpRequestMessage request, Uri proxyUri, Boolean isProxyConnect)\nat System.Net.Http.HttpConnectionPoolManager.SendAsyncCore(HttpRequestMessage request, Uri proxyUri, Boolean async, Boolean doRequestAuth, Boolean isProxyConnect, CancellationToken cancellationToken)\nat System.Net.Http.HttpConnectionPoolManager.SendAsync(HttpRequestMessage request, Boolean async, Boolean …
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core .net-standard

5
推荐指数
1
解决办法
92
查看次数

.NETStandard1.6 PCL出错:"您的项目未引用".NETPlatform,Version = v5.0"框架"

为了帮助重现该问题,需要执行以下步骤在Visual Studio 2015中创建.NETStandard1.6 PCL:

  1. 创建一个新的类库(适用于iOS,Android和Windows).
  2. 在"属性"页面中,单击"Target .NET Platform Standard".
  3. 将.NETStandard表单.NETStandard1.1更改为.NETStandard1.6
  4. 构建这个空项目.

发生以下错误:

>C:\Program Files (x86)\MSBuild\Microsoft\NuGet\Microsoft.NuGet.targets(140,5): error : Your project is not referencing the ".NETPlatform,Version=v5.0" framework. Add a reference to ".NETPlatform,Version=v5.0" in the "frameworks" section of your project.json, and then re-run NuGet restore.
Run Code Online (Sandbox Code Playgroud)

以下是project.json:

{
  "supports": {},
  "dependencies": {
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.6": {}
  }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供一个关于如何在"框架"部分中添加对".NETPlatform,Version = v5.0"的引用的提示吗?我尝试了以下方法:

"donet5.4":{}

"donet5.0":{}

两者都不起作用.

.net .net-standard

4
推荐指数
1
解决办法
1837
查看次数

从.NET标准库获取Windows系统目录

似乎是一个简单的问题,但谷歌似乎没有给我一个答案.

在.NET Framework中,我们可以使用Environment.SystemDirectory获取系统目录(看起来像这样C:\WINDOWS\System32),但当前版本的.NET Standard或.NET Core中不存在该属性.有没有办法在.NET标准(或失败的.NET Core)库中获取该文件夹?

为了我的目的,不要求调用在非Windows平台上返回一些有用的东西(无论如何它会返回 /lib什么 /usr/lib?等等),虽然我觉得它会很酷.

现在似乎我最好的选择是尝试C:\WINDOWS\System32直接使用,如果不存在,那么尝试使用C:\WinNT\System32,但感觉就像这样的黑客做到这一点.

c# windows .net-core .net-standard

4
推荐指数
1
解决办法
1102
查看次数

针对.NET Core 2.0

我只是用VS2017预览2.1创建一个简单的ClassLibrary项目.我使用.NET Core项目模板创建.当我检查项目细节时,我发现它的目标是.NET标准2.0.我如何针对.NET Core 2.0(根据使用API​​-Port工具进行的测试实现大量API)

.net c# .net-core .net-standard

4
推荐指数
1
解决办法
1659
查看次数

.NET Standard 2.0 NuGet包生成

我正在使用针对多个.NET框架的.NET类库项目,现在在我的Windows 10机器中下载了.NET Core 2.0 SDK后,我也试图将其.NET Standard 2.0作为目标.

<TargetFrameworks>net45;net46;netstandard1.0;netstandard1.3;netstandard2.0</TargetFrameworks>

问题是当编译后由Visual Studio(2017)生成的NuGet包通过NuGet Package Explorer打开时,它显示没有依赖关系..NET Standard 2.0

Screenshot1 Screenshot2

它应该不是NETStandard.Library 2.0.0作为依赖?

如果有必要(至少在VS发布更新之前),在NuSet包中为NETStandard.Library 2.0.0明确指定了依赖关系?

为了确保它与我的项目不是一个孤立的案例,我创建了另一个项目并添加了Newtonsoft.Json作为依赖项.结果仍然相同.

Screenshot3

.net visual-studio nuget .net-standard

4
推荐指数
1
解决办法
345
查看次数