Joydip Kanjilal
Contributor

How to consume a WCF SOAP service in ASP.Net Core

how-to
Dec 10, 20185 mins
APIsC#Microsoft .NET

Learn how to use the Microsoft Connected Services extension to consume a WCF SOAP service from ASP.Net Core

network of connections / digital matrix
Credit: Gremlin / Getty Images

Until REST APIs came along, SOAP (Simple Object Access Protocol) was the de facto standard protocol on which web services were based. When working in ASP.Net Core, you might well encounter the need to consume data from third-party or external services that use SOAP as the protocol and XML as the message exchange format.

In this article we will explore how we can consume a WCF (Windows Communication Foundation) SOAP service in ASP.Net Core.

Create a WCF SOAP service in Visual Studio 2017

WCF is a secure, reliable, and scalable messaging platform that provides a unified programming model for developing service-oriented applications in .Net. You can take advantage of WCF to build a SOAP service, using XML as the message exchange format, or a REST service, using JSON as the message exchange format.

In this section we will create a WCF SOAP service. This service will be consumed by an ASP.Net Core application that we will create in the next section. Follow the steps below to create a WCF SOAP service in Visual Studio 2017.

  1. Launch the Visual Studio 2017 IDE.
  2. Click File > New > Project.
  3. Select WCF from the list of project templates displayed in the left pane.
  4. Select “WCF Service Application” from the right pane.
  5. Specify a name for your WCF service project.
  6. Click OK.
create a wcf soap service IDG

Figure 1: Creating a WCF SOAP Service in Visual Studio 2017.

A WCF service is comprised of the following parts: 

  • Service class
  • Service contract
  • One or more operation contracts
  • One or more endpoints
  • Hosting environment

When working with WCF, you first need to create a service contract and then define the service operations or operation contracts in it. A service contract is used to specify the operations that are available for the service client to consume. (You can learn more about WCF services from this article.)

Here is a service contract that contains one service method.

[ServiceContract]
    public interface IAuthorService
    {
        [OperationContract]
        List<string> GetAuthorNames();
    }

Note the use of the OperationContract attribute on the GetAuthorNames method. The service class, AuthorService, implements the IAuthorService interface. Here is the code that comprises the AuthorService class.

public class AuthorService : IAuthorService
    {
        public List<string> GetAuthorNames()
        {
            List<string> lstAuthors = new List<string>();
            lstAuthors.Add("Joydip Kanjilal");
            lstAuthors.Add("Steve Smith");
            lstAuthors.Add("Michael Stevens");
            return lstAuthors;
        }
    }

And that’s all you need to do to build a simple SOAP service in WCF. You can now open a web browser and test the service.

Create an ASP.Net Core Web API project in Visual Studio 2017

In this section we will create our ASP.Net Core Web API application. This will be a REST-based web API. Follow the steps outlined below to create a new ASP.Net Core project in Visual Studio 2017.

  1. In Visual Studio 2017, click File > New > Project.
  2. Select the “ASP.Net Core Web Application” project template.
  3. Specify the name and location for your project.
  4. Click OK.
  5. In the “New ASP.Net Core Web Application” dialog window, select .Net Core.
  6. Select ASP.Net Core 2.1 as the project’s version.
  7. Select “Web API” as the project template.
  8. Uncheck the “Enable Docker Support” and “Configure for HTTPS” check boxes as we will not need them here.
  9. Ensure that the message “No Authentication” is displayed; we won’t be needing authentication here either.
  10. Click OK.
create a rest service IDG

Figure 2: Creating an ASP.Net Core Web API application in Visual Studio 2017.

This will create a new ASP.NET Core Web API application. So far so good. In the next section we will examine how we can consume the SOAP service.

Consume a SOAP service in ASP.Net Core

Unlike legacy ASP.Net, ASP.Net Core doesn’t have the Add Service Reference option that allows us to add external service references as connected services. To consume the SOAP service we created earlier, we can take advantage of the Microsoft WCF Web Service Reference Provider. Follow the steps below to add a connected service and use this provider to reference the SOAP service.

  1. In the Solution Explorer window, select the ASP.Net Core Web API project.
  2. Right-click and select Add > Connected Service.
  3. Click on the “Microsoft WCF Web Service Reference Provider” and wait for the “Configure WCF Web Service Reference” wizard to open.
  4. In the “Configure WCF Web Service Reference” window, specify the URI of the SOAP service in the as shown in Figure 3 below.
  5. Click Next.
  6. In the next window, you can specify the data type options. We’ll skip that step here.
  7. Click Next.
  8. In the next window, you can specify the access level. Let it be public (the default).
  9. Click Finish.
configure wcf web service reference IDG

Figure 3: Configuring the Connected Service in ASP.Net Core.

In a series of automated steps, Visual Studio will download the metadata from the SOAP service we specified earlier, generate the service reference code, and store the code in a file named Reference.cs inside the Connected Services node. The necessary packages will also be installed, and the project file updated accordingly.

connected service added IDG

Figure 4: The WCF SOAP service reference has been added to the ASP.Net Core Web API project.

And that’s it! You can now instantiate the service client and consume the SOAP service in your controller method using the following code.

[HttpGet]
        public async Task<string[]> Get()
        {
            ServiceReference1.AuthorServiceClient authorServiceClient = new ServiceReference1.AuthorServiceClient();
            var data = await authorServiceClient.GetAuthorNamesAsync();
            return data;
        }

WCF Connected Service is an extension in Visual Studio that allows you to generate SOAP service references in your ASP.Net Core application for services that are built using WCF. You can also leverage connected services in ASP.Net Core to connect to Azure Storage services. We will explore this option in a later post here.

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author