How To Migrate From WCF Service To ASP .NET Core Web API (REST)?

How To Migrate From WCF Service To ASP .NET Core Web API (REST)?

For more than ten years, Windows Communication Foundation (WCF) has been a well-liked framework for creating service-oriented applications. To increase performance, scalability, and maintainability nevertheless, it is becoming more and more crucial to switch from WCF to newer technologies like ASP .NET Core Web API (REST). This blog post offers a detailed tutorial for switching from WCF to the ASP .NET Core Web API.

Why Switch to ASP.NET Core Web API From WCF?

You might wish to switch from WCF to the ASP .NET Core Web API for a number of reasons. The following are some of the main advantages of switching to Web API:

  1. Improved Performance: Because ASP .NET Core Web API is quick and light, you can create high-performance RESTful services that can cope with heavy traffic.
  2. Better Scalability: Building scalable services that can operate on several platforms and devices is simpler because to Web API’s support for different platforms and protocols.
  3. Easier Maintenance: Due to its simplified programming paradigm and support for more contemporary development techniques like dependency injection and middleware, Web API is simpler to maintain than WCF.
  4. Better Integration with Modern Web Technologies: Modern web technologies like Angular, React, and Vue connect better with web API, making it simpler to create cutting-edge, responsive, and dynamic user experiences.

Step-by-Step Guide to Migrate from WCF to ASP .NET Core Web API

Here are the steps you can follow to migrate from WCF to ASP .NET Core Web API:

Step 1: Identify the Services to be Migrated

Finding the WCF services that need to be migrated is the first step. Services that are essential to your application and those that need regular upgrades or maintenance should be given top priority.

Step 2: Create an ASP .NET Core Web API Project

The following action is to start a fresh Visual Studio ASP .NET Core Web API project. To achieve this, use the “API” project type after choosing the “ASP .NET Core Web Application” template.

Step 3: Add Required NuGet Packages

Once you have created the Web API project, you need to add the required NuGet packages for building RESTful services. Some of the key packages you will need include:

  • Microsoft.AspNetCore.Mvc
  • Microsoft.AspNetCore.Http.Abstractions
  • Microsoft.AspNetCore.Authentication.JwtBearer

You can add these packages by right-clicking on the project in the Solution Explorer and selecting “Manage NuGet Packages.”

Step 4: Define Models and Controllers

The models and controllers for your Web API service must be defined next. You may accomplish this by declaring the corresponding controllers after establishing new classes for your models.

You can provide the HTTP methods (GET, POST, PUT, DELETE, etc.) and the associated actions that will be taken when these methods are invoked in the controllers.

Step 5: Implement Dependency Injection

A crucial component of ASP .NET Core is dependency injection, which is used to handle dependencies across the various parts of your application. By developing a Startup class and then specifying the necessary services in the ConfigureServices function, dependency injection may be implemented.

Step 6: Configure Authentication and Authorization

Any RESTful service must have authentication and permission, and ASP .NET Core offers numerous ways to implement these functionalities. JSON Web Tokens (JWT) are a popular method for authentication and authorization.

Adding the necessary middleware to your Startup class, then defining the authentication and authorization options, are how you establish authentication and authorization.

Step 7: Test and Debug the Web API Service

Once the implementation is complete, you may test and debug the Web API service with tools like Postman or Swagger. Visual Studio may also be used to troubleshoot the service and inspect the logs.

Step 8: Deploy

The final step is to put the Web API service into production. The service may be deployed on a variety of platforms, including Azure, AWS, and a self-hosted server.

When launching the service, make sure that adequate security measures are in place to secure your data and prevent unwanted access.

Code Examples

Here are some code examples that illustrate how to migrate from WCF to ASP .NET Core Web API.

Defining a Model in Web API

In Web API, you can define models using plain C# classes. For example, to define a “Customer” model, you can create a new class like this:

public class Customer
   public int Id { get; set; }
   public string Name { get; set; }
   public string Email { get; set; }
   public string Phone { get; set; }

Defining a Controller in Web API

In Web API, you can define controllers using the “ApiController” attribute. For example, to define a “Customers” controller, you can create a new class like this:

public class CustomersController : ControllerBase
   private readonly ICustomerService _customerService;

   public CustomersController(ICustomerService customerService)
      _customerService = customerService;

   public async Task<ActionResult<IEnumerable<Customer>>> Get()
      var customers = await _customerService.GetCustomersAsync();
      return Ok(customers);

   public async Task<ActionResult<Customer>> Get(int id)
      var customer = await _customerService.GetCustomerAsync(id);

      if (customer == null)
         return NotFound();

      return Ok(customer);

   public async Task<ActionResult<Customer>> Post(Customer customer)
      await _customerService.AddCustomerAsync(customer);
      return CreatedAtAction(nameof(Get), new { id = customer.Id }, customer);

   public async Task<IActionResult> Put(int id, Customer customer)
      if (id != customer.Id)
         return BadRequest();

      await _customerService.UpdateCustomerAsync(customer);
      return NoContent();

   public async Task<IActionResult> Delete(int id)
      await _customerService.DeleteCustomerAsync(id);
      return NoContent();

Implementing Dependency Injection in Web API

In Web API, you can implement dependency injection using the “IServiceCollection” interface. For example, to configure the “ICustomerService” interface, you can add the following code to the “ConfigureServices” method in your Startup class:

public void ConfigureServices(IServiceCollection services)
   services.AddScoped<ICustomerService, CustomerService>();

Configuring Authentication and Authorization in Web API

In Web API, you can configure authentication and authorization using the “AddAuthentication” and “AddAuthorization” methods in your Startup class. For example, to configure JWT authentication and authorization, you can add the following code to the “ConfigureServices” method:

public void ConfigureServices(IServiceCollection services)
   // ...

      .AddJwtBearer(options =>
         options.RequireHttpsMetadata = false;
         options.SaveToken = true;
         options.TokenValidationParameters = new TokenValidationParameters
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["Jwt:Key"])),
            ValidateIssuer = false,
            ValidateAudience = false

   services.AddAuthorization(options =>
      options.AddPolicy("AdminOnly", policy => policy.RequireRole("admin"));

In the above code, we have defined a policy named “AdminOnly” that requires the user to have the “admin” role to access the resource.

Calling a Web API Service from a Client

To call a Web API service from a client, you can use any HTTP client library that supports sending requests and receiving responses.

Here’s an example of calling the “Get” method of the “Customers” controller using the HttpClient class:

var httpClient = new HttpClient();
var response = await httpClient.GetAsync("https://localhost:5001/api/customers");
var customers = await response.Content.ReadAsAsync<List<Customer>>();

In the above code, we have created a new instance of the HttpClient class and used it to send a GET request to the “https://localhost:5001/api/customers” endpoint. We then read the response content as a list of Customer objects.


We explained how to convert from a WCF service to an ASP.NET Core Web API service in this blog article. We examined the differences between the two technologies and offered code samples to assist you in getting started.

While migration might be difficult, it is critical to maintain your technology stack up to date in order to take advantage of new features, security upgrades, and performance enhancements.

You may assure a seamless and successful migration from WCF to ASP.NET Core Web API by following the procedures provided in this blog article.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *