错误500:ManagedPipelineHandler,在Azure上部署Web API MVC 4

人气:994 发布:2022-10-16 标签: web azure asp.net-web-api asp.net-mvc-4 azure-sql-database

问题描述

我正在尝试将WebAPI项目部署到Azure。

当我有一个没有SQL访问的方法时,它会在我的浏览器中正确地将结果显示为JSON。

然而,当我使用实体框架做任何事情,并且只从DB获得测试字段时,整个事情就会出错:

 Module ManagedPipelineHandler 
 Notification ExecuteRequestHandler 
 Handler System.Web.Http.WebHost.HttpControllerHandler 
 Error Code 0x00000000 

我尝试的内容:

在‘Azure计算模拟器’中运行时,一切运行正常 在调试中使用完全相同的SQL Azure连接字符串时,它运行良好 当使用RDP连接到webrole并telnet到端口1443到SQL azure服务器时,它连接得很好。(所以没有防火墙问题)。

下一步我可以尝试什么来找出导致此问题的原因?

更新: 当我将连接字符串指向本地SQLEXPRESS数据库时,它给出了相同的错误。在本地Azure模拟器上,它仍然有效

更新: 在本地浏览器上显示的错误屏幕截图(通过RDP) http://www.proofofconcept.nl/azure_error_screenshot.png

更新: 应大众的要求。Web.config使用了

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <connectionStrings>

    <add name="ApplicationServices" connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />  
    <add name="Entities" connectionString="metadata=res://*/DAL.Coalition.csdl|res://*/DAL.Coalition.ssdl|res://*/DAL.Coalition.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|ASPNETDB.MDF;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

    <!--
    <add name="ApplicationServices" connectionString="data source=acbdefgh12.database.windows.net;Initial Catalog=MyCatalog;User ID=database_user@acbdefgh12;Password=MyPassword;Encrypt=true;Trusted_Connection=false;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
    <add name="Entities" connectionString="metadata=res://*/DAL.Coalition.csdl|res://*/DAL.Coalition.ssdl|res://*/DAL.Coalition.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=acbdefgh12.database.windows.net;Initial Catalog=MyCatalog;User ID=database_user@acbdefgh12;Password=MyPassword;Encrypt=true;Trusted_Connection=false;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
-->

  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <customErrors mode="Off" />
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="false">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

推荐答案

您是否在使用EF CodeFitst?如果是,是哪个版本?(4.3之前的版本在SQL Azure上有问题吗)?然后遵守以下条件:

您的主DbContext名为"Entities",还是使用指向"Entities"连接的构造函数重载显式创建的? 您的my_dbuser@acbDefgh12用户是否有访问主数据库的权限? 在部署包之前MyCatalog是否已存在? 您的SQL Azure防火墙规则是否"允许来自其他Windows Azure服务的连接"(请看下面的屏幕截图)(我知道您提到过,但不管怎样)?

当任一项为真时,我都看到了完全相同的错误:

my_dbuser@acbDefgh12无权访问主数据库来创建MyCatalaog数据库(如果它不存在) MyCatalog DB已存在,但与DbContext模型不同 主DbContext的命名与实体不同,并且是使用默认的无参数构造函数(在这种情况下使用默认的EF连接字符串)创建的 SQL Azure服务器未配置为接受来自其他Windows Azure服务的连接 遗憾的是,实际错误没有显示任何有用的内容。正如我所说的,当我未能遵守上述声明时,我看到了完全相同的错误。

跟踪错误的建议是挂钩/编辑自动生成的DbContext文件,并在连接打开之前使用正在使用的连接字符串显式引发异常,以确保正在使用该连接字符串。

忽略给定Web后的更新

不确定这是否相关,但我注意到,如果版本较低,则在您的web.config中,程序集绑定会强制使用MVC 3.0:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

相反,MVC 4的web.config与Web API项目类型(.NET FX 4.0/MVC 4)略有不同:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

607