Error using Box SDK on Azure Functions - azure

I am getting a "Could not load file or assembly 'Box.V2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ddda8fe64dde1ac3' or one of its dependencies. The system cannot find the file specified" error in Azure Functions. Any ideas on what the problem might be?
I am using the package.json file to have Azure Functions pull in the references and hook them all up as described in the docs.
When you upload a project.json file, the runtime gets the packages and automatically adds references to the package assemblies. You don't need to add #r "AssemblyName" directives. Just add the required using statements to your run.csx file to use the types defined in the NuGet packages.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp
My package.json file:
{
"frameworks": {
"net46":{
"dependencies": {
"BouncyCastle": "1.8.1",
"BouncyCastle-PCL": "1.0.0.6",
"Box.V2": "2.14.0",
"Box.V2.JWTAuth": "1.2.0",
"Microsoft.Bcl": "1.1.10",
"Microsoft.Bcl.Async": "1.0.168",
"Microsoft.Bcl.Build": "1.0.14",
"Microsoft.Net.Http": "2.2.29",
"Newtonsoft.Json": "6.0.2",
"Nito.AsyncEx": "2.1.3",
"System.IdentityModel.Tokens.Jwt": "4.0.2.206221351"
}
}
}
}
run.csx using statements:
using global::Box.V2;
using global::Box.V2.Config;
using global::Box.V2.Exceptions;
using global::Box.V2.JWTAuth;
using global::Box.V2.Models;
Full Exception:
Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: Functions.GetTelogisFormInstancePdf ---> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException : Could not load file or assembly 'Box.V2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ddda8fe64dde1ac3' or one of its dependencies. The system cannot find the file specified.
at Submission#0.Run(HttpRequestMessage req,TraceWriter log) at : 84
End of inner exception
at System.RuntimeMethodHandle.InvokeMethod(Object target,Object[] arguments,Signature sig,Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object[] parameters,Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object[] parameters,CultureInfo culture)
at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.InvokeCore(Object[] parameters,FunctionInvocationContext context)
at async Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.Invoke(Object[] parameters)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.InvokeAsync[TReflected](Object[] arguments)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker,Object[] invokeParameters,CancellationTokenSource timeoutTokenSource,CancellationTokenSource functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan timerInterval,IFunctionInstance instance)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,IReadOnlyDictionary`2 parameters,TraceWriter traceWriter,CancellationTokenSource functionCancellationTokenSource)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??…

The error is clearly stating that it can't find the assembly:
System.IO.FileNotFoundException : Could not load file or assembly 'Box.V2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ddda8fe64dde1ac3' or one of its dependencies. The system cannot find the file specified.
And while you show in your .csx that you have using statements to include the namespaces, no where do you show that you actually reference the external assemblies
As described in the above linked documentation:
If you need to reference a private assembly, you can upload the assembly file into a bin folder relative to your function and reference it by using the file name (e.g. #r "MyAssembly.dll").
Make sure you get the path correct; I had to ensure the bin folder was within my path where the function was - the shared assembly approach didn't work for me.
I also suggest that perhaps an easier approach than dealing with csx and referencing assemblies is to ditch csx all together and use pre compiled functions as described in this blog post. Then you get full compile time resolution of assemblies and proper intellisense that you just don't get with the CSX files.

This error is misleading and I had to end up overriding BoxJWTAuth in order to make it work. The details of this error can be found here: https://github.com/box/box-windows-sdk-v2/issues/297

Related

How to use System.Runtime.Caching in an Azure Function App

I want to use simple in-memory caching of some values between function calls in a serverless function in an Azure Function App. I am developing directly in the Azure portal using C# script files. Following the suggestion 2) from this blog post by Mark Heath I have the following lines in my function's csx file:
#r "System.Runtime.Caching"
using System.Runtime.Caching;
using System;
static MemoryCache memoryCache = MemoryCache.Default;
//.....
This should be the System.Runtime.Caching assembly from this doc.
But on complilation (save in Azure Portal), I get:
2021-01-05T07:25:39.687 [Information] Script for function 'Intake' changed. Reloading.
2021-01-05T07:25:39.807 [Error] run.csx(1,1): error CS0006: Metadata file 'System.Runtime.Caching' could not be found
2021-01-05T07:25:39.865 [Error] run.csx(2,22): error CS0234: The type or namespace name 'Caching' does not exist in the namespace 'System.Runtime' (are you missing an assembly reference?)
2021-01-05T07:25:39.938 [Error] run.csx(4,8): error CS0246: The type or namespace name 'MemoryCache' could not be found (are you missing a using directive or an assembly reference?)
2021-01-05T07:25:40.008 [Error] run.csx(4,34): error CS0103: The name 'MemoryCache' does not exist in the current context
2021-01-05T07:25:40.041 [Information] Compilation failed.
This is my host.json, for reference:
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
Do I need to add something here? I would have expected, adding a #r reference to the assembly would be sufficient.
Upon inspecting the documentation, apart from the well-known assemblies mentioned in the document which can be referenced with #r, other nuget packages, may have to be uploaded by using a .proj file I suspect.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#using-nuget-packages
I crafted a function.proj file with its contents as follows
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Runtime.Caching" Version="5.0.0" />
</ItemGroup>
Then I uploaded function.proj using App Service Editor in the portal
Once that was complete, I was able to compile csx successfully

Cannot run PowerShell scripts in Azure Functions v2

I'm trying to write a Function App using .NET Core in Azure Functions v2. When using the Microsoft.Powershell.SDK package from Nuget (required for .NET Core PowerShell runtime) I cannot get Visual Studio to copy the System.Management.Automation library to the bin with my Function App.
This results in the following error:
System.Private.CoreLib: Exception while executing function: Function1. TestPowershellInFunction: Could not load file or assembly 'System.Management.Automation, Version=6.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.
I've reproduced this in an existing Azure Function and a new Solution by simply creating a Timer function and adding the following snippet:
PowerShell shell = PowerShell.Create();
IEnumerable<PSObject> result = shell.AddScript("Write-Output 'Hello, World!'").Invoke();
foreach(PSObject line in result)
{
log.LogInformation(line.ToString());
}
This works on a new Console App with the PowerShell Nuget installed, but when added to the Function App I get the error. I do notice that System.Management.Automation doesn't get put in the bin directory with a regular Console App but I'm not sure how to interpret this. I know it's a System library but I can't use it unless the Nuget is installed, so I don't know if this is a special case. In both scenarios I'm using v6.1.1 of the PowerShell Nuget.
Is this a known bug with Functions v2? Or am I missing something?
It's a known issue that Function can't load runtime assemblies([FunctionProject]\bin\Debug\netcoreapp2.1\bin\runtimes) correctly.
The workaround is to move assemblies to output dir bin manually. Right click on your function project and Edit <FunctionProject>.csproj. Add following items to achieve our goal.
<PropertyGroup>
<SDKVersion>6.1.1</SDKVersion>
<SDKPlatform>win-x86</SDKPlatform>
</PropertyGroup>
<ItemGroup>
<None Include="
$(USERPROFILE)\.nuget\packages\system.directoryservices\4.5.0\runtimes\win\lib\netcoreapp2.0\System.DirectoryServices.dll;
$(USERPROFILE)\.nuget\packages\system.management\4.5.0\runtimes\win\lib\netcoreapp2.0\System.Management.dll;
$(USERPROFILE)\.nuget\packages\system.management.automation\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\System.Management.Automation.dll;
$(USERPROFILE)\.nuget\packages\microsoft.management.infrastructure\1.0.0\runtimes\win10-x86\lib\netstandard1.6\Microsoft.Management.Infrastructure.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.management\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Management.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.utility\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Utility.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.diagnostics\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Diagnostics.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.sdk\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.SDK.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.security\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Security.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.coreclr.eventing\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.CoreCLR.Eventing.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.consolehost\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.ConsoleHost.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.markdownrender\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.MarkdownRender.dll;
$(USERPROFILE)\.nuget\packages\microsoft.wsman.runtime\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.WSMan.Runtime.dll;
$(USERPROFILE)\.nuget\packages\microsoft.wsman.management\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.WSMan.Management.dll;
">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="CopyRuntimeToBin" AfterTargets="Build">
<Copy SourceFiles="
$(USERPROFILE)\.nuget\packages\system.directoryservices\4.5.0\runtimes\win\lib\netcoreapp2.0\System.DirectoryServices.dll;
$(USERPROFILE)\.nuget\packages\system.management\4.5.0\runtimes\win\lib\netcoreapp2.0\System.Management.dll;
$(USERPROFILE)\.nuget\packages\system.management.automation\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\System.Management.Automation.dll;
$(USERPROFILE)\.nuget\packages\microsoft.management.infrastructure\1.0.0\runtimes\win10-x86\lib\netstandard1.6\Microsoft.Management.Infrastructure.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.management\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Management.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.utility\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Utility.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.diagnostics\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Diagnostics.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.sdk\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.SDK.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.security\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Security.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.coreclr.eventing\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.CoreCLR.Eventing.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.consolehost\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.ConsoleHost.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.markdownrender\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.MarkdownRender.dll;
$(USERPROFILE)\.nuget\packages\microsoft.wsman.runtime\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.WSMan.Runtime.dll;
$(USERPROFILE)\.nuget\packages\microsoft.wsman.management\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.WSMan.Management.dll;
" DestinationFolder="$(OutputPath)\bin" />
</Target>
Note that microsoft.management.infrastructure is set to win10-x86 on my side(Win10), you may need to change according to your pc platform. Assemblies are x86 as VS uses x86 Function CLi by default, we don't need to worry about it unless we need to work with x64.

Azure function error: Could not load file or assembly 'Microsoft.Xrm.Sdk, Version=7.0.0.0'?

I m doing a azure function and testing it locally where it will add some data to Dyanmcis CRM. When I run function it throw this error:
Could not load file or assembly 'Microsoft.Xrm.Sdk, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Please note my code uses Microsoft.Xrm.Client(version 7) and it has Microsoft.Xrm.Sdk (version 8.1.0.235).
Please help.
The problem is due to the missing assembly redirect. You can see exactly which assembly redirect is needed by adding the Microsoft.CrmSdk.CoreAssemblies NuGet package to a normal .Net project and looking at the generated binding redirect statements added to app.config.
Currently Azure Functions do not support setting assembly redirects, so it has to be done in code. I solved this problem by using the code from this blog post: How to fix the assembly binding redirect problem in Azure Functions
With that code, local.settings.json looks like this:
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDashboard": "",
"BindingRedirects": "[ { \"ShortName\": \"Microsoft.Xrm.Sdk\", \"RedirectToVersion\": \"8.0.0.0\", \"PublicKeyToken\": \"31bf3856ad364e35\" } ]"
}
and in Application settings in the Azure portal:
[ { "ShortName": "Microsoft.Xrm.Sdk", "RedirectToVersion": "8.0.0.0", "PublicKeyToken": "31bf3856ad364e35" } ]

unable to compile my c++ project after install VS 2012

Error 1 error MSB4018: The "MIDL" task failed unexpectedly.
System.TypeInitializationException: The type initializer for
'Microsoft.Build.Utilities.FileTracker' threw an exception. --->
System.IO.FileNotFoundException: The system cannot find the file
specified. (Exception from HRESULT: 0x80070002) at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32
errorCode, IntPtr errorInfo) at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32
errorCode) at
Microsoft.Build.Shared.NativeMethodsShared.ThrowExceptionForErrorCode(Int32
errorCode) at
Microsoft.Build.Shared.NativeMethodsShared.GetShortFilePath(String
path) at Microsoft.Build.Utilities.FileTracker..cctor() --- End of
inner exception stack trace --- at
Microsoft.Build.CPPTasks.TrackedVCToolTask.ComputeOutOfDateSources()
at Microsoft.Build.CPPTasks.TrackedVCToolTask.SkipTaskExecution() at
Microsoft.Build.Utilities.ToolTask.Execute() at
Microsoft.Build.CPPTasks.TrackedVCToolTask.Execute() at
Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at
Microsoft.Build.BackEnd.TaskBuilder.d__20.MoveNext()
C:\Program Files
(x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppCommon.targets 1179
Is this problem affecting all projects?
I had similar problem when porting code from older version of VS. Form my experience the most errors are generated because paths. I do not remember details but using output paths explicitly (done in older versions) creates problem since project conversion mechanism tries to use macros and as a result in empty, wrongly formatted paths.
My advice is to verify all settings pertaining to output. Make sure OutputPath, TargetDir and others are set properly. Try changing settings and use to inherit from parent.
Create empty project of the same type and compare properties of both.

Could not load file or assembly 'Microsoft.VisualStudio.Shell, Version=2.0.0.0

I recently updated a website from VS2008 to VS2010. Publish with Web Deploy tool but I get the following error
Server Error in '/' Application. Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. Parser Error Message: Reference.svcmap: Could not load file or assembly 'Microsoft.VisualStudio.Shell, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Source Error: [No relevant source lines] Source File: /App_WebReferences/websConsultarCOVE/ Line: 1 Assembly Load Trace: The following information can be helpful to determine why the assembly 'Microsoft.VisualStudio.Shell, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' could not be loaded. WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18010
The error is in reference to a third party webservices
OS: Windows Server 2012
Thank you very much in advance

Resources