question

net6newbie-4004 avatar image
0 Votes"
net6newbie-4004 asked AgaveJoe edited

ISession Extension methods

For creating/adding sessions I am using

  1. app.UseSession(); // middleware


  2. registering services as
    builder.Services.AddSession(options =>
    {
    options.Cookie.Name = "Session";
    options.IdleTimeout = TimeSpan.FromSeconds(500);
    options.Cookie.HttpOnly = true;
    options.Cookie.IsEssential = true;
    options.Cookie.SameSite = SameSiteMode.Lax;
    });

It is working fine. But my question is

the extension methods SetString(),GetString() on ISession interface are not found in the app.UseSession() middleware.
Then how this extension methods are working.

I have searched the src files in the following link but could not find where the extension methods are declared.

https://github.com/dotnet/aspnetcore/tree/main/src/Middleware/Session


dotnet-aspnet-core-generaldotnet-aspnet-core-auth
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your goal is not clear.

AddSession() configures the middleware when the application starts. SetString() and GetString() are accessed during runtime in a Razor Page, controller, or passed to a type via constructor injection.

Are you trying to build custom middleware that needs access to session? Can you explain the use case?

0 Votes 0 ·

I want to know where the SetString() and GetString() extension methods/Functions are declared.
ISession by default has the following code

public interface ISession
{

     bool IsAvailable { get; }
        
     string Id { get; }
       
     IEnumerable<string> Keys { get; }

       
     void Clear();
       
     Task CommitAsync(CancellationToken cancellationToken = default);
        
     Task LoadAsync(CancellationToken cancellationToken = default);
       
     void Remove(string key);
        
     void Set(string key, byte[] value);
        
     bool TryGetValue(string key, [NotNullWhen(true)] out byte[]? value);
 }
0 Votes 0 ·
AgaveJoe avatar image AgaveJoe net6newbie-4004 ·

I want to know where the SetString() and GetString() extension methods/Functions are declared.

You want the community to find the source on github for you???

https://github.com/dotnet/aspnetcore/blob/a450cb69b5e4549f5515cdb057a68771f56cefd7/src/Http/Http.Extensions/src/SessionExtensions.cs

If you're asking how to reference the methods, then you need to add "using Microsoft.AspNetCore.Http" to the top of the page as explained in the reference documentation.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-6.0#set-and-get-session-values

Extension Methods (C# Programming Guide)


0 Votes 0 ·
Show more comments

0 Answers