gpt4 book ai didi

asp.net-core - .Net 核心 Ping 类 - System.Net.Ping 或 System.Net.Utilities

转载 作者:行者123 更新时间:2023-12-02 03:07:10 35 4
gpt4 key购买 nike

我正在尝试开发一项功能,以在 .Net Core 应用程序中对网络上的 PC 执行 ping 操作。

根据这个StackOverflow ,它说我需要使用 System.Net.Utilities NuGet .

我的项目.json

{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0",
"type": "platform"
},
"BundlerMinifier.Core": "2.2.306",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
"Microsoft.Extensions.Configuration.Json": "1.1.0",
"Microsoft.Extensions.Logging": "1.1.0",
"Microsoft.Extensions.Logging.Console": "1.1.0",
"Microsoft.Extensions.Logging.Debug": "1.1.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
"Microsoft.AspNetCore.Diagnostics": "1.1.0",
"Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
"Microsoft.AspNetCore.Routing": "1.1.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
"Dapper": "1.50.2",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.1.0-preview4-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.1.0-preview4-final",
"Microsoft.AspNetCore.Session": "1.1.0",
"AutoMapper": "5.2.0",
"Microsoft.AspNetCore.Mvc": "1.1.1",
"System.Net.Utilities": "4.0.0-beta-23516"
},

"tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"BundlerMinifier.Core": "2.2.306",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.1.0-preview4-final",
"imports": [
"portable-net45+win8+wp8+wpa81"
]
}
},

"frameworks": {
"netcoreapp1.1": {
"imports": [
"dotnet5.6",
"portable-net45+win81"
]
}
}
}

它添加了库,我可以毫无错误地构建它。但是在运行时,我不断收到此错误:

FileNotFoundException: Could not load file or assembly 'System.Net.Utilities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Unknown location
ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)

但是,如果我使用 System.Net.Ping NuGet ,相同的代码没有任何错误,我可以 ping 主机。

根据 VS2015 Intellisense,它建议添加不起作用的 System.Net.Utilities 包。

我很困惑,无法弄清楚为什么在使用 System.Net.Utilities 时会出现该错误。通常,如果它与 .Net Core 不兼容,我们会直接得到引用错误/编译失败。但这一次完全不同,我根本无法浏览任何页面。

如果以防万一,这是我的 Ping 代码...

public async Task<double> PingPC(string host, int echoNum)
{
long totalTime = 0;
int timeout = 120;
Ping pingSender = new Ping();

for (int i = 0; i < echoNum; i++)
{
var reply = await pingSender.SendPingAsync(host, timeout);
if (reply.Status == IPStatus.Success)
{
totalTime += reply.RoundtripTime;
}
}
return totalTime / echoNum;
}

你能指出我做错了什么吗?谢谢。

最佳答案

要使此版本的 System.Net.Utilities 使用 VS 2015,您必须在导入部分添加 dnxcore50:

"netcoreapp1.1": {
"imports": [
"dnxcore50",
"dotnet5.6",
"portable-net45+win81"
]
}

如果您决定使用 VS 2017,根据这篇文章:http://www.natemcmaster.com/blog/2017/01/19/project-json-to-csproj/#imports ,您必须编辑您的 csproj 并在 PropertyGroup 中添加此行:

<PackageTargetFallback>dnxcore50</PackageTargetFallback>

这里有一个 VS2017 的工作示例:https://github.com/trenoncourt/ping-netcore

关于asp.net-core - .Net 核心 Ping 类 - System.Net.Ping 或 System.Net.Utilities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41988384/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com