3

I have a C# azure function on .NET 6 running in dotnet-isolated mode. This function calls another azure function, which is using Azure AD Authentication. In order to generate the token I have the following code:

    var audience = $"api://{appRegistrationClientId}";           
    var tokenCredential = new DefaultAzureCredential();
    var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(new[] { $"{audience}/.default" }) { });
    var apiToken = token.Token;

    return apiToken;

If I deploy this code to Azure, the code runs fine. I am able to call and retrieve data from the other function app. However, when running this locally on Visual Studio I get the following exception:

Azure PowerShell authentication failed due to an unknown error. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/powershellcredential/troubleshoot Unhandled exception. System.ArgumentException: Startup hook assembly 'Microsoft.Azure.Functions.Worker.Core' failed to load. See inner exception for details.

---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Azure.Functions.Worker.Core, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

File name: 'Microsoft.Azure.Functions.Worker.Core, Culture=neutral, PublicKeyToken=null'

at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assemblyName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)

at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)

at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)

at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)

--- End of inner exception stack trace ---

at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)

at System.StartupHookProvider.ProcessStartupHooks()

I tried going through the troubleshooting on the link provided by the exception message, but nothing worked. I am able to call Get-AzAccessToken -ResourceUrl https://management.core.windows.net and generate a token in powershell.

I have also added my azure account to Visual Studio under Azure Service Authentication -> Account Selection option.

Is there a way to make this call work locally or otherwise what are the workarounds for this. I do need to be able to call this function from my dev machine in order to test my own code.

paddingtonMike
  • 1,441
  • 1
  • 21
  • 37
  • Hi, could you share the version of the function framework/sdk you re using please ? – Thomas Mar 22 '22 at 03:41
  • you also need to check that you have access to the api. – Thomas Mar 22 '22 at 04:38
  • I am using .NET 6.0 and function apps v4. They are running in dotnet-isolated mode and the libraries are: Microsoft.Azure.Functions.Worker 1.60, Microsoft-Azure-Functions.Workder.Sdk 1.3.0. They appear to be the last version as nuget is not telling me there is an update – paddingtonMike Mar 22 '22 at 09:52

4 Answers4

8

When I got this error I had to restart Visual Studio 2022 and re-enter the credentials of the Azure Service Authentication (including MFA).

Visual Studio > Tools > Options > Azure Service Authentication

DeMaki
  • 371
  • 4
  • 15
0

I don't have enough rep to make a comment, but I was running into the same issue. I had to install Azure CLI (and restart of visual studio after doing so). May need to Log in to Azure CLI before you re-run as well.

Jeff G
  • 43
  • 5
0

SQL authentication with AAD was unusable locally; I was seeing a lot of a task was cancelled.

The solution appears to be to have increased the connnection timeout like this:

SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder(Configuration.GetConnectionString("MyDb"));
csb.ConnectTimeout = 60;
services.AddDbContext<MyContext>(options =>
{
    options.UseLazyLoadingProxies().UseSqlServer(csb.ToString());
});
Richard Barraclough
  • 2,625
  • 3
  • 36
  • 54
0

I ran into this issue as well.

When debugging my isolated Azure Function App in Visual Studio locally, calling GetSecretAsync, the Microsoft.Asal.TokenService.exe process starts Microsoft.ServiceHub.Controller.exe which starts ServiceHub.Host.dotnet.x64.exe which crashes several times per request, possibly due to retries. You can see in the Windows Event Viewer that the ServiceHub.Host.dotnet.x64.exe process crashes.

[In this Process Explorer image, multiple retries are seen causing crashes WerFault.exe is Windows Error Reporting Process

enter image description here

From the crash dump:

System.ArgumentException

HResult=0x80070057 Message=Startup hook assembly 'Microsoft.Azure.Functions.Worker.Core' failed to load. See inner exception for details. KERNELBASE.dll!RaiseException Line 936 C [Managed to Native Transition]

System.Private.CoreLib.dll!System.StartupHookProvider.CallStartupHook Line 135 C# System.Private.CoreLib.dll!System.StartupHookProvider.ProcessStartupHooks Line 104 C# [Native to Managed Transition]
[Inline Frame] hostpolicy.dll!coreclr_t::execute_assembly Line 89 C++ hostpolicy.dll!run_app_for_context Line 255 C++ hostpolicy.dll!run_app Line 284 C++ hostpolicy.dll!corehost_main Line 430 C++ hostfxr.dll!execute_app Line 146 C++ hostfxr.dll!`anonymous namespace'::read_config_and_execute Line 533 C++ hostfxr.dll!fx_muxer_t::handle_exec_host_command Line 1018 C++ hostfxr.dll!fx_muxer_t::execute Line 579 C++ hostfxr.dll!hostfxr_main_startupinfo Line 61 C++ ServiceHub.Host.dotnet.x64.exe!exe_start Line 235 C++ ServiceHub.Host.dotnet.x64.exe!wmain Line 304 C++ [Inline Frame] ServiceHub.Host.dotnet.x64.exe!invoke_main Line 90 C++ ServiceHub.Host.dotnet.x64.exe!__scrt_common_main_seh Line 288 C++ kernel32.dll!BaseThreadInitThunk Line 75 C ntdll.dll!RtlUserThreadStart Line 1166 C

I reported this issue and found that it was fixed recently. I installed the latest version of Visual Studio: 17.6 Preview 3 and it seems to no longer crash.

Calvin Hsia
  • 1
  • 1
  • 2