For my application I created a MailService class in the Application project, the service creates a connection to a IMAP server, to get emails. In order to prevent constant reconnecting with each request, I need to save the object who is responsible for the connection in Session state. The object is not serializable and needs to be retrieved as soon as possble. Therefore I want to use Inproc session.
According to documentation the following should work:
class MailService : MyAppServiceBase, IMailService
{
private MailKitHelper _mailKitHelper;
public MailService()
{
// try to get MailKitHelper object via inproc session state
if (HttpContext.Current.Session != null)
_mailKitHelper = (MailKitHelper)HttpContext.Current.Session["MailKitHelper"];
However it doesn't, the Session object is null. What's the best way to solve this problem, any help would be greatly appricated?
2 Answer(s)
-
0
Hi,
Why don't you make your service singleton, just derive it from ISingletonDependecy. In this way, it will be created only once and MailKitHelper will be created for MailService one time.
-
0
Thanks for your reply,
I need a MailKitHelper object for each tenant, so making singleton would not be a good choice.
Update: I basically solved it now by using MemoryCache