12

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 :

enter image description here

Pang
  • 8,605
  • 144
  • 77
  • 113
RajeeshMenoth
  • 999
  • 2
  • 12
  • 27

7 Answers7

13

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!

Lewis86
  • 481
  • 6
  • 15
  • The package is already installed in the project and that you can see in my given .csproj code. Any how everything working fine and this is not an answer for me. – RajeeshMenoth Dec 14 '17 at 07:27
7

The following way I fixed the Issue.

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

Thanks @HenkMollema

RajeeshMenoth
  • 999
  • 2
  • 12
  • 27
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;
Stephen Wilson
  • 1,362
  • 1
  • 16
  • 30
2

Make sure you have

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

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

Pang
  • 8,605
  • 144
  • 77
  • 113
coolhand
  • 1,357
  • 2
  • 15
  • 34
  • To much late buddy ! – RajeeshMenoth Dec 21 '17 at 04:18
  • This is meant for others who come across this post with a similar error, but different root cause. If rebooting was the fix, I would suggest deleting this question/answer as it is probably of limited use to others – coolhand Dec 26 '17 at 21:36
  • Whenever people posting any questions in SO , they will mention the exact reason of the bug. Question 4 U , "Session" package already existing in the given code then why are you trying to downgrade the package version without any reason ?. – RajeeshMenoth Dec 28 '17 at 10:33
  • I'm not, that's why i put the parenthetical comment – coolhand Dec 28 '17 at 14:21
0

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

Patrick
  • 1,545
  • 12
  • 20
0

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

Daleman
  • 609
  • 7
  • 15
-1

Run the below command in your package manager console

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection

  • 2
    This is a new answer to an old question but you did not explain why `Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection` should be run. – Mech Sep 02 '20 at 01:31