TransferOperationResult disposed before I can serialize it.

Advertisement

dlambert
Joined:
Posts:
4
Location:
Ohio

TransferOperationResult disposed before I can serialize it.

I have a method that performs an FTP put and returns results to the caller in the form of a TransferOperationResult object. The intent is to write these results as a JSON doc and use success & failure counts to evaluate overall success.

The method doing the put is pretty straightforward:
TransferOperationResult? result = null;
try
{
    SessionOptions sessionOptions = new SessionOptions
    {
        ...
    };
 
    using (Session session = new Session())
    {
        // Connect
        session.Open(sessionOptions);
 
        // Upload files
        TransferOptions transferOptions = new TransferOptions();
        transferOptions.TransferMode = TransferMode.Binary;
        transferOptions.OverwriteMode = OverwriteMode.Overwrite;
        transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
 
        result = session.PutFiles(fromPath, toPath, false, transferOptions);
    }
}
catch (Exception ex)
{
    _logger.LogError("Exception in ftp CopyToDestination: {0}", ex.ToString());
}
return result;
In the calling routine, I'm serializing result to JSON. Most of the time, this works great, but every once in a while, I see "System.InvalidOperationException: Object is disposed" when writing result to JSON.

With result declared outside the using block, I didn't think result was going to be disposed, but it seems to be doing just that. Does anyone see anything *obvious* I'm doing wrong?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,453
Location:
Prague, Czechia

Re: TransferOperationResult disposed before I can serialize it.

I believe that the only disposable element of TransferOperationResult is the reference to the Session itself in TransferOperationResult.Failures[...].Session. So if you are serializing whole TransferOperationResult object, your code will fail whenever there's any "failure". Skip the Session object when serializing the results.

Reply with quote

dlambert
Joined:
Posts:
4
Location:
Ohio

Consider an attribute to exclude from serialization?

Thanks for your help, Martin. I don't believe I can exclude that property with a broad brush – many of the properties I want are readonly, so I can use a serialization option like IgnoreReadOnlyProperties = true, for instance, per these guidelines:
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/ignore-properties

Would you consider adding a JSON serialization decoration like [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] to the WinSCP.SessionException.Session property?

Reply with quote

dlambert

I've got a workaround

FYI – I moved the serialization of FTP results inside the Session using {} block – this seems to be an ok workaround for me right now. I think this is still a minor gotcha, but I'm not in dire need of a fix right now.

Reply with quote

Advertisement

dlambert
Joined:
Posts:
4
Location:
Ohio

Workaround

My entire flow does a bunch of work in addition to the FTP I described above. Originally, I'd returned the FTP results to an orchestrator that determines whether each step in the process worked. That orchestrator also handled writing the report to JSON, since it knows about config options for verbosity, logging folders, and so on.

My workaround is to have the routine above also handle writing the JSON output. Not as clean for reasons above, but it seems to be working.

Reply with quote

Advertisement

You can post new topics in this forum