title: Call Dynamics Web API in C# Plugin
body: <p>I have a Business Process Flow in Microsoft Dynamics to handle creation of a new client. When the process finishes, I am attaching a workflow that kicks of an action which calls a plugin to do some custom processing. I am following <a href="https://sigaostudios.com/2017/05/business-process-flow-trigger-plugin/" rel="nofollow noreferrer">this</a> article to set up this process.</p>
  <p>In my plugin, I have a call to the Dynamics Web API (see code below). When I hit the <code>responseMessage = client.GetAsync(url).Result;</code> line, my plugin exits without returning an error.  When I am debugging in the Plugin Registration Tool, the Plugin Registration Tool crashes and has to be restarted. When I look at the network traffic, this call appears to be failing with a <code>401 - Unauthorized</code> error.</p>
  <p>When I try this same <code>HttpClient</code> call from a console app, the call succeeds. I've tried a few different credentials for authentication without success. I've also tried calling the <code>GetAsync</code> function a few different ways. Is this error related to the async or authentication methods? What am I missing here?</p>
  <pre><code>HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(&quot;admin&quot;, &quot;password&quot;, &quot;DOMAIN&quot;) });
  client.BaseAddress = new Uri(Helpers.GetSystemUrl(COHEN.APIConnector.Application.Dynamics));
  client.DefaultRequestHeaders.Clear();
  client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
  client.DefaultRequestHeaders.Add(&quot;OData-MaxVersion&quot;, &quot;4.0&quot;);
  client.DefaultRequestHeaders.Add(&quot;OData-Version&quot;, &quot;4.0&quot;);
  HttpResponseMessage responseMessage;
  string url = &quot;ccseq_clients&quot;;
  
  responseMessage = client.GetAsync(url).Result;
  </code></pre>
  <hr />
  <h3>Update</h3>
  <p>We are using Dynamics 2016 On-Premise.</p>
  <p>The reason we are using the Web API is that this call occurs in a library we have developed that will interface between multiple systems. The library is used within a plugin, but can also be used outside of a plugin for other applications. The code sample we provided is the chunk of code that is failing in the library. We pulled the chunk of code directly into the plugin to see if the error was related more to the library or the plugin.</p>
  <p>We noticed that the <code>client.GetAsync(url).Result</code> call was actually returning a correct value.  We missed seeing in our network traffic that two calls failed before a third succeeded and returned the correct value (See screenshot below).  The odd thing is that when we debug this line of code, stepping over the line of code causing the Plugin Registration Tool to crash while the network traffic shows we returned the correct values. If we set a breakpoint after this line of code, then the Plugin Registration Tool does not crash and we have a correct value in our response.  This behavior changes when we went back to debugging in the library rather than directly in the plugin.  In the library, the Plugin Registration Tool always crashes when hitting this line regardless of where we set our breakpoints even though the network traffic still shows a successful response.</p>
  <p><a href="https://i.stack.imgur.com/mEsJD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mEsJD.png" alt="Network Traffic" /></a></p>
  <hr />
  <h3>Update 2</h3>
  <p>It would appear that my library is successfully making multiple different calls to the Web API so I'm thinking the issue is more that the Plugin Registration Tool can't handle the Web API call more than the call not actually working.</p>
  <p><a href="https://i.stack.imgur.com/2SkKE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2SkKE.png" alt="Network Traffic" /></a></p>