Coder Social home page Coder Social logo

Comments (7)

davidfowl avatar davidfowl commented on June 4, 2024

Session is always backed by an IDistributedCache implementation. IMemoryCache is a different interface completely. Are you asking if you can use 2 implementations of session within the same app or are you asking if you can configure session to use the SQL implementation of IDistributedCache and also use IMemoryCache for other things?

If it's the latter, then yes, because those are 2 different interfaces that becomes trivial.

from session.

Tratcher avatar Tratcher commented on June 4, 2024

Sounds like he wants IDistributedCache for session and IMemoryCache for other stuff. Yes, that's just fine. Go look at the cache repo samples.

from session.

SimonOrdo avatar SimonOrdo commented on June 4, 2024

Right, so the sample at https://github.com/aspnet/Session/blob/dev/samples/SessionSample/Startup.cs says to do something like this:

// Uncomment the following line to use the Microsoft SQL Server implementation of IDistributedCache.
        // Note that this would require setting up the session state database.
        //services.AddSqlServerCache(o =>
        //{
        //    o.ConnectionString = "Server=.;Database=ASPNET5SessionState;Trusted_Connection=True;";
        //    o.SchemaName = "dbo";
        //    o.TableName = "Sessions";
        //});

I'm probably completely misinterpreting this, but to me that implies that in order to have SQL backed sessions, I need to change caching to also use a SQL store.

I I have all three:

services.AddSqlServerCache(); // add SQL backed implementation of IDistributedCache ?
services.AddCaching(); // add IMemoryCache,IDistributedCache for general in-proc caching?
services.AddSession(); 

The sample implies that without line 1 AddSqlServerCache(), the call to AddSession() results in the use of the IMemoryCache added from the call to AddCaching()?

How does AddSession know to use the SQL IDistributedCache instead of the IMemoryCache?

Am I just completely confused about how any of this works? LOL!

from session.

Tratcher avatar Tratcher commented on June 4, 2024

Session always uses IDistributedCache
https://github.com/aspnet/Session/blob/dev/src/Microsoft.AspNet.Session/SessionServiceCollectionExtensions.cs#L27
https://github.com/aspnet/Session/blob/dev/src/Microsoft.AspNet.Session/DistributedSessionStore.cs#L16
AddCaching adds a IDistributedCache implemented using IMemoryCache:
https://github.com/aspnet/Caching/blob/dev/src/Microsoft.Extensions.Caching.Memory/MemoryCacheServicesExtensions.cs#L23
If you replace that IDistributedCache registration with AddSqlServerCache then you use SQL distributed cache, but IMemoryCache is still registered as a seperate service.

from session.

SimonOrdo avatar SimonOrdo commented on June 4, 2024

Oh, I see. So the order in which I register them is key to making this work? Specifically, for my scenario it needs to be:

AddCaching(); // Add IMemoryCache and IDistributedCache
AddSqlServerCache(); // overwrite IDistributedCache registration from AddCaching with SQL backing
AddSession(); // which will now resolve to using SQL backed IDistributedCache

Is that about right?

from session.

Tratcher avatar Tratcher commented on June 4, 2024

Yes, that should work.

from session.

SimonOrdo avatar SimonOrdo commented on June 4, 2024

Phew! Thank for your time! I appreciate the help.

As a side note, it would be cool if the "AddServiceX" extension methods had some easy way to see what services they register without having to find the source (maybe in the method comments).

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.