Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

System.MethodAccessException when executing plugin (D365 online)

Posted on by 338

So I wrote a plugin on preLeadUpdate that calls dynamics webapi. When running unit tests agains 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("resource", resource),
                                    new KeyValuePair("client_id", clientId),
                                    new KeyValuePair("client_secret", secret),
                                    new KeyValuePair("grant_type", "client_credentials")
                    })).Result;

                if (response.IsSuccessStatusCode)
                {
                    var result = response.Content.ReadAsStringAsync();
                    token = JsonConvert.DeserializeObject(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.

  • Suggested answer
    Fedorov Profile Picture
    Fedorov 338 on at
    RE: System.MethodAccessException when executing plugin (D365 online)

    I tried making it asynchronous and then it reports "success" but the field is not updated.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans