Hello, When i try to download user collected data. It is giving the error. Abp.AbpException: 'Session.UserId is null! Probably, user is not logged in.'
this is in ChatMessageListExcelExporter class. When it is working as a background job.
public FileDto ExportToFile(List<ChatMessageExportDto> messages)
{
var tenancyName = messages.Count > 0 ? messages.First().TargetTenantName : L("Anonymous");
var userName = messages.Count > 0 ? messages.First().TargetUserName : L("Anonymous");
return CreateExcelPackage(
$"Chat_{tenancyName}_{userName}.xlsx",
excelPackage =>
{
var sheet = excelPackage.Workbook.Worksheets.Add(L("Messages"));
sheet.OutLineApplyStyle = true;
AddHeader(
sheet,
L("ChatMessage_From"),
L("ChatMessage_To"),
L("Message"),
L("ReadState"),
L("CreationTime")
);
AddObjects(
sheet, 2, messages,
_ => _.Side == ChatSide.Receiver ? (_.TargetTenantName + "/" + _.TargetUserName) : L("You"),
_ => _.Side == ChatSide.Receiver ? L("You") : (_.TargetTenantName + "/" + _.TargetUserName),
_ => _.Message,
_ => _.Side == ChatSide.Receiver ? _.ReadState : _.ReceiverReadState,
_ => _timeZoneConverter.Convert(_.CreationTime, _abpSession.TenantId, _abpSession.GetUserId())
);
//Formatting cells
var timeColumn = sheet.Column(5);
timeColumn.Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";
});
}
here is used _abpSession which is null on the background thread. I have this issue when i have a chat history. When i try to download collected data for the user.
any suggestions?
5 Answer(s)
-
0
Because the session cannot be get in the background job. But may be you can use
AbpSession.Use (tenant, user)
Please share the complete code of your background job. Such as
ChatMessageListExcelExporter
calss etc. -
0
Hello again, i know that session can not be used :) but the job is triggered from aspnetzero code :) when i download aspnetzero from scratch it comes with the code and you can find it below and it has been triggered from ProfileAppService i didn't write extra code for ChatMessageListExcelExporter. Just look at the picture that i posted i have just downloaded the project and start it nothing else i have done on the project. I post this so you can report the bug i was thinking.
public async Task PrepareCollectedData() { await _backgroundJobManager.EnqueueAsync<UserCollectedDataPrepareJob, UserIdentifier>(AbpSession.ToUserIdentifier()); }
public class UserCollectedDataPrepareJob : BackgroundJob<UserIdentifier>, ITransientDependency { private readonly IBinaryObjectManager _binaryObjectManager; private readonly ITempFileCacheManager _tempFileCacheManager; private readonly IAppNotifier _appNotifier; private readonly ISettingManager _settingManager; public UserCollectedDataPrepareJob( IBinaryObjectManager binaryObjectManager, ITempFileCacheManager tempFileCacheManager, IAppNotifier appNotifier, ISettingManager settingManager) { _binaryObjectManager = binaryObjectManager; _tempFileCacheManager = tempFileCacheManager; _appNotifier = appNotifier; _settingManager = settingManager; } [UnitOfWork] public override void Execute(UserIdentifier args) { using (UnitOfWorkManager.Current.SetTenantId(args.TenantId)) { var userLanguage = AsyncHelper.RunSync(() => _settingManager.GetSettingValueForUserAsync(LocalizationSettingNames.DefaultLanguage, args.TenantId, args.UserId)); var culture = CultureHelper.GetCultureInfoByChecking(userLanguage); using (CultureInfoHelper.Use(culture)) { var files = new List<FileDto>(); using (var scope = IocManager.Instance.CreateScope()) { var providers = scope.ResolveAll<IUserCollectedDataProvider>(); foreach (var provider in providers) { var providerFiles = AsyncHelper.RunSync(() => provider.GetFiles(args)); if (providerFiles.Any()) { files.AddRange(providerFiles); } } } var zipFile = new BinaryObject { TenantId = args.TenantId, Bytes = CompressFiles(files) }; // Save zip file to object manager. AsyncHelper.RunSync(() => _binaryObjectManager.SaveAsync(zipFile)); // Send notification to user. AsyncHelper.RunSync(() => _appNotifier.GdprDataPrepared(args, zipFile.Id)); } } } private byte[] CompressFiles(List<FileDto> files) { using (var outputZipFileStream = new MemoryStream()) { using (var zipStream = new ZipArchive(outputZipFileStream, ZipArchiveMode.Create)) { foreach (var file in files) { var fileBytes = _tempFileCacheManager.GetFile(file.FileToken); var entry = zipStream.CreateEntry(file.FileName); using (var originalFileStream = new MemoryStream(fileBytes)) using (var zipEntryStream = entry.Open()) { originalFileStream.CopyTo(zipEntryStream); } } } return outputZipFileStream.ToArray(); } } }
PS: And to see the bug you need to have a chat conversation .
-
0
I will try to reproduce the problem and fix it, thanks for your feedback.
-
0
Please follew: https://github.com/aspnetzero/aspnet-zero-core/pull/2911
-
0
Thank you @maliming