Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Usage of wrong Tenant Database #5957


User avatar
1
kpmg created

Good morning,

we use multi tenancy with a host database and separate databases for several tenants. We encountered an issue when loading from the tenant database.

Some more details:

When a file is saved as binary (it could be other data types as well) with selected tenant X it cannot be loaded again in production. If we select the host, save and reload the file it works but it doesn't work on a tenant database.

The upload method is located in the C# Project <ProjectName>.Web.Core in a controller that is derived from <ProjectName>ControllerBase that is derived from AbpController.

The download method is located in the same C# project in FileController also derived from <ProjectName>ControllerBase. The download method is

public async Task<FileResult> DownloadBinaryObject(Guid id, string displayName)
	    {
		    var binary = await _binaryObjectManager.GetOrNullAsync(id);
			var file = new FileContentResult(binary.Bytes, System.Net.Mime.MediaTypeNames.Application.Octet);
		    file.FileDownloadName = displayName;
		    return file;
	    }

The upload method:

        [HttpPost]
        [AbpMvcAuthorize]
        public async Task<JsonResult> UploadFile()
        {
            try
            {
                var file = Request.Form.Files.First();
                byte[] fileBytes;
                using (var stream = file.OpenReadStream())
                {
                    fileBytes = stream.GetAllBytes();
                }

                var fileObject = new BinaryObject(null, fileBytes);
                using (CurrentUnitOfWork.SetTenantId(AbpSession.TenantId))
                {
                    fileObject.TenantId = AbpSession.TenantId;
                    await BinaryObjectManager.SaveAsync(fileObject);
                }
                return Json(new AjaxResponse(new
                {
                    id = fileObject.Id,
                    name = file.FileName,
                    contentType = file.ContentType
                }));
            }
            catch (UserFriendlyException ex)
            {
                return Json(new AjaxResponse(new ErrorInfo(ex.Message)));
            }
        }

How can we resolve this issue?


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    In the DownloadBinaryObject method, what is the TenantId of the AbpSession?

    and Why are you calling CurrentUnitOfWork.SetTenantId(AbpSession.TenantId) in theUploadFile

  • User Avatar
    0
    kpmg created

    If no tenant is selected, the tenant Id is null and the binary object is saved to the master database with tenant Id null. If a tenant is selected the correct tenant Id is set in the corresponding tenant database.

    Actually it is not my code but in this case it should not be a problem to set the tenant Id multiple times even if is not necessary.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @christianw

    There might be a bug here. Could you create an issue on https://github.com/aspnetzero/aspnet-zero-core/issues/new ?