locked
CRM Online, Access security critical method 'System.Web.Script.Serialization.JavaScriptSerializer..ctor()' failed. RRS feed

  • Question

  • Hi,

    In my custom workflowactivity, I try to desterilize json data. I registered the workflowactivity in Sandbox mode, because it was not allowed to be registered in None mode.  Error occured at this line of code:

    respone = new JavaScriptSerializer().Deserialize<myObject>(jsonData);

    The error message:

    Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): XXXXXXXX.CRM2015.WorkflowActivities.XXXXXXXX: System.MethodAccessException: Attempt by security transparent method 'XXXXXXXX.CRM2015.WorkflowActivities.XXXXXXXX.SetLocationInfo(Microsoft.Xrm.Sdk.IOrganizationService, Microsoft.Xrm.Sdk.ITracingService, System.String)' to access security critical method 'System.Web.Script.Serialization.JavaScriptSerializer..ctor()' failed.

    Assembly 'XXXXXXXX.CRM2015.WorkflowActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3f9fc15734725b08' is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself.  In order to access security critical code, this assembly must be fully trusted.

    Assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is a conditionally APTCA assembly which is not enabled in the current AppDomain.  To enable this assembly to be used by partial trust or security transparent code, please add assembly name 'System.Web.Extensions, PublicKey=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9' to the the PartialTrustVisibleAssemblies list when creating the AppDomain.
       at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)
       at Microsoft.Crm.Workflow.Services.ProxyCustomActivity.Execute(CodeActivityContext executionContext)

    Does anyone have the same situation and have found a solution? Thanks.

    Thursday, January 14, 2016 10:02 PM

Answers

  • you get null inside your myObject because the properties of the class don't have the same name as your json object.

    In this case ipAddress and IPAddress are different, also your class doesn't contain properties like statusCode and statusMessage, for you it's normal that ipAddress should be mapped to IPAddress, for the .NET framework this is not obvious.

    You have 3 options:

    1) add the missing properties and rename the properties of your myObject class to be consistent with the json object you receive
    OR

    2) create a brand new class that is consistent to your json object and create a method to convert an instance of this new class to your myObject class
    OR

    3) add the missing attributes to your myObject class and add the decorators in order to make the serializer understand the mapping, for example:

    [DataContract]
    public class myObject
    {
        [DataMember(Name = "ipAddress", IsRequired = true)]
        public string IPAddress { get; set; }
    	// ...
    	// rest of the properties
    	// ...
    }

    I tested all the 3 options I mentioned above and they work.

    hope it helps


    My blog: www.crmanswers.net - CRM Theme Generator

    • Proposed as answer by Guido PreiteMVP Saturday, January 16, 2016 11:45 AM
    • Marked as answer by chicago bq Monday, January 18, 2016 4:26 PM
    Saturday, January 16, 2016 11:45 AM

All replies

  • You need to use DataContractJsonSerializer, you will find all the details here:

    JSON and CRM Sandbox Plugins


    My blog: www.crmanswers.net - CRM Theme Generator

    Friday, January 15, 2016 3:11 AM
  • Here is my code.  I am using DataContractJsonSerializer.  However, it does not seem working.  The only extra step is I first get jsonData as string.

    Then, convert string to MemoryStream.  The Trace returns the json data well.  But response return null.

    using (WebClient client = new WebClient())
    {
              string jsonData = client.DownloadString(url);
              _tracingService.Trace("jsonData ///////////////// " + jsonData);
              MemoryStream myStream = GenerateStreamFromString(jsonData);
              DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(myObject));
              respone = (myObject)serializer.ReadObject(myStream);               
    }

    Friday, January 15, 2016 4:33 AM
  • you don't post the json data, you don't post the myObject class, how can we help you?

    If I test with my json data and my class (as the link I provided you before) it works


    My blog: www.crmanswers.net - CRM Theme Generator

    Friday, January 15, 2016 8:46 AM
  • Hi,

    I think this is because you want to access the memory stream object in sandbox mode.

    Instead of using JavaScriptSerializer , you can use Newtonsoft.Json library's method JsonConvert.SerializeObject(yourobject) 


    If you find this post helpful then please Vote as Helpful and Mark As Answer. Thanks and Regards, Polat Aydın My blog

    Friday, January 15, 2016 10:09 AM
  • Hi,

    I think this is because you want to access the memory stream object in sandbox mode.

    Instead of using JavaScriptSerializer , you can use Newtonsoft.Json library's method JsonConvert.SerializeObject(yourobject) 


    If you find this post helpful then please Vote as Helpful and Mark As Answer. Thanks and Regards, Polat Aydın My blog

    It's not for that, and if he wants to use Newtonsoft.Json he will need to merge the dll

    until he doesn't post the json data and the myObject class there is no way to simulate his code


    My blog: www.crmanswers.net - CRM Theme Generator

    Friday, January 15, 2016 1:26 PM
  • Thanks for your response and discussion.

    Json data look like this:

    {
     "statusCode" : "OK",
     "statusMessage" : "",
     "ipAddress" : "71.201.120.87",
     "countryCode" : "US",
     "countryName" : "United States",
     "regionName" : "New York",
     "cityName" : "New York",
     "zipCode" : "11356",
     "latitude" : "42.1081",
     "longitude" : "-87.7359",
     "timeZone" : "-06:00"
    }

    myObject is defined as a class:

    public class myObject
        {
            public string IPAddress { get; set; }
            public string CountryName { get; set; }
            public string CountryCode { get; set; }
            public string CityName { get; set; }
            public string RegionName { get; set; }
            public string ZipCode { get; set; }
            public string Latitude { get; set; }
            public string Longitude { get; set; }
            public string TimeZone { get; set; }
        }

    Friday, January 15, 2016 7:29 PM
  • the problem is respone.CityName returns empty string.  All attributes of myObject return empty string.
    Friday, January 15, 2016 8:48 PM
  • you get null inside your myObject because the properties of the class don't have the same name as your json object.

    In this case ipAddress and IPAddress are different, also your class doesn't contain properties like statusCode and statusMessage, for you it's normal that ipAddress should be mapped to IPAddress, for the .NET framework this is not obvious.

    You have 3 options:

    1) add the missing properties and rename the properties of your myObject class to be consistent with the json object you receive
    OR

    2) create a brand new class that is consistent to your json object and create a method to convert an instance of this new class to your myObject class
    OR

    3) add the missing attributes to your myObject class and add the decorators in order to make the serializer understand the mapping, for example:

    [DataContract]
    public class myObject
    {
        [DataMember(Name = "ipAddress", IsRequired = true)]
        public string IPAddress { get; set; }
    	// ...
    	// rest of the properties
    	// ...
    }

    I tested all the 3 options I mentioned above and they work.

    hope it helps


    My blog: www.crmanswers.net - CRM Theme Generator

    • Proposed as answer by Guido PreiteMVP Saturday, January 16, 2016 11:45 AM
    • Marked as answer by chicago bq Monday, January 18, 2016 4:26 PM
    Saturday, January 16, 2016 11:45 AM
  • Hi, Guido-

    Excellent!  I updated the name of attributes in myObject class to be consistent with Json Data, and it worked.  Thank you so much. 

    Monday, January 18, 2016 4:29 PM