按照此示例,我尝试使用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 Standard 2.0 类库项目,我向其中添加了一个与 .Net Standard 2.0 兼容的 Nu NuGet 包。
然后,我从 .Net Framework 控制台项目和 .Net Core 控制台项目引用该类库项目。
用图片重述一下:
每个控制台应用程序如何处理为其应用程序类型获取正确的 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 可以轻松修复此问题)。
该解决方案在 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) 我将现有库从 .Net Framework 4.7.2 移植到 .Net Standard 2.0,以使其跨平台,但仍保留与 .Net Framework 应用程序的兼容性。
我不明白如何避免非跨平台依赖关系。
我的库使用动态类型和Settings.Designer.cs。
为了在 .Net Standard 中支持它,我必须添加对NuGet包的引用,这反过来又添加了对其他一些包(如 )的依赖项,即使我不使用任何与安全相关的代码。Microsoft.CSharp
System.Configuration.ConfigurationManager
System.Security.Principal.Windows
最后两个显然是Microsoft.Windows.Compatibility包的一部分。
当我转到.NET API 浏览器时,我看到它Microsoft.Csharp
不在 .Net Standard 2.0 中,但它在 .Net Platform Extensions 2.1 及更高版本中。这是否意味着这个包不是跨平台的?是否有其他服务可以检查 NuGet 包的平台支持?
dotnet publish -c Release --self-contained true --runtime linux-x64
,该应用程序实际上在 Linux 计算机上运行良好。这是否意味着我的库是完全跨平台的,我可以在生产中安全地使用它,或者我System.PlatformNotSupportedException
将来可能仍然会在 Linux 上遇到?我不确定它什么时候消失,但我无法再更新我的服务引用,这是全新的,我像一个月前一样在以前的 VS 版本中更新了它们,没有任何问题......
它位于 Xamarin Android 项目的解决方案中,服务引用位于 .netstandard 2.1 项目中。
另外,如果我尝试从服务引用管理器手动添加它,则会弹出奇怪的错误,并且它们没有意义,因为使用 dotnet-svcutil 正在工作:
从HttpClient执行SendAsync Method时,我的 WebApi 客户端项目丢失当前用户的WindowsIdentity。
\nHttpContext.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\n无法在空上下文上调用 Set
\n
例外:
\nat 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) 为了帮助重现该问题,需要执行以下步骤在Visual Studio 2015中创建.NETStandard1.6 PCL:
发生以下错误:
>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 Framework中,我们可以使用Environment.SystemDirectory
获取系统目录(看起来像这样C:\WINDOWS\System32
),但当前版本的.NET Standard或.NET Core中不存在该属性.有没有办法在.NET标准(或失败的.NET Core)库中获取该文件夹?
为了我的目的,不要求调用在非Windows平台上返回一些有用的东西(无论如何它会返回 /lib
什么 /usr/lib
?等等),虽然我觉得它会很酷.
现在似乎我最好的选择是尝试C:\WINDOWS\System32
直接使用,如果不存在,那么尝试使用C:\WinNT\System32
,但感觉就像这样的黑客做到这一点.
我只是用VS2017预览2.1创建一个简单的ClassLibrary项目.我使用.NET Core项目模板创建.当我检查项目细节时,我发现它的目标是.NET标准2.0.我如何针对.NET Core 2.0(根据使用API-Port工具进行的测试实现大量API)
我正在使用针对多个.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
它应该不是NETStandard.Library 2.0.0作为依赖?
如果有必要(至少在VS发布更新之前),在NuSet包中为NETStandard.Library 2.0.0明确指定了依赖关系?
为了确保它与我的项目不是一个孤立的案例,我创建了另一个项目并添加了Newtonsoft.Json作为依赖项.结果仍然相同.
.net-standard ×10
c# ×7
.net ×5
.net-core ×3
nuget ×2
asp.net-core ×1
config ×1
json ×1
msbuild ×1
windows ×1