AreYouFreeBusy / AlexaSkillsKit.NET

.NET library that simplifies Alexa skills development; same object model as Amazon's AlexaSkillsKit for Java
MIT License
212 stars 99 forks source link

Azure Functions issue? #71

Closed mgroves closed 6 years ago

mgroves commented 6 years ago

I'm creating an Azure Function to act as a backend for an Alexa skill.

All I've done so far in Visual Studio:

  1. File -> New Azure Functions project
  2. Add new Azure Function
  3. Add AlexaSkillsKit.NET with NuGet
  4. Create a shell of a Speechlet implementation
  5. Add the minimum code to the Azure Function to process the speechlet

When I compile, I get an error:

Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

But according to NuGet in Visual Studio, it's not trying to use 7.0.0.0, it's using 9+. I've tried updating and downgrading the JSON.net package. I've tried Clean/Rebuild/restarting from scratch.

I thought that maybe assembly binding might be the answer, but there's no Web.config or App.config in an Azure Functions project.

What am I missing? How do I get rid of this error?

Speechlet code:

public class MySpeechlet : SpeechletBase, ISpeechletWithContextAsync
{
    public Task<SpeechletResponse> OnIntentAsync(IntentRequest intentRequest, Session session, Context context)
    {
        throw new System.NotImplementedException();
    }

    public Task<SpeechletResponse> OnLaunchAsync(LaunchRequest launchRequest, Session session, Context context)
    {
        throw new System.NotImplementedException();
    }

    public Task OnSessionStartedAsync(SessionStartedRequest sessionStartedRequest, Session session, Context context)
    {
        throw new System.NotImplementedException();
    }

    public Task OnSessionEndedAsync(SessionEndedRequest sessionEndedRequest, Session session, Context context)
    {
        throw new System.NotImplementedException();
    }
}

Azure Function:

public static class MyFunction
{
    [FunctionName("MyFunction")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
    {
        var speechlet = new MySpeechlet();
        return await speechlet.GetResponseAsync(req);
    }
}

I think this may be an issue with the Azure Functions SDK, but is there some way to workaround it?

stefann42 commented 6 years ago

Sounds like you need to figure out how to do assembly binding in Azure Functions. I haven't worked with Functions thus far so I'm not much help, sorry.

mgroves commented 6 years ago

I would love to try that, but there's no web.config/app.config for Azure Functions :-/

ElvenMonky commented 6 years ago

Hi @mgroves, binding redirects are not supported by Azure Functions v1 because of this issue. Azure Functions v2 are available in preview, but only support .Net Core, so to use it you need to wait for .Net Core version of AlexaSkillsKit.Net library to be released.

So the options are:

  1. You can simply ignore this error, as it does not prevent your Function App from further building or publishing.
  2. You can try workaround as described here. But I have not tested it and not sure if it helps to suppress error messages during initial build.
mgroves commented 6 years ago
  1. It does prevent the app from compiling. A compiler error (see above).
  2. I've tried that solution and it doesn't stop the compiler error.

I've come up with a workaround that seems to be okay so far: I've created a second project and put all the AlexaSkill code into that project, and reference the project from the Functions project. The Functions project still needs an AlexaSkills reference, but now everything seems to compile okay.

ElvenMonky commented 6 years ago

My personal Skill also implemented as Azure Function. Azure Functions v1 currently only support Newtonsoft.Json 9.0.1, so I use this version. I have same compilation error, but only when rebuild project from scratch. Further builds are successful. Are you experiencing different behavior?

mgroves commented 6 years ago

Every compile causes a compile error. Though maybe I should double check, I may have been overly aggressive with using Rebuild (because of the error).

ElvenMonky commented 6 years ago

Can we close it, as it is more of Azure issue and there is not much we can do, apart from releasing .Net Core library version?

ElvenMonky commented 6 years ago

Finally I had to deal with this error, as I was unable to publish my Function to Azure.

It seems to be a known issue in Azure function generator.

The solution is to mark your custom speechlet class as internal. Alternatively, you can move speechlet class to separate project as mentioned by @mgroves

Note: To make speechlet visible for unit tests project, add AssemlyInfo.cs to Function's Properties folder and put [assembly: InternalsVisibleTo("MySkill.UnitTests")] attribute there.