cs-script.core icon indicating copy to clipboard operation
cs-script.core copied to clipboard

Could Not load Microsoft.CodeAnalysis.Scripting in Azure Function

Open Sanjay-Radhakrishnan opened this issue 6 years ago • 7 comments

Exception Version 1.2.2 had a problem in loading the Microsoft.CodeAnalysis.Scripting reference, while downgrading to version 1.0.1 works well in an Azure Function. The above error occurs while using the CSScript class.

Sanjay-Radhakrishnan avatar Dec 20 '19 06:12 Sanjay-Radhakrishnan

I suspect that there is some compatibility issues with the latest version of Roslyn (scripting).

Can you send me a hello-world style sample so I can investigate it.

If for whatever reasons it's not an option then try to download the package manually and reference it as assemblies and not as nuget package.

oleg-shilo avatar Dec 20 '19 12:12 oleg-shilo

Please run this within an Azure Function/Emulator to reproduce.

using System;
using System.Data;
using CSScriptLib;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.CodeAnalysis.Scripting;

namespace MyFunctionApp
{
    public static class SayHelloFunction
    {
        [FunctionName("SayHelloFunction")]
        public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
        {
            var SayHello =
                        @"        using System;
                                  using System.IO;
                                  public class Helper
                                        {
                                            public void SayHello()
                                            { 
                                                Console.WriteLine(""Hello World!"");
                                            }
                                        }";
            CSScript.Evaluator.ReferenceAssembliesFromCode(SayHello);
            dynamic block = CSScript.Evaluator.LoadCode(SayHello);
            block.SayHello();
        }
    }
}

Sanjay-Radhakrishnan avatar Dec 20 '19 13:12 Sanjay-Radhakrishnan

I cannot run/debug it on Azure. I just don't have an Azure environment handy.

But I tested it locally and your code runs just fine. You only have a few unnecessary code fragments:

using Microsoft.CodeAnalysis.Scripting;
// and 
CSScript.Evaluator.ReferenceAssembliesFromCode(SayHello);

It's clear that you have problem with Microsoft.CodeAnalysis.Scripting package, not CS-Script. At least the exception you provided explicitly say that.

As a test code you can try to load Microsoft.CodeAnalysis.Scripting assembly by instantiating any type from it just befor you try to execute your script. The chances are that you will have the same exception even before you trigger the script execution.

oleg-shilo avatar Dec 20 '19 13:12 oleg-shilo

Thanks @oleg-shilo for running in through, this code block runs fine in my core console app also, the exception arises only when I run it within an Azure Function environment. I tried instantiating a type from Microsoft.CodeAnalysis.Scripting package and there wasn't an exception though. As I said before the CS-Script class works perfectly fine for me in version 1.0.1.

Sanjay-Radhakrishnan avatar Dec 22 '19 11:12 Sanjay-Radhakrishnan

The fact that it is running fine with v1.0.1 does not help much. None from the added functionality of v1.2.2 should trigger any probing problems. at least i cannot see any obvius suspect.

I suspect that there is a missmatch between requested and available version of Microsoft.CodeAnalysis.Scripting. Thus is what you can do to investigate and possibly fix it. Run this exact code as below:

using System;
using Microsoft.CodeAnalysis;
using CSScriptLib;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            var avaliableAsm = typeof(MetadataReference).Assembly;
            Console.WriteLine("Available: " + avaliableAsm);

            AppDomain.CurrentDomain.AssemblyResolve += (s, a) =>
                {
                    Console.WriteLine("Trying to resolve: " + a.Name);
                    // return avaliableAsm;
                    return null;
                };

            LoadEngine();
        }

        static void LoadEngine()
        {
            var SayHello = @"using System;
                           using System.IO;
                           public class Helper
                                 {
                                     public void SayHello()
                                     {
                                         Console.WriteLine(""Hello World!"");
                                     }
                                 }";

            dynamic block = CSScript.Evaluator.LoadCode(SayHello);
            block.SayHello();
        }
    }
}

Run this code on Asure. I suspect that the first console output ('Available: ...') will not ble like mine:

image

Most likely it will be some other version.

And "Trying to resolve: " will be trying to find the required v2.10. If it is the case you can try to uncomment // return avaliableAsm; and this in turn may be just enough to resolve your assembly.

Let me know how did it go.

oleg-shilo avatar Dec 23 '19 12:12 oleg-shilo

see Azure/azure-functions-host/issues/5796 for detailed explanation on why it happends and how to fix it (easy fix through) ;)

@oleg-shilo this could be closed

DenisBalan avatar Aug 31 '20 10:08 DenisBalan

Thank you for letting me know

oleg-shilo avatar Aug 31 '20 14:08 oleg-shilo