'IServiceCollection' does not contain a definition for 'AddSession'

我与影子孤独终老i 提交于 2020-01-12 14:03:14

问题


I am getting an error while adding 'AddSession' in ASP.Net Core 1.1 using VS2017.

'IServiceCollection' does not contain a definition for 'AddSession' and no extension method 'AddSession' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

<PropertyGroup>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
</PropertyGroup>
 <ItemGroup>
  <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
  <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
 </ItemGroup>
 <ItemGroup>
 <DotNetCliToolReference 
 Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>

 </Project>

Error :


回答1:


You need to include following Nuget package

AutoMapper.Extensions.Microsoft.Dependencyinjection




回答2:


I know this is a bit late, but did you try to install the Microsoft.AspNetCore.Session package from nuget?

https://www.nuget.org/packages/Microsoft.AspNetCore.Session/

In Visual Studio: Install-Package Microsoft.AspNetCore.Session -Version 1.1.2(in my case)

It worked for me!




回答3:


The following way I fixed the Issue.

  1. Clean and Rebuild the solution.
  2. Restart Visual Studio 2017.

Thanks @HenkMollema




回答4:


The versions of the dependencies seem to be out of sync. Use Nuget to update all your packages to be the same version and that should solve the issue. This applies to most if not all Microsoft.* packages.

Also you need the following using statements:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;



回答5:


Make sure you have

<PackageReference Include="Microsoft.AspNetCore.Session" Vesion="1.1.1" />

(or more up-to-date version) within your .csproj file




回答6:


In my case AutoMapper.Extensions.Microsoft.DependencyInjection (v1 was installed). Uninstalled and installed AutoMapper.Extensions.Microsoft.DependencyInjection v5.0.1




回答7:


It looks like you missed something. In Package Manager Console, write:

install-package Microsoft.AspNetCore.Session -version x.x.x

x.x.x depends on your project reference




回答8:


Adding the nuget package for Scrutor fixed it for me



来源:https://stackoverflow.com/questions/44409554/iservicecollection-does-not-contain-a-definition-for-addsession

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!