我遇到以下错误,我不知道此错误的全部含义。正在寻找哪个文件?
我要在Azure函数中运行的代码是run.csx
#r "/home/.nuget/microsoft.azure.management.containerinstance.fluent/1.16.1/lib/netstandard1.4/Microsoft.Azure.Management.ContainerInstance.Fluent.dll"
#r "/home/.nuget/microsoft.azure.management.fluent/1.14.0/lib/netstandard1.4/Microsoft.Azure.Management.Fluent.dll"
#r "/home/.nuget/microsoft.azure.management.resourcemanager.fluent/1.16.1/lib/netstandard1.4/Microsoft.Azure.Management.ResourceManager.Fluent.dll"
using System;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ContainerInstance.Fluent;
public static IAzure GetAzureContext(string authFilePath, ILogger log)
{
IAzure azure;
ISubscription sub;
try
{
azure = Azure.Authenticate(authFilePath).WithDefaultSubscription();
sub = azure.GetCurrentSubscription();
//log.LogInformation($"Authenticated with subscription '{sub.DisplayName}' (ID: {sub.SubscriptionId})");
}
catch (Exception ex)
{
log.LogInformation($"\nFailed to authenticate:\n{ex.Message}");
if (String.IsNullOrEmpty(authFilePath))
{
log.LogInformation("Have you set the AZURE_AUTH_LOCATION environment variable?");
}
throw;
}
return azure;
}
public static void Run(string myEventHubMessage, ILogger log)
{
// Authenticate with Azure
string authFilePath = "/home/site/wwwroot/EventHubTriggerCSharp3/my.azureauth";
IAzure azure = GetAzureContext(authFilePath, log);
log.LogInformation($"C# Event Hub trigger function processed a message: {myEventHubMessage}");
}
并且依赖文件是function.proj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ContainerInstance.Fluent" Version="1.16.1" />
</ItemGroup>
</Project>
如果我在void Run中注释了我调用GetAzureContext函数的行,则位,它将成功运行。
答案 0 :(得分:0)
如果函数运行时约为1,请创建具有以下内容的 project.json 。
{
"frameworks": {
"net46":{
"dependencies": {
"xxxxxx": "xxx.xx.xx"
}
}
}
}
但是现在当我们现在在Azure门户上创建Azure函数时,默认运行时版本为〜2。我们需要如下创建 function.proj 。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.16.1" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager.Fluent" Version="1.16.1" />
</ItemGroup>
</Project>
上传function.proj后,它将添加到函数宿主环境。还需要删除不必要的 #r "Microsoft.Azure.Management.Fluent"
和"Microsoft.Azure.Management.ResourceManager.Fluent"
,否则您将收到错误消息。
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
try
{
var azure = Azure.Authenticate(@"D:\\home\\site\\wwwroot\\my.azureauth").WithDefaultSubscription();
var sub = azure.GetCurrentSubscription();
log.LogInformation($"Authenticated with subscription '{sub.DisplayName}' (ID: {sub.SubscriptionId})");
}
catch(Exception ex)
{
log.LogInformation($"\nFailed to authenticate:\n{ex.Message}");
throw;
}
测试结果: