0

I am getting the following error and I don't know what this error is all about. Which file it is looking for?

enter image description here

The code I want to run in Azure functions is 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}");
}

and Dependency file is 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>

Bit if I comment the line where I call GetAzureContext function in void Run, It runs successfully.

Rajat Sharma
  • 47
  • 1
  • 15
  • You can load private assemblies from Nuget packages using the steps provided here https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#using-nuget-packages – Ling Toh Oct 20 '18 at 00:19
  • Do you have any update about this thread? If it is useful, you could mark it as an answer. – Tom Sun - MSFT Oct 29 '18 at 00:41

1 Answers1

0

If your function runtime is ~1, create project.json with following content.

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "xxxxxx": "xxx.xx.xx"
      }
    }
   }
} 

But now when we create the azure function on the Azure portal now, the default runtime version is ~2. we need to create function.proj as below.

<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>

enter image description here After you upload the function.proj, it will add the to function host environment. Also need to remove unnecessary #r "Microsoft.Azure.Management.Fluent" and "Microsoft.Azure.Management.ResourceManager.Fluent" or you will got error.

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;
}

Test Result:

enter image description here

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47