Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could Not load Microsoft.CodeAnalysis.Scripting in Azure Function #7

Open
Sanjay-Radhakrishnan opened this issue Dec 20, 2019 · 7 comments
Labels
question Further information is requested

Comments

@Sanjay-Radhakrishnan
Copy link

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.

@oleg-shilo oleg-shilo added the question Further information is requested label Dec 20, 2019
@oleg-shilo
Copy link
Owner

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.

@Sanjay-Radhakrishnan
Copy link
Author

Sanjay-Radhakrishnan commented Dec 20, 2019

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

@oleg-shilo
Copy link
Owner

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.

@Sanjay-Radhakrishnan
Copy link
Author

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.

@oleg-shilo
Copy link
Owner

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.

@DenisBalan
Copy link

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

@oleg-shilo
Copy link
Owner

Thank you for letting me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants