web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

System.MethodAccessException when executing plugin (D365 online)

(0) ShareShare
ReportReport
Posted on by 40

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.

I have the same question (0)
  • Suggested answer
    Fedorov Profile Picture
    40 on at

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 96 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans