简体   繁体   English

Web api在本地工作,但在部署到Azure时则不能

[英]Web api working locally but not when deployed to Azure

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 . 我有一个我开发的Web Api,它在本地运行时工作得非常好,但是当我在Azure上发布它时,我得到500 Internal Sever Error

I have 3 tables in the Database - "Messages", "Users", "Conversations" 我在数据库中有3个表 - “消息”,“用户”,“对话”

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. 我的web.config中的其他连接字符串似乎没问题。

Is this a database problem or a code first problem? 这是数据库问题还是代码问题?

Web.confg: 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>

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 . 您可以使用log4net或sirilog等任何记录器,然后在返回HTTP 500的操作或方法中记录错误,或者您可以在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. 此外,如果整个Web API应用程序没有运行(返回内部服务器错误),它似乎是一个配置错误(例如,错误的ConnectionString),但如果您的某些操作返回200而其他操作返回500,您应该调试您的代码或日志你的错误,知道确切的例外。

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! 几天浪费在如此小的东西上!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署在Azure Web App / Azure API上时缺少CORS标头 - CORS headers missing when deployed on Azure Web App / Azure API Azure移动服务-自定义API在本地运行,但在部署到Azure时获得404 - Azure Mobile Service - Custom API runs locally but get 404 when Deployed to Azure 简单的WebClient在本地工作,但在部署到Azure时无法工作 - Simple WebClient works locally, but not when deployed to Azure 当应用程序部署为 Azure Web 应用程序时,OnTokenValidated 事件未触发,但在本地运行时触发 - OnTokenValidated event not firing when application is deployed as Azure Web App, but fires when running locally Identity Server 4部署到Azure网站时无法正常工作 - Identity Server 4 not working when deployed to Azure Web Site Azure AD图形api在本地工作但在部署时失败 - Azure AD graph api working on local but fails when deployed C#预处理器在本地工作,但在部署时不工作 - C# Preprocessor working locally but not when deployed 将映像上传到Azure Blob存储-本地工作,部署后失败 - Uplod images to azure blob storage - Works locally, fails when deployed ASP.NET Web API服务器代码在本地运行时连接到数据库,但在Azure上托管时不连接到数据库 - ASP.NET Web API server code connects to database when run locally, but not when hosted on Azure 部署到天蓝色的Google身份验证不起作用 - Google authentication not working when deployed to azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM