Web api working locally but not when deployed to Azure

asp.net-web-api2 azure-sql-database c# ef-code-first entity-framework-6

Question

I have a Web Api that I have developed which works perfectly fine when run locally, but when I publish it on Azure, I get a 500 Internal Sever Error.

I have 3 tables in the Database - "Messages", "Users", "Conversations"

The error only happens when I make a call to "Conversations" or "Users". (Conversations contains a list of Users and Users contains a list of conversations.)

I am not sure what is causing this and I don't know how to get more information from this error.

I have tried changing the entity framework connection string, although I didn't really know what I was doing with it. The other connection strings in my web.config seem to be fine.

Is this a database problem or a code first problem?

Web.confg:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=tcp:uqq5j22p7m.database.windows.net,1433;Initial Catalog=AA_db;Persist Security Info=True;User ID=KENH@uqq5j22p7m;Password=Password;Connect Timeout=30;Encrypt=True;MultipleActiveResultSets=True;"
  providerName="System.Data.SqlClient" />
<add name="AcademicAssistantContext" connectionString="Data Source=tcp:uqq5j22p7m.database.windows.net,1433;Initial Catalog=AA_db;Persist Security Info=True;User ID=KENH@uqq5j22p7m;Password=Password;Connect Timeout=30;Encrypt=True;MultipleActiveResultSets=True;" 
  providerName="System.Data.SqlClient" />

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

1
4
3/6/2016 3:28:09 PM

Accepted Answer

I have found a solution just now! I was doing:

public IQueryable<Conversation> GetConversations()
{
    return db.Conversations;
}

This must have been causing a serialization problem because it works when changed to:

public IEnumerable<Conversation> GetConversations()
{
    return db.Conversations.ToList();
}

Couple of days wasted on something so small!

0
3/6/2016 4:04:52 PM

Popular Answer

It's hard to say what is the error without debugging.

You can use any logger like log4net or sirilog and then logs errors in the action or method that return HTTP 500 or you can live debug your code in azure.

Also if the over all web API application didn't run (returns internal server error) it seems to be a configuration error ( wrong ConnectionString for example) but if some of your actions returns 200 and others returns 500 you should debug your code or log your errors to know the exact exception for that.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow