Click here to Skip to main content
14,826,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two .net core 2.2 services, InitialisationHandler and WorkingTicketHandler, one holds initial information and the other is intended to hold a working ticket.
The InitialisationHandler always has its information ready when asked, but the WorkingTicketHandler always returns a null record.

I need to figure out how to have WorkingTicketHandler hold on to its data just like InitialisationHandler does.

using VTModels.Other;

namespace VT_Online22.Infrastructure.Services
{
    /// <summary>
    /// Interface for the WorkingTicket Service
    /// </summary>
    public interface IWorkingTicketHandler
    {
        WorkingTicket GetWorkingTicket();
        void SaveWorkingTicket(WorkingTicket  argWorkingTicket);
        void ClearWorkingTicket();
    }

    /// <summary>
    /// WorkingTicket service class definition
    /// </summary>
    public class WorkingTicketHandler : IWorkingTicketHandler
    {

        public WorkingTicketHandler()
        {
            this.WorkingTicket = new WorkingTicket();
        }

        public WorkingTicket GetWorkingTicket()
        {
            return this.WorkingTicket;
        }

        public void SaveWorkingTicket(WorkingTicket arg_WorkingTicket)
        {
            this.WorkingTicket = arg_WorkingTicket;
        }

        public void ClearWorkingTicket()
        {
            this.WorkingTicket = new WorkingTicket();
        }

        private WorkingTicket WorkingTicket;
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(WorkingTicketHandler));
    }
}



using Microsoft.Extensions.Configuration;
using SharedModels.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
using VT_OnlineCore.Infrastructure.Other;
using VTModels.Models;

namespace VT_Online22.Infrastructure.Services
{
    /// <summary>
    /// Interface for the Initialization Service
    /// </summary>
    public interface IInitializationHandler
    {
        void Initialize();
        CConfigMasterBase GetConfig();
        IEnumerable<CScopeMasterBase> GetScopes(string clientId);
    }

    /// <summary>
    /// Initialization service class definition
    /// </summary>
    public class InitializationHandler : IInitializationHandler
    {

        public InitializationHandler(IConfiguration configTech)
        {
            ConfigurationTech = configTech;
            Initialize();
        }

        public void Initialize()
        {
            this.ldh = new LocalDataHandler(logger, this.ConfigurationTech);
            this.config = new CConfigMasterBase();
            LoadConfig();
        }

        public IEnumerable<CScopeMasterBase> GetScopes(string clientId)
        {
            IEnumerable<CScopeMasterBase> scopes = new List<CScopeMasterBase>();
            scopes =  this.ldh.GetScopes(clientId);
            return scopes;
        }
        public CConfigMasterBase GetConfig()
        {

            return this.config;
        }

        private async Task LoadConfig()
        {
            this.config =   this.ldh.GetConfig();           
        }

        private LocalDataHandler ldh;
        private CConfigMasterBase config;
        private IEnumerable<CScopeMasterBase> scopes;
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(InitializationHandler));

        public IConfiguration ConfigurationTech { get; private set; }
    }
}



services.AddTransient<IInitializationHandler,InitializationHandler>();
services.AddTransient<IWorkingTicketHandler, WorkingTicketHandler>();


What I have tried:

Both are added to as transient in ConfigureServices, and created as identically as possible.

There is one key difference, the InitialisationHandler has its data created during startup by its constructor method and is only ever read from thereafter, whereas the WorkingTicketHandler is written to by a controller and re read in the same controller (Get vs Post).
Posted
Updated 20-Feb-20 2:15am
v2

1 solution

The only way the GetWorkingTicket method will return null is if you first call SaveWorkingTicket(null) on the same instance.

I assume you meant that it returns a blank WorkingTicket instance? The value you store in the field won't be persisted between requests - each request creates a new instance of your controller class, which will receive a new instance of your IWorkingTicketHandler service.

Making this a singleton service won't work either. That would mean a single working ticket was shared between every user of your service.

You'll need to use session state to store the value between requests.

Session and app state in ASP.NET Core | Microsoft Docs[^]
   
Comments
Ger Hayden 20-Feb-20 8:26am
   
Hi Richard, I have been using session state with some success - I was trying to achieve the same effect using a service.
Richard Deeming 20-Feb-20 8:31am
   
The service will still need to use session state, unless you use a custom DI container which supports a "per-session" lifetime.

It looks like you might need to inject the IHttpContextAccessor service to access the session from your service class.
Ger Hayden 20-Feb-20 9:00am
   
I'm going to have to think about this. Session has privacy issues, and I would have made those worse if I have got the service approach to work. Maybe I need to persist that information to a table. That is something that could morph into a feature - allowing a user to save a purchase without completing it and return to it later.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900