Unchase.Odata.Connectedservice icon indicating copy to clipboard operation
Unchase.Odata.Connectedservice copied to clipboard

Exception in property changed event

Open dazinator opened this issue 1 year ago • 0 comments

Describe the bug

I have generated the client based on metadata file downloaded from a vanilla dev instance of Microsoft Dynamics 365 Finance and Operations.

I ticked the option to generate property change tracking, and also "async base" for c# only (not sure what that means precisely. I should also say, I am runnign the version from this PR: https://github.com/unchase/Unchase.Odata.Connectedservice/pull/87 as that fixes an issue with spaces being in generated method names.

I wrote a quick console app to new up the client, and post a header record with a related child record, using a single change set.

I haven't managed to get this to work just yet, I have been able to post a header on its own however thats a seperate issue. Sometimes when I run the app I see this:

image

Here is how I am using:

       var serviceRootUri = new Uri($"{url}/data");
       var dataServiceContext = new Resources(serviceRootUri);
       dataServiceContext.SendingRequest2 += (sender, eventArgs) =>
        {
            var request = eventArgs.RequestMessage;        
            eventArgs.RequestMessage.SetHeader("Authorization", $"Bearer {token.Token}");
        };
        
        var item = new Microsoft.Dynamics.DataEntities.LedgerJournalHeader();      
        // dataServiceContext.AddToLedgerJournalHeaders(item); // in order to use PostOnlySetProperties it seems necessary to use DataServiceCollection instead.
             
        var col = new DataServiceCollection<Microsoft.Dynamics.DataEntities.LedgerJournalHeader>(dataServiceContext);    
        col.Add(item);

       // after adding the item to the collection, we sets its properties, this is so that change tracking works, and PostOnlySetProperties works.
        item.DataAreaId = "DEMF";
        item.JournalName = "GenJrn";
        item.Description = "Posted by poc";      

        var linesCol = new DataServiceCollection<Microsoft.Dynamics.DataEntities.LedgerJournalLine>(dataServiceContext);
        for (int i = 0; i < 2; i++)
        {
            var amount = 100000;
            if (i % 2 == 0)
            {
               
            }
            else
            {
                amount = -amount;
            }

            var line = new Microsoft.Dynamics.DataEntities.LedgerJournalLine();
            linesCol.Add(line);
            // after adding the item to the collection, the following just sets its propertie, this is so that change tracking works, and PostOnlySetProperties works.
            SetLine(line, item.DataAreaId, item.JournalBatchNumber, amount);

            dataServiceContext.AddLink(item, nameof(item.LedgerJournalLine), line);
            dataServiceContext.SetLink(line, nameof(line.LedgerJournalHeader), item);         
        }

   
        await dataServiceContext.SaveChangesAsync(SaveChangesOptions.BatchWithSingleChangeset | SaveChangesOptions.PostOnlySetProperties);   

Assemblies affected

As per above,

Expected result

Wouldn't expect an exception here.

Actual result

Exception thrown when the generated property change handlers are iterated due to some concurrency issue.

Screenshots

As per above.

Additional detail

Add any other context about the problem here. *Optional, details of the root cause if known.

dazinator avatar Jan 13 '23 17:01 dazinator