user3845056
user3845056

Reputation: 497

JSON.NET causes System.TypeAccessException in Dynamics CRM

I get the below error when running a custom workflow action that uses JSON.NET to serialize a dynamics object to JSON.

Is there a restriction on using reflection in CRM Dynamics customer workflow activities / plugins?

Is it because I am using dynamic variables?

System.TypeAccessException: Attempt by method 'DynamicClass.CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Object, System.String)' to access type 'Newtonsoft.Json.Linq.JObject+JObjectDynamicProxy' failed. at CallSite.Target(Closure , CallSite , Object , String ) at WSWA.CRM.Logic.MyobIntegrationLogic.CreateInvoice(Boolean retry) at WSWA.CRM.Workflows.MyobJob.MyobIntegrationTester.Execute(CodeActivityContext context)

        dynamic account = new JObject();
        account.UID = GetAccount("Undeposited Funds Account");

        dynamic job = new JObject();
        job.UID = GetJob("JFC Interiors");

        dynamic gstTaxCode = new JObject();
        gstTaxCode.UID = GetTaxUidByCode("GST");

        dynamic customer = new JObject();
        customer.UID = GetCustomerUid("Bar001.test");

        dynamic line1 = new JObject();
        line1.Total = 22.55;
        line1.Account = account;
        line1.Job = job;
        line1.TaxCode = gstTaxCode;

        dynamic line2 = new JObject();
        line1.Total = 23.55;
        line1.Account = account;
        line1.Job = job;
        line1.TaxCode = gstTaxCode;

        var lines = new JArray();
        lines.Add(line1);
        lines.Add(line2);

        dynamic invoice = new JObject();
        invoice.Date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        invoice.Customer = customer;
        invoice.CustomerPurchaseOrderNumber = "PO Number";
        invoice.Number = "INV-1000";
        invoice.Lines = lines;

        var content = new StringContent(contact.ToString());
        content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/json");

        var responseTask = httpClient.PostAsync(url, content);

        Task.WaitAll(responseTask);

Upvotes: 1

Views: 797

Answers (2)

Daryl
Daryl

Reputation: 18895

You can't use dynamic types in Dynamics (ironic isn't it?) CRM Sandboxed plugins. You can use reflection, as long as whatever you're reflecting is publicly available. i.e. You can use reflection to get a list of public properties, but you can't get a list of private fields.

You can always farm your work out to an Azure service and do whatever you'd like there.

Upvotes: 4

Jordi
Jordi

Reputation: 1470

There are known issues with dynamic types and using NuGet packages like Microsoft.AspNet.WebApi.Client.

Could you try with a WebClient?

More info here

Upvotes: 0

Related Questions