Homogenous AppDomain Error within Microsoft Dynamics N.A.V.
What an earth I hear you ask.. well that’s what I thought too.
Background
So I have doing some development, basically I have an incoming Json feed from a Shopify Web-hook, my initial thought was to create a C# Object which I could reference as a DotNet variable in Dynamics N.A.V. the using Newtonsoft deserialize into this object.. easy.. and to be honest it was…
Until that is I got sent a request from Shopify that had an unexpected reference. Originally when I built the object class I used the samples provided by Shopify, turns out though that the sample doesn’t contain everything, so the first time a shipment notification came through that contained an order with a refund…. bam! It broke.
My original object didn’t have a Refunds section, so when I tried to deserialize it, well it didn’t know what to do.
So… I thought I would simply use a dynamic object, then just map info I actually needed, ignoring what I didn’t, then pass this back into Dynamics N.A.V.
I added an overloaded constructor to my c# class which now looked like;
///
<summary>
/// Ctor +1
/// </summary>
/// <param name="jsonText"></param>
public FulfillmentNotice (string jsonText)
{
dynamic t = JsonConvert.DeserializeObject<dynamic>(jsonText);
id = t.id;
order_number = t.order_number;
billing_address = t.billing_address.ToObject<Address>();
shipping_address = t.shipping_address.ToObject<Address>();
payment_gateway_names = new List<string>();
foreach (string s in t.payment_gateway_names)
payment_gateway_names.Add(s);
// init list
shipping_lines = new List<Shipping_Lines>();
foreach (dynamic shipline in t.shipping_lines)
shipping_lines.Add(shipline.ToObject<Shipping_Lines>());
// init list
fulfillments = new List<Fulfillment>();
foreach (dynamic fulLine in t.fulfillments)
fulfillments.Add(fulLine.ToObject<Fulfillment>());
// init list
discount_codes = new List<Discount_Codes>();
foreach (dynamic disLine in t.discount_codes)
discount_codes.Add(disLine.ToObject<Discount_Codes>());
}
So now I call the new constructor passing in the Json text, worked nicely, now it completely ignores anything unexpected, great!
Next I altered my codeunit within Dynamics N.A.V. to use the new code, complied then ran the function, unfortunately I got..
A call to ShopifyFulfillmentReceiver.Library.FulfillmentNotice failed with this message: Dynamic operations can only be performed in homogenous AppDomain.
Well I wasn’t really expecting that, though I thought I had seen it before.
The Solution
So what do you need to do? You need to remove/change the following file in the Microsoft.Dynamics.Nav.Server.exe.config file. Find the section <runtime> and you should see a line <NetFx40_LegacySecurityPolicy enabled=”false”/> and change it to false, restart the NST and it should work – if the section exist, put the below withing the <configuration> section
<runtime>
<NetFx40_LegacySecurityPolicy enabled="false"/>
</runtime>
As always, you should check it on a dev instance before rolling out to a live instance, making sure it doesn’t affect anything!