Asp.net mvc Session[""]="xxx" 提供对象引用未设置为服务器上的对象实例(在本地工作的同一项目)

Posted

技术标签:

【中文标题】Asp.net mvc Session[""]="xxx" 提供对象引用未设置为服务器上的对象实例(在本地工作的同一项目)【英文标题】:Asp.net mvc Session[""]="xxx" gives Object reference not set to an instance of an object on server (same project working on local) 【发布时间】:2021-12-14 19:53:03 【问题描述】:

我正在开发一个 ASP.NET MVC 项目。我在控制器中使用过 Session。

它在我的本地运行良好。但它在服务器上给出了错误。

我该怎么办?对于本地和实时所有文件都相同,但 web.config 中仅更改了连接字符串

还要检查我的 web.config 文件。我应该添加什么?

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusivejavascriptEnabled" value="true" />
  </appSettings>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.7.2" />
      </system.Web>
  -->
  
   <system.web>
    <customErrors mode="Off" defaultRedirect="/404?error=1" redirectMode="ResponseRedirect">
    </customErrors>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.6.1" maxRequestLength="2147483647" executionTimeout="1600" requestLengthDiskThreshold="2147483647" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
      </dependentAssembly>
      <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.WebPages" 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.7.0" newVersion="5.2.7.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
      <remove name="Session" />
      <add name="Session" type="System.Web.SessionState.SessionStateModule" />
    </modules>
  </system.webServer>
  <connectionStrings>
    <!--<add name="ConnectionStringName" connectionString="Data Source=ADMIN-PC\IRIPL;Initial Catalog=Mahendra_Revamp;Integrated Security=True;Max Pool Size=50000;Pooling=True;" />-->
    <!--<add name="ConnectionStringName" connectionString="Data Source=DESKTOP-EITB23B\SQLEXPRESS;Initial Catalog=Mahindra_Uat_Live_Copy;Integrated Security=True;Max Pool Size=50000;Pooling=True;" />-->

  <add name="Mahindra_Uat_Live_IdentityConnection" connectionString="Data Source=DESKTOP-EITB23B\SQLEXPRESS; Initial Catalog=Mahindra_Uat_Live;MultipleActiveResultSets=True;integrated security=True;" providerName="System.Data.SqlClient" />
  
  <!--<add name="Mahindra_Uat_Live_IdentityConnection" connectionString="datasource=DESKTOP-EITB23B\SQLEXPRESS;initial catalog=Mahindra_Uat_Live_Copy;integrated security=True;MultipleActiveResultSets=True;App=IdentitySample.Admin" providerName="System.Data.EntityClient" />-->
    <add name="Mahendra_RevampEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-EITB23B\SQLEXPRESS;initial catalog=Mahindra_Uat_Live;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

【问题讨论】:

【参考方案1】:

你必须在你的代码中进行一些验证,至少你会知道错误来自哪里

var dealerDetails=...your code
if (dealerDetails==null) return BadRequest("dealer is not found);
Session["DealerDetailsId"]=dealerDetails.Id;

如果您仅在某些服务器上出现此错误,则必须检查您的连接字符串和数据库权限。也许您将不得不向您的 DBA 寻求帮助或检查服务器日志记录

    以管理员身份运行事件查看器 转到 Windows 日志 => 应用程序

【讨论】:

以上是关于Asp.net mvc Session[""]="xxx" 提供对象引用未设置为服务器上的对象实例(在本地工作的同一项目)的主要内容,如果未能解决你的问题,请参考以下文章

asp.net MVC Session锁的问题

Asp.net Mvc 自定义Session

Asp.net Mvc 自定义Session ,

ASP.NET MVC动作方法使用Session在其它方法中无法取值

在 ASP.NET MVC 站点中实现 Session 的公认模式是啥?

Asp.Net MVC Ajax轮训解决Session失效时间