botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

500 Internal Server Error in emulator - V3

Open Huskitch opened this issue 10 years ago • 54 comments

I've recently upgraded my bot to the new V3 bot framework, and since then I've been getting 500 InternalServerErrors in the emulator and the "sorry, the bot is having trouble responding" messages in Slack, breakpoints anywhere in the method below don't get triggered at all so I can't really get a stack trace.

        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                // return our reply to the user
                Activity reply = activity.CreateReply("hello world");
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }

This code is the default VS template code, with the correct Web.config settings.

Below is what appears in the log when testing the connection to the bot:

InternalServerError
{
  "message": "An error has occurred.",
  "exceptionMessage": "Could not load type 'System.IdentityModel.Tokens.TokenValidationParameters' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.0.0.127, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.",
  "exceptionType": "System.TypeLoadException",
  "stackTrace": "   at Microsoft.Bot.Connector.BotAuthentication.<OnAuthorizationAsync>d__16.MoveNext()\r\n   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)\r\n   at Microsoft.Bot.Connector.BotAuthentication.OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}

Huskitch avatar Jul 12 '16 07:07 Huskitch

@msft-shahins I tried only updating the bot builder package, but then I get this:

image

Huskitch avatar Jul 12 '16 20:07 Huskitch

@msft-shahins Thank you, I made MicrosoftAppId and MicrosoftAppPassword empty (both in web.config and emulator) and now I'm in the same state as @Huskitch ...

lamaq-git avatar Jul 12 '16 20:07 lamaq-git

@Huskitch Can you share more details about when this exception happens? What are the configuration for bot and emulator? I guess your project still uses the wrong version of assembly. Make sure to clean your project and rebuild again after you fix dependencies to correct version. This might help you solve the problem.

msft-shahins avatar Jul 12 '16 20:07 msft-shahins

@msft-shahins The exception happens when I send a message to the bot through the emulator, it shows 505 InternalServerError in the emulator and that exception in VS.

The versions for the nuget packages are as follows:

Microsoft.Bot.Builder: 3.0.1
Microsoft.IdentityModel.Protocol.Extensions: v1.0.2.206221351
Microsoft.Rest.ClientRuntime: 2.3.1
Newtonsoft.Json: 8.0.3
System.IdentityModel.Tokens.Jwt

The Web.config assembly bindings are:

   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.29.0" newVersion="4.2.29.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Rest.ClientRuntime" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>

I've tried both with and without the correct appid and passwords.

Huskitch avatar Jul 12 '16 20:07 Huskitch

@Huskitch Microsoft.Rest.ClientRuntime: 2.3.1 is the problem. It seems that you have updated that too. The correct dependencies are:

Microsoft.Bot.Builder- 3.0.1
Microsoft.IdentityModel.Protocol.Extensions- 1.0.2.206221351
Microsoft.Rest.ClientRuntime - 1.8.2
Newtonsoft.Json - 8.0.3
System.IdentityModel.Tokens.Jwt - 4.0.2.206221351

here is the packages.config for a project from template with correct dependencies:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Autofac" version="3.5.2" targetFramework="net46" />
  <package id="Chronic.Signed" version="0.3.2" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.Bot.Builder" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.2.206221351" targetFramework="net46" />
  <package id="Microsoft.Rest.ClientRuntime" version="1.8.2" targetFramework="net46" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.2.1" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="8.0.3" targetFramework="net46" />
  <package id="System.IdentityModel.Tokens.Jwt" version="4.0.2.206221351" targetFramework="net46" />
</packages>

msft-shahins avatar Jul 12 '16 20:07 msft-shahins

@msft-shahins Just made a fresh project and made sure all of the dependencies are the same, now I'm getting this exception after sending a message to the bot:

image

Huskitch avatar Jul 12 '16 20:07 Huskitch

@Huskitch can you set a breakpoint on the messages controller and check if you are getting the message from the emulator?

msft-shahins avatar Jul 12 '16 21:07 msft-shahins

@msft-shahins Yeah, I get the message, the method runs fine up until this breakpoint:

image

Huskitch avatar Jul 12 '16 21:07 Huskitch

@Huskitch can you check the reply activity that is getting generated? It seems to me that the post to emulator is failing. what is the emulator configuration that you are using to post the message?

msft-shahins avatar Jul 12 '16 21:07 msft-shahins

This is the emulator setup:

image

I've tried both with and without appid and pw, (though it shouldn't matter right?).

Here's the activity object received: image

And here's the reply object: image

Huskitch avatar Jul 12 '16 21:07 Huskitch

@Huskitch, your emulator url is wrong! It should be http://localhost:9000. It is basically the url that the emulator is listening on and is set in the ServiceUrl of incoming activity. You can read more about the reply scheme here.

msft-shahins avatar Jul 12 '16 21:07 msft-shahins

I'm getting a 500 error also, my Emulator URI is correct, app id and secret are blank and I have the correct Nuget package versions referenced.

The exceptions seem to be thrown somewhere internally after the StartAsync method of my LuisDialog returns.

This is what I see in the output window of VS:

image

Here's my emulator:

image

craigomatic avatar Jul 13 '16 04:07 craigomatic

@msft-shahins I've deleted the 2 keys and set a new one. But then i realized that this wont have anything to do with the emulator authenticating at the local running bot. Anyway, unless i leave everything blank i still get the mentioned exception. My web config keys are BotId MicrosoftAppId MicrosoftAppPassword

NanashiGIT avatar Jul 13 '16 07:07 NanashiGIT

Oops, turns out my issue was my own fault - I was using a LuisDialog and had a space at the end of the attribute so the authentication was failing. Removing the space leaves me with a working bot.

craigomatic avatar Jul 13 '16 17:07 craigomatic

@NanashiGIT are you getting "SecurityTokenSignatureKeyNotFoundException" when you set the MicrosoftAppId and password? Are you getting the exception on the incoming request or when wants to post back the reply? can you share your MicrosoftAppId?

msft-shahins avatar Jul 13 '16 17:07 msft-shahins

@msft-shahins I only get the error when setting the AppId and Password, so yes. Im getting the exception even before the Post-Method is called. So on incoming requests. My ID: ......

NanashiGIT avatar Jul 13 '16 21:07 NanashiGIT

@msft-shahins, I?m also having the same error:

InternalServerError { "message": "An error has occurred.", "exceptionMessage": "Could not load type 'System.IdentityModel.Tokens.TokenValidationParameters' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.0.0.127, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.",

I'm having the error while calling the bot in the "Test connection to your bot" of the dev portal.

I have updated packages.config to the contents you mention above (+clean project + redeploy). And I upgraded a project to v3, and fixed the 3 keys in web.config. Ideas?

lokijota avatar Jul 15 '16 19:07 lokijota

@lokijota it seems that your bot still depends on the newer System.IdentityModel.Tokens.Jwt (5.0.0.127). Make sure that you have the right dependency and everything works locally against emulator. Then do a clean deployment. If you are deploying to azure from Visual Studio, make sure to select Remove additional files at destination. I have noticed that sometimes the dependencies aren't refreshed correctly, if I don't select that option.

image

msft-shahins avatar Jul 15 '16 19:07 msft-shahins

@msft-shahins, here's what I did:~

  • there was an assembly redirect in web.config:
 <dependentAssembly>
        <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.127" newVersion="5.0.0.127" />
      </dependentAssembly>

I commented this line, cleaned, redeployed, and when I tested it, the Internal Server Error remained, but this time referring to the 4.x version of the Jwt assembly;

  • I had already set the "clear files at destination" option;
  • I removed the Jwt reference from the project, removed/added Bot.Builder again in Nuget, and redeployed. And again the 500 error mentioning the 5.x version.

frustrated... :-(

Any other idea?

ps: here's the packages.config file:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Autofac" version="3.5.2" targetFramework="net46" />
  <package id="Chronic.Signed" version="0.3.2" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.Bot.Builder" version="3.0.1" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.2.206221351" targetFramework="net46" />
  <package id="Microsoft.Rest.ClientRuntime" version="1.8.2" targetFramework="net46" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.2.1" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="8.0.3" targetFramework="net46" />
  <package id="System.IdentityModel.Tokens.Jwt" version="4.0.2.206221351" targetFramework="net46" />
</packages>

and when I remove and re-add the Nuget reference to bot.builder, the redirect xml gets reinserted in web.config (and thus I end up with the 500 again):

      <dependentAssembly>
        <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.127" newVersion="5.0.0.127" />
      </dependentAssembly>

Plus, the reference in Nuget seems to be right (and un-upgraded):

2016-07-16 00_48_49-rbs chatbot - microsoft visual studio

lokijota avatar Jul 15 '16 23:07 lokijota

remove this from your web.config:

 <dependentAssembly>
        <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.127" newVersion="5.0.0.127" />
      </dependentAssembly>

and also make sure that your bot works against emulator with your MicrosoftAppId and MicrosoftAppPassword before trying the deployment.

msft-shahins avatar Jul 15 '16 23:07 msft-shahins