如何在Visual Studio代码中解决C#脚本csx文件的依赖性问题?

问题描述 投票:1回答:1

我正在尝试使用csx文件使用服务总线队列触发器模板创建azure函数程序。但是我在解决依赖关系时遇到问题。老实说,我对[this doc](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp)中提到的项目结构非常困惑。我尝试了两种方法。

第一种方法...在Visual Studio代码中创建一个名为MyAzureFunc的文件夹,然后手动添加function.json,project.json,project.lock.json和run.csx。然后,在MyAzureFunc文件夹之外,添加host.json和local.settings.json。这种方法导致依赖性问题。

Folder Structure

第二种方法...在Visual Studio代码中使用服务总线队列模板创建一个Azure Function项目,该模板将生成完全不同的文件夹和项目结构。然后,我将删除大多数自动生成的文件,并手动添加第一种方法中提到的结构。这仍然导致依赖性问题。

Service Bus Queue Project Structure

我认为这可能发生的一些可能原因如下...1.框架版本-我的project.json调用“ net46”,但是当我使用服务总线队列模板在Visual Studio代码中创建azure函数时,它实现了“ netcoreapp2.1”2.需要运行一些命令来识别软件包的依赖关系3.需要一些项目配置才能使用csx

我相信这里的重要文件是project.json。下面是代码...

{
    "frameworks": {
      "net46":{
        "dependencies": {
          "MongoDB.Driver": "2.6.1",
          "MongoDB.Driver.Core": "2.6.1",
          "MongoDB.Bson": "2.6.1",
      "SharpZipLib": "0.86.0",
      "RabbitMQ.CLient": "5.0.1"
        }
      }
     }
  }

下面是我要在csx文件中使用的软件包的代码...

#r "Newtonsoft.Json"
#r "Microsoft.ServiceBus"

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging; --> ERROR
using Newtonsoft.Json; --> ERROR
using MongoDB.Driver; --> ERROR
using MongoDB.Bson; --> ERROR
using MongoDB.Bson.IO; --> ERROR
using RabbitMQ.Client; --> ERROR
using ICSharpCode.SharpZipLib.Zip.Compression.Streams; --> ERROR
visual-studio-code azure-functions azure-servicebus-queues csx csharpscript
1个回答
0
投票

因此,我已经在这个问题上取得了一些进展。

我没有使用VS Code,而是在VS2017中从头开始创建了项目。我选择使用针对.NET Framework 4.6.1的服务总线队列触发器来创建一个Azure函数项目。

现在project.json解决了很多依赖问题。

{
  "frameworks": {
    "net46": {
      "dependencies": {
        "MongoDB.Driver": "2.6.1",
        "MongoDB.Driver.Core": "2.6.1",
        "MongoDB.Bson": "2.6.1",
        "SharpZipLib": "0.86.0",
        "RabbitMQ.CLient": "5.0.1"
      }
    }
  }
}

运行代码时,我现在面临的唯一问题是使用#r "Microsoft.ServiceBus"参考。我收到下面的错误。

[9/25/2019 8:03:48 PM] run.csx(2,1): error CS0006: Metadata file 'Microsoft.ServiceBus' could not be found
[9/25/2019 8:03:48 PM] run.csx(8,17): error CS0234: The type or namespace name 'ServiceBus' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
[9/25/2019 8:03:48 PM] run.csx(80,24): error CS0246: The type or namespace name 'BrokeredMessage' could not be found (are you missing a using directive or an assembly reference?)

我知道#r "Microsoft.ServiceBus"导致此错误。唯一显示出希望的方法是引用,如下所示。

#r "../bin/Microsoft.ServiceBus"

这将导致“找不到元数据文件...”。但是它仍然会引发其他错误。

© www.soinside.com 2019 - 2024. All rights reserved.