带有 .NET Core 的 Azure 队列存储 WebJob

Azure Queue Storage WebJob with .NET Core

我有 2 个项目,它们都是 .NET 核心。一个定义架构、模型、命令、查询等的基础设施项目,以及一个由 endpoints/controllers 组成的 API 来处理数据。整个设置驻留在 Azure 上,数据库是 Azure SQL.

该系统用于分析,因此整个设置基于只读 API 等等。但是,我显然需要将数据插入数据库。数据来自我的环境之外的软件,所以我的想法是创建一个 WebJob,它将在 Azure 队列上接收有效负载,使用 NewtonSoft JSON 来填充我的模型并将其插入数据库。

如果我创建一个普通的 Azure WebJob,它将使用与 .NET Core 不兼容的 .NET 版本,因此我将我的项目设置为 here。为了设置我的 Web 作业,我需要对其进行配置:

    public static void Main(string[] args)
    {
        var config = new JobHostConfiguration
        {
            DashboardConnectionString = "",
            StorageConnectionString = ""
        };

        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }

为了使用RunAndBlock();以及我需要引用 mscorlib 的配置,这是我在 project.json:

中所做的
{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.Azure.WebJobs": "1.1.2",
    "Microsoft.Azure.WebJobs.Core": "1.1.2",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.3"
    },
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "Microsoft.WindowsAzure.ConfigurationManager": "3.2.3",
    "WindowsAzure.Storage": "8.0.1"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dnxcore50",
        "net451"
      ]
    }
  },

  "publishOptions": {
    "include": [
      "run.cmd"
    ]
  },

  "runtimes": {
    "win10-x64": {}
  }
}

阅读 here 关于 "Microsoft.NETCore.Portable.Compatibility" 包,它说:

As such, when using this package you may encounter errors like

error CS0012: The type 'WebRequest' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Net.Requests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

这正是我得到的,

Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The located assembly's manifest definition does not match the assembly reference.

但是,我不完全确定如何解决这个问题。我对 foo 的依赖性不是很强,我不确定我是否能够让 Azure 队列存储 + webjobs 与 .NET 核心很好地配合

如果可以修复,我该如何修复?

根据您的情况,我尝试为我的 WebJob 构建 NETCore 控制台应用程序,但我可能会遇到与您提供的相同的错误。然后我发现一些类似issue and issue about WebJob SDK in NETCore, and as David Ebbo的表述如下:

To clarify, Azure WebJobs that use .NET Core work fine. It's specifically the use of the WebJobs SDK that is not yet supported.

考虑到您的情况,我假设您需要构建普通的 WebJobs 和相关库(模型、查询等)而不是 NETCore,以便使用 WebJobs SDK。或者您需要 re-design 您的场景。

如果有人仍在寻找解决方案,最新版本已支持此功能

Install-Package Microsoft.Azure.WebJobs -Version 3.0.0-beta4

请注意,此软件包仍处于测试阶段。您可以在 https://www.nuget.org/packages/Microsoft.Azure.WebJobs/3.0.0-beta4

查看最新更新