title: Can I run WCF on top of WebAPI?
body: <p>For a work project, I'm building an application consisting of a frontend SPA (Aurelia), a WebAPI backend for that SPA, and a multitude of web service WebAPI projects contained in existing applications. (This application will perform data aggregation - it's a dashboard for our clients to show relevant information from many sources.)</p>
  
  <p>Immediately, I was faced with some challenges. Using WebAPI, we wanted to expose the web services as REST endpoints. This works well for client applications and is very open. However, making server-to-server calls in .NET, I wanted to abstract away the REST calls and simply provide a method-based interface (so I could call, say, <code>new MyWebServiceClient().getOrders()</code> or something like that); I also did not want to have to duplicate data model classes across solutions, or worry about deserializing one JSON model type to another type. (Blegh.)</p>
  
  <p>To achieve this goal, I've created an internal nuget package that a) provides access to the data model classes used in the service via the assembly, and b) provides an interface for HTTP calls, abstracting away the JSON serialization and deserialization, like so:</p>
  
  <pre><code>public async Task&lt;T&gt; Get&lt;T&gt;(string endpoint, IEnumerable&lt;KeyValuePair&lt;string, string&gt;&gt; parameters = null, CancellationToken cancellationToken = default(CancellationToken))
  {
      var builder = new UriBuilder(Properties.Settings.Default.MyEndpointHost + endpoint);
      builder.Query = buildQueryStringFromParameters(parameters);
  
      _httpClient.DefaultRequestHeaders.Accept.Clear();
      _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
      try
      {
          // After this, we really shouldn't continue.
          var request = await _httpClient.GetAsync(builder.Uri, cancellationToken);
  
          if (!request.IsSuccessStatusCode)
          {
              if (request.StatusCode &gt;= HttpStatusCode.BadRequest &amp;&amp; request.StatusCode &lt; HttpStatusCode.InternalServerError)
              {
                  throw new EndpointClientException("Service responded with an error message.", request.StatusCode, request.ReasonPhrase);
              }
  
              if (request.StatusCode &gt;= HttpStatusCode.InternalServerError &amp;&amp; (int)request.StatusCode &lt; 600)
              {
                  throw new EndpointServerException("An error occurred in the Service endpoint.", request.StatusCode, request.ReasonPhrase);
              }
          }
  
          var json = await request.Content.ReadAsStringAsync();
          return JsonConvert.DeserializeObject&lt;T&gt;(json);
      }
      catch (Exception ex)
      {
          throw;
      }
  }
  </code></pre>
  
  <p>The public methods are simply convenience methods, calling that function with the requisite arguments, like this:</p>
  
  <pre><code>    public async Task&lt;IEnumerable&lt;MyModel&gt;&gt; SearchMyModelsByFooName(string fooName, CancellationToken cancellationToken)
      {
          var parameters = new List&lt;KeyValuePair&lt;string, string&gt;&gt;();
          parameters.Add(new KeyValuePair&lt;string, string&gt;("searchText", fooName));
  
          return await this.Get&lt;List&lt;MyModel&gt;&gt;("myModel", parameters, cancellationToken);
      }
  </code></pre>
  
  <p>I've had good results with this, although I have to maintain it manually and update dependencies.</p>
  
  <p>However, upon talking to my colleagues, I was introduced to WCF, and it looks as though it solves a lot of the issues I'm trying to solve manually. Looking into this, though, reveals that the setup can be tricky, and we're not sure if it's worth the trouble. (Additionally, we'd have to maintain two APIs.)</p>
  
  <p>Although it's fun, I don't want to reinvent the wheel. Is there a way to bolt WCF on top of WebAPI for server-to-server calls only, or have WCF generate data for WebAPI controllers?</p>