Coder Social home page Coder Social logo

Comments (11)

davidfowl avatar davidfowl commented on May 24, 2024

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.

leonardlabat avatar leonardlabat commented on May 24, 2024

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.

davidfowl avatar davidfowl commented on May 24, 2024

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.

leonardlabat avatar leonardlabat commented on May 24, 2024

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.

Tratcher avatar Tratcher commented on May 24, 2024

@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.

muratg avatar muratg commented on May 24, 2024

@davidfowl Do you still want this?

from session.

muratg avatar muratg commented on May 24, 2024

@davidfowl

from session.

muratg avatar muratg commented on May 24, 2024

@davidfowl Putting this in backlog. We can discuss later if we want to do something around this.

from session.

davidfowl avatar davidfowl commented on May 24, 2024

Can close

from session.

ronnieoverby avatar ronnieoverby commented on May 24, 2024

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.

davidfowl avatar davidfowl commented on May 24, 2024

Sure it's fine as long as you access it when the HttpContext is valid.

from session.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.