Search code examples
c#wpfexceptiontask-parallel-librarymethodaccessexception

MethodAccessException on Task.CompletedTask property


I am working on a small wpf application and one of the users is getting the following exception:

System.MethodAccessException: 
Attempt by method "xxx.HttpConfirmation.Invoke()" to access method "System.Threading.Tasks.Task.get_CompletedTask()" failed.
at xxx.HttpConfirmation.Invoke()
at xxx.RequestPipeline.<ProcessQueuedRequests>d__11.MoveNext()

According to MSDN documentation, such exception is thrown in the following situations:

  • A private, protected, or internal method that would not be accessible from normal compiled code is accessed from partially trusted code by using reflection.
  • A security-critical method is accessed from transparent code.
  • The access level of a method in a class library has changed, and one or more assemblies that reference the library have not been recompiled.

Task.get_CompletedTask() is public since its introduction and I am also not using reflection to access the property.

I also don't think that there is a problem with code security/transparency since only one user is having this issue.

The exception is thrown at the Task.CompletedTask line:

public class HttpConfirmation
{
    public static Task Invoke()
    {
        using (var client = new WebClient())
        {
            try
            {
                // Send the request and don't wait for the response.
                client.UploadStringTaskAsync("http://sampleUrl.com", string.Empty);
            }
            catch
            {
                // ignore
            }
        }

        return Task.CompletedTask;
    }
}

Any ideas on what may cause the exception?


Solution

  • The problem was that the customer had .NET 4.5.2 installed and the program targeted .NET 4.6. Though I still have no clue as to why exactly System.MethodAccessException was thrown as none of the 3 documented situations for throwing this exception did happen.