Comments (11)
I'm 1000% for this behavior (see aspnet/HttpAbstractions#488). Do you have any code samples showing how you're testing session right now?
from session.
That'how my tests are looking with session out of the box
public class HomeControllerTests
{
[Fact]
public void GetCorrectValueFromSession()
{
var session = new Mock<ISession>();
byte[] bytes = Encoding.UTF8.GetBytes("leo");
session.Setup(x => x.TryGetValue("userName", out bytes));
var homeController = new HomeController(new SessionFeature { Session = session.Object });
var result = homeController.Index();
Assert.NotNull(result);
var jsonResult = result as JsonResult;
Assert.NotNull(jsonResult);
var name = jsonResult.Value as string;
Assert.NotNull(name);
Assert.Equal("leo", name);
}
}
That's how I want it to look like
public class HomeControllerTests
{
[Fact]
public void GetCorrectValueFromSession()
{
var session = new Mock<ISession>();
byte[] bytes = Encoding.UTF8.GetBytes("leo");
session.Setup(x => x.TryGetValue("userName", out bytes));
var homeController = new HomeController(session.Object);
var result = homeController.Index();
Assert.NotNull(result);
var jsonResult = result as JsonResult;
Assert.NotNull(jsonResult);
var name = jsonResult.Value as string;
Assert.NotNull(name);
Assert.Equal("leo", name);
}
}
And how I'm achieving it
services.AddTransient(typeof(ISession), serviceProvider =>
{
var sessionFeature = serviceProvider.GetService<ISessionFeature>();
return sessionFeature.Session;
});
Thanks
from session.
This looks pretty wrong:
services.AddTransient(typeof(ISession), serviceProvider =>
{
var sessionFeature = serviceProvider.GetService<ISessionFeature>();
return sessionFeature.Session;
});
How does that even work? There's no ISessionFeature
service in the DI container. Do you have a working application somewhere? (preferably github)
from session.
Oh... I tried to do it by memory and I got wrong. I'm doing it throught the IHttpContextAccessor. But I you really plan to remove it from the HttpContext, the trick won't work anymore.
services.AddTransient(typeof(ISession), serviceProvider =>
{
var httpContextAccessor = serviceProvider.GetService<IHttpContextAccessor>();
return httpContextAccessor.HttpContext.Session;
});
from session.
@davidfowl, @leonardlabat's use of IHttpContextAccessor is about the only way to implement this. Session requires access to request and response cookies and state (e.g. has the response started yet).
from session.
@davidfowl Do you still want this?
from session.
from session.
@davidfowl Putting this in backlog. We can discuss later if we want to do something around this.
from session.
Can close
from session.
What about registering a factory that will get the session via the IHttpContextAccessor
?
services.AddScoped(sp => sp.GetService<IHttpContextAccessor>().HttpContext.Session);
Is this safe to do? I'm wondering if there's some gotcha with the sync context/task scheduler/etc that would cause me to get the wrong user's session. 😨
If this is viable, one could depend on ISession
directly and simplify their code / tests.
from session.
Sure it's fine as long as you access it when the HttpContext is valid.
from session.
Related Issues (20)
- Remove obsolete options APIs HOT 3
- Update session to configure DistributedMemoryCache options by default HOT 3
- Add logging scope for session HOT 2
- Random "Accessing expired session" warning HOT 4
- Error closing the session with .net 2.1.0-preview1 HOT 12
- Any plans for maximum session size property? HOT 2
- [1.1.4] Flaky test: SessionTests.SessionLogsCacheRefreshException HOT 6
- System.OperationCanceledException at Microsoft.Extensions.Caching.Redis.RedisCache+<RefreshAsync>d__25.MoveNext HOT 4
- cant access Session in other API HOT 5
- Error closing the session HOT 1
- [Patch 2.0.x] Downgrade cancellation logs HOT 1
- Add EventSource/EventCounter tracing and metrics for Session HOT 1
- Session does not work with Redis when the client has HTTP keep-alives disabled HOT 6
- Session cannot be re-loaded HOT 8
- Determine if the session cookie is considered essential HOT 1
- THIS ISSUE TRACKER IS CLOSED - use the Home repo issue tracker
- Session OnStart and Session OnEnd HOT 8
- How to get number of session HOT 3
- Re-initialize Session middleware from controller HOT 2
- How to add a custom session in .net core? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from session.