How to connect WCF Soap API Service to Asp.net core 3.0 Application

Asad Iftikhar
4 min readNov 29, 2020

--

In this article we will be learning how to connect WCF Asmx soap api to your Asp.net Core App So let’s get Started and write some code

First Create an WCF Service Project

Then Right Click on your Solution and select add new item

Add Asmx Service in your Project

Microsoft will provide you some hard written code in this article

Next Step is to Select your asmx service as start up page

Run IIS Express from Visual Studio you will have this Chrome Page

Our next step is to create Asp.net Core 3.0 app and connect Windows service to .net Core App

Create Asp.net Core Web Application Project

Create Service Folder in which we will be writing Proxy class for

Folder Structure will be look like some of this

Our Next step is to run the Soap Service and then add it in our application

Most Important Step is to run our Service once your service is running do the following

Once you have done that this Window will apear then select WCF service library

And then Add your Service url to connect to service

Click Next

Click Next

Click Finish

This will take some time to finish

Once it is done your folder directory will look something like of this

Write Proxy Service Code in it

using ServiceReference1;using System;using System.Collections.Generic;using System.Linq;using System.ServiceModel.Channels;using System.Threading.Tasks;using static ServiceReference1.APISoapClient;namespace WCFServiceAsp.netCore.Services{public class WCFSoapProxy{readonly string l_ClassName = "WCFSoapProxy";readonly APISoapClient   l_APISoapClient;public WCFSoapProxy(){l_APISoapClient = new APISoapClient(new EndpointConfiguration());l_APISoapClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost:50016/API.asmx");l_APISoapClient.Endpoint.Binding = GetBindingForEndpoint(new EndpointConfiguration(), l_APISoapClient.Endpoint.Address);}public static Binding GetBindingForEndpoint(EndpointConfiguration p_EndpointConfiguration, System.ServiceModel.EndpointAddress p_EndpointAddress){dynamic l_Bindings = null;if (p_EndpointAddress.Uri.Scheme.Equals("http")){if ((p_EndpointConfiguration == EndpointConfiguration.APISoap)){System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();result.MaxBufferSize = int.MaxValue;result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;result.MaxReceivedMessageSize = int.MaxValue;result.AllowCookies = true;l_Bindings = result;}if ((p_EndpointConfiguration == EndpointConfiguration.APISoap12)){CustomBinding result = new CustomBinding();TextMessageEncodingBindingElement textBindingElement = new TextMessageEncodingBindingElement();textBindingElement.MessageVersion = MessageVersion.CreateVersion(System.ServiceModel.EnvelopeVersion.Soap12, AddressingVersion.None);result.Elements.Add(textBindingElement);HttpTransportBindingElement httpBindingElement = new HttpTransportBindingElement();httpBindingElement.AllowCookies = true;httpBindingElement.MaxBufferSize = int.MaxValue;httpBindingElement.MaxReceivedMessageSize = int.MaxValue;result.Elements.Add(httpBindingElement);l_Bindings = result;}}else{if ((p_EndpointConfiguration == EndpointConfiguration.APISoap12)){System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();result.MaxBufferSize = int.MaxValue;result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;result.MaxReceivedMessageSize = int.MaxValue;result.AllowCookies = true;result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;l_Bindings = result;}if ((p_EndpointConfiguration == EndpointConfiguration.APISoap12)){CustomBinding result = new CustomBinding();TextMessageEncodingBindingElement textBindingElement = new TextMessageEncodingBindingElement();textBindingElement.MessageVersion = MessageVersion.CreateVersion(System.ServiceModel.EnvelopeVersion.Soap12, System.ServiceModel.Channels.AddressingVersion.None);result.Elements.Add(textBindingElement);HttpsTransportBindingElement httpsBindingElement = new HttpsTransportBindingElement();httpsBindingElement.AllowCookies = true;httpsBindingElement.MaxBufferSize = int.MaxValue;httpsBindingElement.MaxReceivedMessageSize = int.MaxValue;result.Elements.Add(httpsBindingElement);l_Bindings = result;}}return l_Bindings;}public string GetSoapString(){string objstring = string.Empty;try{var Response = l_APISoapClient.HelloWorldAsync().Result.ToString();objstring = Response;}catch (Exception ex){throw ex;}return objstring;}}}

Rebuild your Solution After Writing Proxy Class

Next Step we need to do is to Create an API Controller fetch the result from our Web Service .asmx and then print it on the browser

Create Controllers Folder and Create an API Controller screenshot attach bellow.

Import your service namespace in my case it was something like this

using WCFServiceAsp.netCore.Services;

and then Write Controller Code

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;using WCFServiceAsp.netCore.Services;namespace WCFServiceAsp.netCore.Controllers{[Route("api/[controller]")][ApiController]public class SOAPAPI : ControllerBase{WCFSoapProxy l_WCFSoapProxy = new WCFSoapProxy();[Route("GetResultFromSoapService")]public string GetResultFromSoapService(){string result = string.Empty;try{result= l_WCFSoapProxy.GetSoapString();}catch(Exception ex){throw ex;}return result;}}}

Now Rebuild and Run IIS Express Browser will appear and your have the response from the WCF asmx Service response from your Asp.net Core App

Sign up to discover human stories that deepen your understanding of the world.

--

--

No responses yet

Write a response