简体   繁体   中英

Azure Function C# 'Run' is inaccessible due to its protection level 2

I am new to Azure Functions but am attempting to call ac# file and method from the function. The "Run.csx" file itself is saying it is inaccessible due to the security level.

Azure run file:

using System;

public static void Run(TimerInfo myTimer, TraceWriter log)
{
Integration.Run.ImportRosters(
    log: s => log.Info(s),
    connectionString: "conString",
    siteID: 5,
    nowUtc: DateTime.UtcNow,
    ActualDays: 7,
    RosterDays: 7     
);
log.Info($"Import Roster Data function executed at: {DateTime.Now}");
}

And its calling this c# function:

namespace Integration
{
public static class Run
{
    private static readonly TimeZoneInfo NewZealand = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");

    public static void ImportRosters(
        Action<string> log,
        string connectionString,
        int siteID,
        DateTime nowUtc,
        int actualDays,
        int rosterDays
        )
    {

        var adminDatabase = new HospoIQ_AdminDBContext();
        var settings = adminDatabase.Settings.ToList();
        var localDateTime = TimeZoneInfo.ConvertTimeFromUtc(nowUtc, NewZealand);
        log($"Roster Data import function executed at: {localDateTime}");
        var sqlConnection = new SqlConnection(connectionString);

I am in the belief that it is an error with my code on the Azure side as it doesn't even reach my c# code.

Error Message:

[Error] Function compilation error 
2018-02-25T21:31:07.467 [Error] run.csx(10,5): error CS0149: Method name expected
2018-02-25T21:31:07.467 [Error] run.csx(11,33): error CS0122: 'Run' is inaccessible due to its protection level
2018-02-25T21:31:07.526 [Error] Exception while executing function: 
Functions.ImportRosterData. Microsoft.Azure.WebJobs.Script: Script compilation failed.2018-02-25T21:31:07.698 [Error] Function completed (Failure, Id=08508394-4d6b-4cb0-a9e7-9479a017df05, Duration=289ms)

You have a number of problems in your code sample:

  • No load directive in Run.csx
  • Namespaces are not allowed in C# script files
  • Parameter names of the called method do not match (eg ActualDays vs actualDays )
  • (recommended) Don't call your helper class Run not to confuse it with Run of the Function

Below is a complete working example that you should base your implementation on.

Integration.csx :

public static class Integration
{
    private static readonly TimeZoneInfo NewZealand = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");

    public static void ImportRosters(
        Action<string> log,
        string connectionString,
        int siteID,
        DateTime nowUtc,
        int actualDays,
        int rosterDays
        )
    {
        var localDateTime = TimeZoneInfo.ConvertTimeFromUtc(nowUtc, NewZealand);
        log($"Roster Data import function executed at: {localDateTime}");
    }
}

Run.csx :

#load "Integration.csx"

using System;

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    Integration.ImportRosters(
        log: s => log.Info(s),
        connectionString: "conString",
        siteID: 5,
        nowUtc: DateTime.UtcNow,
        actualDays: 7,
        rosterDays: 7     
    );
    log.Info($"Import Roster Data function executed at: {DateTime.Now}");

}

I'm not 100% sure, but I think that the error means, that the static class Run was inaccessible, not the Run method of your function.

According to this source you'll have to load your .csx file with the #load directive in order to use it

#load "integration.csx"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM