Is it possible to use BackgroundWorker class with .net core?

最后都变了- 提交于 2021-02-08 15:13:25

问题


I am trying to port a C# console project that works fine on Windows to Linux with .NET Core. I have created a project.json file, run dotnet restore and everything seems to work fine. But when I run dotnet build, I get this message :

The type or namespace name 'BackgroundWorker' could not be found (are you missing a using directive or an assembly reference?)

According to .NET Core API, the class BackgroundWorker seems to exist in System.ComponentModel.

Here's my project.json :

"version": "1.0.0-*",
  "buildOptions": {
  "debugType": "portable",
  "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  }

Am I missing something ?

Thanks !


回答1:


You need to include the System.ComponentModel.EventBasedAsync nuget package as a dependency (not the System.ComponentModel nuget package). If you look at the BackgroundWorker.cs file on GitHub, you can see that it's nested under the System.ComponentModel.EventBasedAsync namespace.

Your project.json would look like this:

  "version": "1.0.0-*",
  "buildOptions": {
  "debugType": "portable",
  "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        },
        "System.ComponentModel.EventBasedAsync": "4.3.0"
      },
      "imports": "dnxcore50"
    }
  }


来源:https://stackoverflow.com/questions/41190369/is-it-possible-to-use-backgroundworker-class-with-net-core

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!