title: Call Web API 2 method using c# CLR stored procedure by HTTPClient
body: <p>I have a WPF application that consumes services of MVC web API 2. Created RestClient wrapper using HTTPClient to call async methods such as PostAsync and GetAsync. For ex my POST method wrapper would be like:</p>
  
  <pre><code>using (var client = new HttpClient(new HttpClientHandler()
                                         { AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip }))
  {
      var content = new FormUrlEncodedContent(postObject);
  
      SetupClient(client, methodName, apiUrl, postObject, headerContent);
  
      if (apiKey != null &amp;&amp; appId != null)
          await SetAuthorizationHeader(client, methodName, apiUrl, appId, apiKey, content).ConfigureAwait(false);
  
      using (HttpResponseMessage response = Task.Run(() =&gt; client.PostAsync(apiUrl, content)).Result)
      {
          response.EnsureSuccessStatusCode();
  
          using (HttpContent httpContent = response.Content)
          {
              if (response.IsSuccessStatusCode)
              {
                  result = response.Content.ReadAsAsync&lt;T&gt;().Result;
              }
          }
      }
  }
  </code></pre>
  
  <p>Which is working fine. Right now I am trying to call some of the API calls through C# CLR stored procedure by creating SQL Server Database Project.</p>
  
  <p>C# CLR stored procedure will be like :</p>
  
  <pre><code>[Microsoft.SqlServer.Server.SqlProcedure]
  public static void SQLRestClient(SqlString weburl, SqlString postBody, SqlString appIdString, SqlString apiKeyString, SecureString baseAddress, out SqlString returnval)
  {
      string apiUrl = Convert.ToString(weburl);
      string baseAddressString = Convert.ToString(baseAddress);
  
      string result = string.Empty;
      var appId = ConvertToSecureString(Convert.ToString(appIdString));
      var apiKey = ConvertToSecureString(Convert.ToString(apiKeyString));
  
      try
      {
          string methodName = HttpMethod.Post.Method.ToUpper();
  
          using (var client = new HttpClient(new HttpClientHandler()
          {
              AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
          }))
          {
              var content = new FormUrlEncodedContent(postObject);
  
              SetupClient(client, methodName, apiUrl, postObject, headerContent);
  
              if (apiKey != null &amp;&amp; appId != null)
                  await SetAuthorizationHeader(client, methodName, apiUrl, appId, apiKey, content).ConfigureAwait(false);
  
              using (HttpResponseMessage response = Task.Run(() =&gt; client.PostAsync(apiUrl, content)).Result)
              {
                  response.EnsureSuccessStatusCode();
  
                  using (HttpContent httpContent = response.Content)
                  {
                      if (response.IsSuccessStatusCode)
                      {
                          result = response.Content.ReadAsStringAsync();
                      }
                  }
              }
          }
      }
      catch (Exception ex)
      {
          SqlContext.Pipe.Send(ex.Message.ToString());
      }
  
      returnval = result;
  }
  </code></pre>
  
  <p>When I try to generate the DLL of this procedure I am getting build error.
  Its because the compiler is not recognizing Assembly references such as </p>
  
  <blockquote>
    <p>System.Net.Http.dll</p>
  </blockquote>
  
  <p>As I gone through this thread</p>
  
  <p><a href="https://stackoverflow.com/a/28436018/1721911">Sending HTTP POST request from SQL Server 2012 or SQL CLR C#</a></p>
  
  <p>I found a solution to use HttpWebRequest instead of HttpClient. Since I have been using HttpClient throughout my application I don't want to switch to HttpWebRequest.</p>
  
  <p>Can anyone suggest any other way so that I can generate the CLR stored procedure dll by using HttpClient. Any help will be appreciated and thanks in advance.</p>