question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AbpSession.TenantId is not set when saving audit logs in background

See original GitHub issue

Hi guys,

I’am having an intermittent issue where AbpSession.TenantId is not set, even thought that client is sending the correct TenantId per request.

code behind GetItem:

public async Task<AdDto> GetItem(GetAdInput input)
        {
            var culture = await _applicationLanguageRepository.GetCultureAsync(AbpSession.TenantId);

            var sessionId = Extensions.GetGuid(input.SessionId);

            var ad = await _adRepository.SingleOrDefaultAsync(input.AdId, sessionId, AbpSession.UserId, input.IsEdit, culture.TwoLetterISOLanguageName);

            if (ad == null)
            {
                throw new UserFriendlyException("There is no such an ad.");
            }

            if (ad.TenantId != AbpSession.TenantId && ad.TenantId.HasValue)
            {
                throw new UserFriendlyException("Tenant session is not valid.");
            }

            if (ad.Status == AdStatus.Active)
            {
                ad.Slug = ad.Title.GenerateSlug();
            }

            var adOutput = ad.MapTo<AdDto>();
}

As you can see in the image below that the first request for GetItem?AdId=1000021352&isEdit=false was succeed without error but the second request failed with the error Tenant session is not valid

image

Abp version: V4.0.0 Template base: .Net Framework

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
malimingcommented, Nov 1, 2018

This is indeed due to an error caused by RunInBackground. Of course this problem is not easy to find because it does not cause a task exception log.

The problem is in the class HttpContextTenantResolverCache. My understanding is that the background thread incorrectly sets the contents of httpContext.Items [CacheItemKey] Causes random problems that cannot read TenandId correctly.

https://github.com/aspnetboilerplate/aspnetboilerplate/blob/e0ded5d8702f389aa1f5947d3446f16aec845287/src/Abp.AspNetCore/AspNetCore/MultiTenancy/HttpContextTenantResolverCache.cs#L7

If someone can share specific details inside, I am very interested to learn. 🙂

1reaction
malimingcommented, Nov 1, 2018

@armory09 Sorry, I have reproduced the problem you have encountered, please wait a moment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

set tenant Session #6269
SaveAsync() is called. At this stage AbpSession is null. The result is that the audit log gets written to the audit log for...
Read more >
How to assign TenantId to abpsession in login process API?
For login used TokenAuthController/Authenticate as post request, it's logging but cannot be getting the Tenant details there. As the AbpSession.
Read more >
Audit Logging | Documentation Center | ABP.IO
Wikipedia: "An audit trail (also called audit log) is a security-relevant chronological record, set of records, and/or destination and source of records ...
Read more >
Overriding Current Session Values
Introduction. ASP.NET Boilerplate provides an IAbpSession interface to obtain the current user and tenant without using ASP.NET's Session.
Read more >
Custom Audit Log Entries not saving/creating #9830
I have a custom Audit class as below. When I try to call and save to the Audit log no audit data si...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found