'IServiceCollection' does not contain a definition for 'AddSession' [duplicate]
Asked Answered
E

6

15

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

The package is already installed in this solution.

<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

Ebro answered 7/6, 2017 at 10:3 Comment(5)
Building seems to work fine. Visual Studio might just be off here, did you try to restart it?Feltie
@HenkMollema Yes, It's throwing the same issue.Ebro
Well-known problem. Try https://mcmap.net/q/506146/-deleted-all-the-nuget-packages-in-my-local-package-cache-now-i-can-39-t-use-vs-at-allSmelt
Now it's working !! I just restarted my machine.Ebro
@llya Chumakov well known yet ignored by microsoftReverberate
E
13

The following way I fixed the Issue.

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

Thanks @HenkMollema

Ebro answered 10/6, 2017 at 8:29 Comment(0)
R
14

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!

Revelationist answered 6/11, 2017 at 9:34 Comment(1)
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.Ebro
E
13

The following way I fixed the Issue.

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

Thanks @HenkMollema

Ebro answered 10/6, 2017 at 8:29 Comment(0)
M
7

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;
Melisma answered 7/6, 2017 at 10:24 Comment(6)
Which version we can prefer for all dependencies ?Ebro
There is a list of reccomended versions here: nuget.org/packages/Microsoft.AspNetCoreMelisma
I changed to 1.1.2 but no use.Ebro
It's downloaded and the versions are now 1.1.2. I also done 'Clean' and 'Rebuild' but still I am facing the same Issue.Ebro
Yes, Three more using statements in my class using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging;Ebro
using Microsoft.Extensions.DependencyInjection; fixed it for the example service fabric application I'm looking at.Lecialecithin
C
4

Make sure you have

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

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

Cornie answered 19/12, 2017 at 17:51 Comment(4)
To much late buddy !Ebro
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 othersCornie
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 ?.Ebro
I'm not, that's why i put the parenthetical commentCornie
S
2

Run the below command in your package manager console

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection

Sumter answered 1/9, 2020 at 15:24 Comment(1)
This is a new answer to an old question but you did not explain why Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection should be run.Tracietracing
G
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

Gustaf answered 26/5, 2019 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.