0

I have two projects:

  1. ASP.NET webforms (.NET 4.8)
  2. ASP.NET Core Web API (.NET 6.0)

There are many session values which are used by the ASP.NET webforms app. Since the ASP.NET Core Web API application would be running side-by-side, I need to access these session values in the .NET Core project, too.

I followed the instruction given in

Session sharing between Asp.Net Webforms and Asp.Net Core

but I got Null values in the .NET Core Web API project every time.

Here is my code snippet for the ASP.NET application

Web.config:

I have installed Microsoft.AspNetCore.SystemWebAdapters and Microsoft.AspNet.SessionState:

enter image description here

Global.asax:

enter image description here

Setting a session value:

enter image description here

Changes made to the ASP.NET Core Web API:

Program.cs:

enter image description here

appsettings.json:

enter image description here

Code to retrieve the session value:

enter image description here

Let me know if I am missing anything here.

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aijaz Chauhan
  • 1,511
  • 3
  • 25
  • 53

1 Answers1

1

I am checking the source code here, and this is the video.

Please quick test, first

Please open the Contact.aspx page first, make sure the code Session["Test"] = "Hello World"; was excuted.

My Suggestions

The sample code your provided I found 2 issues, but your asp.net web application could be working well and youe asp.net core web app should get error like below

An unhandled exception occurred while processing the request.
ArgumentException: An item with the same key has already been added. Key: Test
System.Collections.Generic.Dictionary<TKey, TValue>.TryInsert(TKey key, TValue value, InsertionBehavior behavior)

Please change it and check it works or not.

In ASP.NET Global.asax.cs

Change

.AddJsonSessionSerializer(options =>
                {
                    // Serialization/deserialization requires each session key to be registered to a type
                    options.RegisterKey<string>("Test");
                    options.KnownKeys.Add("Test", typeof(string));
                })

to

.AddJsonSessionSerializer(options =>
                {
                    // Serialization/deserialization requires each session key to be registered to a type
                    options.RegisterKey<string>("Test");
                    //options.KnownKeys.Add("Test", typeof(string));
                })

In ASP.NET Core Program.cs

Change

.AddJsonSessionSerializer(options =>
    {
        options.RegisterKey<string>("Test");
        options.KnownKeys.Add("Test", typeof(string));
        
    })

to

.AddJsonSessionSerializer(options =>
    {
        //options.RegisterKey<string>("Test");
        options.KnownKeys.Add("Test", typeof(string));
    })

The package in my project.

Asp.net core

enter image description here

Asp.net

enter image description here

If it still not works, please share a Minimal, Reproducible Example, so that I could help you.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • i made the changes you asked but still no luck. i have put my sample code at https://github.com/AijazChauhan/SessionShare please check and let me know if i am missing out there – Aijaz Chauhan May 04 '23 at 12:25
  • please review the code from link i have shared i above comment – Aijaz Chauhan May 04 '23 at 12:32
  • Hi @AijazChauhan , will check it tomorrow – Jason Pan May 04 '23 at 14:22
  • 1
    Hi @AijazChauhan, I have compared the two repos, my working on is using asp.net core mvc, yours is swagger api. I think this should be the expected behavior because [APIs are stateless](https://stackoverflow.com/a/3105337/7687666) – Jason Pan May 05 '23 at 02:13
  • It make sense to me as well. Thank you @JasonPan for your efforts – Aijaz Chauhan May 05 '23 at 11:45
  • @AijazChauhan If my findings useful to you,could you accept it as answer,tks, have a nice day – Jason Pan May 05 '23 at 14:07