System.PlatformNotSupportedException:“Operation is not supported on this platform.”
vs2019创建.net core3.1 的控制台应用程序
执行以下代码:
using System;
using System.Diagnostics;
using System.Threading; namespace ConsoleApp3
{
/// <summary>
/// 委托必须和要调用的异步方法有相同的签名
/// </summary>
/// <param name="callDuration">sleep时间</param>
/// <param name="threadId">当前线程id</param>
/// <returns></returns>
public delegate string AsyncMethodCaller(int callDuration, out int threadId);
class Program
{ /// <summary>
/// 主函数
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
AsyncMethodCaller caller = new AsyncMethodCaller(TestMethodAsync);
int threadid = ;
//开启异步操作
IAsyncResult result = caller.BeginInvoke(, out threadid, null, null);
for (int i = ; i < ; i++)
{
Console.WriteLine("其它业务" + i.ToString());
}
//调用EndInvoke,等待异步执行完成
Console.WriteLine("等待异步方法TestMethodAsync执行完成");
string res = caller.EndInvoke(out threadid, result);
Console.WriteLine("Completed!");
Console.WriteLine(res);
Console.Read();
}
/// <summary>
/// 与委托对应的方法
/// </summary>
/// <param name="callDuration"></param>
/// <param name="threadId"></param>
/// <returns></returns>
static string TestMethodAsync(int callDuration, out int threadId)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine("异步TestMethodAsync开始");
for (int i = ; i < ; i++)
{ // 模拟耗时操作
Thread.Sleep(callDuration);
Console.WriteLine("TestMethodAsync:" + i.ToString());
}
sw.Stop();
threadId = Thread.CurrentThread.ManagedThreadId;
return string.Format("耗时{0}ms.", sw.ElapsedMilliseconds.ToString());
}
}
}
IAsyncResult result = caller.BeginInvoke(3000, out threadid, null, null);
所在行提示错误

System.PlatformNotSupportedException
HResult=0x80131539
Message=Operation is not supported on this platform.
Source=ConsoleApp3
StackTrace:
at ConsoleApp3.AsyncMethodCaller.BeginInvoke(Int32 callDuration, Int32& threadId, AsyncCallback callback, Object object)
at ConsoleApp3.Program.Main(String[] args) in C:\Users\pxm\source\repos\ConsoleApp3\ConsoleApp3\Program.cs:line
原因
The Asynchronous Programming Model (APM) (using IAsyncResult and BeginInvoke) is no longer the preferred method of making asynchronous calls. The Task-based Asynchronous Pattern (TAP) is the recommended async model as of .NET Framework 4.5. Because of this, and because the implementation of async delegates depends on remoting features not present in .NET Core, BeginInvoke and EndInvoke delegate calls are not supported in .NET Core. This is discussed in GitHub issue dotnet/corefx #5940.
异步编程模型(APM)(使用IAsyncResult和BeginInvoke)不再是异步调用的优选方法。从.NET Framework 4.5开始,基于任务的异步模式(TAP)是推荐的异步模型。因此,而且由于异步委托的实现取决于远程处理但.NET Core不存在的功能,BeginInvoke和EndInvoke委托调用不.NET Core支持。GitHub问题 dotnet/corefx#5940 中对此进行了讨论
Async delegates are not in .NET Core for several reasons:
- Async delegates use deprecated IAsyncResult-based async pattern. This pattern is generally not supported throughout .NET Core base libraries, e.g. System.IO.Stream does not have IAsyncResult-based overloads for Read/Write methods in .NET Core.
- Async delegates depend on remoting (System.Runtime.Remoting) under the hood. Remoting is not in .NET Core - implementation of async delegates without remoting would be challenging.
解决办法
改为基于任务的异步模式Task.Run
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks; namespace ConsoleApp3
{
/// <summary>
/// 委托必须和要调用的异步方法有相同的签名
/// </summary>
/// <param name="callDuration">sleep时间</param>
/// <param name="threadId">当前线程id</param>
/// <returns></returns>
public delegate string AsyncMethodCaller(int callDuration, out int threadId);
class Program
{ /// <summary>
/// 主函数
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
AsyncMethodCaller caller = new AsyncMethodCaller(TestMethodAsync);
int threadid = ;
//开启异步操作
//IAsyncResult result = caller.BeginInvoke(3000, out threadid, null, null);
var workTask = Task.Run(() => caller.Invoke(, out threadid)); for (int i = ; i < ; i++)
{
Console.WriteLine("其它业务" + i.ToString());
}
//调用EndInvoke,等待异步执行完成
Console.WriteLine("等待异步方法TestMethodAsync执行完成");
//string res = caller.EndInvoke(out threadid, result);
string res = workTask.Result; Console.WriteLine("Completed!");
Console.WriteLine(res);
Console.Read();
}
/// <summary>
/// 与委托对应的方法
/// </summary>
/// <param name="callDuration"></param>
/// <param name="threadId"></param>
/// <returns></returns>
static string TestMethodAsync(int callDuration, out int threadId)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine("异步TestMethodAsync开始");
for (int i = ; i < ; i++)
{ // 模拟耗时操作
Thread.Sleep(callDuration);
Console.WriteLine("TestMethodAsync:" + i.ToString());
}
sw.Stop();
threadId = Thread.CurrentThread.ManagedThreadId;
return string.Format("耗时{0}ms.", sw.ElapsedMilliseconds.ToString());
}
}
}
参考:
1.Migrating Delegate.BeginInvoke Calls for .NET Core
System.PlatformNotSupportedException:“Operation is not supported on this platform.”的更多相关文章
- 解决 cmake_symlink_library: System Error: Operation not supported
在编译uchardet时遇到这个错误: cmake_symlink_library: System Error: Operation not supported 创建链接不成功,要确认当前帐户下是否有 ...
- firefox同步ajax请求报错的问题 A parameter or an operation is not supported by the underlying object
今天在测试系统时,一个很正常的功能在firefox下报错,经过验证在ie和chrome浏览器中功能这个正常. 调试后发现: 请求比其他请求的特殊点在于同步请求. 经过firefox的控制台上测 ...
- Eclipse进度条出现“Remote System Explorer Operation”解决方法
Eclipse进度条出现“Remote System Explorer Operation”解决方法
- 【转载】Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法
原来是eclipse后台进程在远程操作,就是右下角显示的“Remote System Explorer Operation”.折腾了半天,在Stack Overflow找到答案(源地址).把解决方案翻 ...
- ORA-27300: OS system dependent operation:sendmsg failed with status: 105 ORA-27301: OS failure message: No buffer space available
早上查看数据库alert日志,发现如下ORA-报错: kgxpvfynet: mtype: 61 process 6460 failed because of a resource problem i ...
- Remote System Explorer Operation总是运行后台服务,卡死eclipse
阿里云 > 教程中心 > android教程 > Remote System Explorer Operation总是运行后台服务,卡死eclipse Remote System E ...
- Remote System Explorer Operation在eclipse后台一直跑 解决办法
在用eclipse开发时,经常遇到卡死的情况,其中一种就是右下角出现:“Remote System Explorer Operation”,解决方案如下: 第一步:Eclipse -> Pref ...
- Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法
当你右键编辑控件的id或者其他属性时都会卡很久,发现原来是eclipse后台进程在远程操作,就是右下角显示的“Remote System Explorer Operation”.折腾了半天,在Stac ...
- eclipse remote system explorer operation
Remote System Explorer Operation卡死Eclipse解决方案 - 披萨大叔的博客 - CSDN博客https://blog.csdn.net/qq_27258799/ar ...
随机推荐
- 「雕爷学编程」Arduino动手做(8)——湿度传感器模块
37款传感器和模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器与模块,依照实践出真知(动手试试)的理念,以学习和交流为目的,这里准备 ...
- webpack指南(六)命令行环境配置
webpack 命令行环境配置中,通过设置 --env 可以使你根据需要,传入尽可能多的环境变量.在 webpack.config.js 文件中可以访问到这些环境变量. webpack --env.N ...
- Hyperledger Fabric——balance transfer(三)创建和加入Channel
详细解析blance transfer示例的创建通道(Channel)和加入节点到通道的过程. 创建Channel 1.首先看app.js的路由函数 var createChannel = requi ...
- C语言基础知识(一)——关键字
存储类别说明符变量:auto.register.static.extern._Thread_local.typedef 存储类型限定符:const.volatile.restrict._Atomic ...
- 【Java】手把手理解CAS实现原理
先来看看概念,[CAS] 全称“CompareAndSwap”,中文翻译即“比较并替换”. 定义:CAS操作包含三个操作数 —— 内存位置(V),期望值(A),和新值(B). 如果内存位置的值与期望值 ...
- MyBatis—— org.apache.ibatis.binding.BindingException: Type interface com.web.mybatis.i.PersonMapper is not known to the MapperRegistry.
报错信息: Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interfac ...
- wordpress批量修改域名SQL
UPDATE wow_options SET option_value = REPLACE(option_value, 'https://wooooooow.cn' ,'http://wooooooo ...
- 循序渐进VUE+Element 前端应用开发(3)--- 动态菜单和路由的关联处理
在我开发的很多系统里面,包括Winform混合框架.Bootstrap开发框架等系列产品中,我都倾向于动态配置菜单,并管理对应角色的菜单权限和页面权限,实现系统对用户权限的控制,菜单一般包括有名称.图 ...
- [SD心灵鸡汤]005.每月一则 - 2015.09
精彩的激励人生的话都是从成功者嘴里说出来的,他们成功的过程我们仅仅知道一二,结果却是名满天下.既然看这个激励语,就是想要成功的人,所以大家好好读懂,然后执行,那么你离成功就不远了! 1.万里寻山历百艰 ...
- [Objective-C] 010_Foundation框架之NSSet与NSMutableSet
在Cocoa Foundation中的NSSet和NSMutableSet ,和NSArray功能性质一样,用于存储对象属于集合.但是NSSet和NSMutableSet是无序的, 保证数据的唯一性, ...