0

So I wrote a plugin on preLeadUpdate that calls dynamics webapi. When running unit tests against f.e the dev-environment from Visual Studio it works fine but when deploying the plugin to the dev-dynamics instance I get the following error when it executes:

System.MethodAccessException: Attempt by method 'System.Net.Http.HttpClientHandler.Dispose(Boolean)' to access method 'System.Net.ServicePointManager.CloseConnectionGroups(System.String)' failed

In the stack trace I see that it fails when executing the line "var response = authClient.PostAsync(url, ....)" in the following method:

 using (HttpClient authClient = new HttpClient())
    {
        AzureTokenResponse token;
        string url = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";

        var response = authClient.PostAsync(url,
            new FormUrlEncodedContent(new[]
            {
                            new KeyValuePair<string, string>("resource", resource),
                            new KeyValuePair<string, string>("client_id", clientId),
                            new KeyValuePair<string, string>("client_secret", secret),
                            new KeyValuePair<string, string>("grant_type", "client_credentials")
            })).Result;

        if (response.IsSuccessStatusCode)
        {
            var result = response.Content.ReadAsStringAsync();
            token = JsonConvert.DeserializeObject<AzureTokenResponse>(result.Result);
        }
        else
        {
            throw new Exception("Could not get token for Azure");
        }

        return token;
    }

After some googling on the error I almost feel as if there is some referenced dll that is not part of the pluginassembly but instead maybe referenced from the GAC on the dev-webserver. And maybe that dll is of an older version on the server where some method is not accessible (maybe not declared as "public")

I've made sure that all the references to System.Net.Http are "copy local = true" but that doesn't seem to help.

So I'm getting a bit desperate and open for suggestions.

Toby Fieldgroove
  • 243
  • 4
  • 16
  • Just so that I understand the problem, you have written a dynamics plugin to fetch data from within the same dynamics instance? why can you not just do a query from within the plugin instead? also could you please give more details like your framework, the dynamics' api version and the exact error. – imran chowdhury Dec 10 '20 at 14:24
  • The code fetches marketing data which according to my research is not available as an "entity" but however it is available via a WebApi call. The weird thing is that I cannot even get a token but when doing the same thing in another instance of Dynamics, same version (online, v.9.1) there are no problems.. So it's almost like there is something with this environemnt.. – Toby Fieldgroove Dec 14 '20 at 10:49
  • apologies for the late reply. please could you possibly share the entity you want to access. this might not be in line with the question you are asking but if you came up with an entity that is not exposed through sdk, chances are what you are trying to achieve is unsupported and hence cannot be done even if you have a resolution for the above problem. – imran chowdhury Dec 16 '20 at 18:42

0 Answers0