MediaFileInfoProvider.GetMediaFileInfo not returning file in document event

Ashutosh Pandey asked on June 19, 2021 11:38

Hi, we are stuck in a strange problem. We have a custom form control with following code:

public partial class CMSFormControls_TechDogs_PDF_PDFSelector : FormEngineUserControl
{
    ....

    public override object Value
    {
        get
        {
            return hdnFileGUID.Value;
        }
        set
        {        
            Guid mediaGuid;
            hdnFileGUID.Value = "256ba8dc-9b48-4601-8265-72b8700db94d";
            Guid.TryParse("256ba8dc-9b48-4601-8265-72b8700db94d", out mediaGuid);
            var mediaFile = MediaFileInfoProvider.GetMediaFileInfo(mediaGuid, SiteContext.CurrentSiteName);
            lblFilePath.Text = mediaFile == null ? string.Empty : ">> " + mediaFile.FilePath;
        }             
    }

This returns correct mediaFile and we can see the proper file path.

We intentionally used hard coded file guid to test

Now, we are trying to use same code in DocumentUpdate event:

// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomDocumentsModule))]
public class CustomDocumentsModule : Module
{

// Module class constructor, the system registers the module under the name "CustomInit"
public CustomDocumentsModule()
    : base("CustomDocuments")
{
}

// Contains initialization code that is executed when the application starts
protected override void OnInit()
{
    base.OnInit();
    ...
    DocumentEvents.Update.After += Document_Update_After;
}

private void Document_Update_After(object sender, DocumentEventArgs e)
{
    try
    {
        Task.Run(() => PushPdfToS3(e.Node));
    }
    catch (Exception ex)
    {
        EventLogProvider.LogEvent(EventType.INFORMATION, "Document_Events", "Update_After", "Failed to push pdf to s3");
    }
}    

private async void PushPdfToS3(TreeNode node)
{
    if (node != null)
    {
        string guid = node.GetStringValue("Pdf", string.Empty);

        if (guid == string.Empty){
            EventLogProvider.LogEvent(EventType.INFORMATION, "Document_Events guid", "Guid not found");
            return;
        }

        Guid mediaGuid;
        Guid.TryParse("256ba8dc-9b48-4601-8265-72b8700db94d", out mediaGuid);
        var mediaFile = MediaFileInfoProvider.GetMediaFileInfo(mediaGuid, SiteContext.CurrentSiteName);

        if(mediaFile == null){
            EventLogProvider.LogEvent(EventType.INFORMATION, "Document_Events pdf", "Cannot find media file");
            return;
        }

        string filePath = mediaFile.FilePath;
        ....
    }
}                    

This code returns mediaFile as null where as both the code are same.

What is wrong? Please help

Recent Answers


Ashutosh Pandey answered on June 19, 2021 13:36

I removed the async from method and moved Task.Run() and it works now.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.