How to consume-web service by ASP.net core application?
In one of my previous posts I have describe How to create ASP.net Core web application to consume a web Service and created a WebServiceSoapDemo project in the solution: WebServiceSoapMaster-Demo . Now I am going to show how to consume our WebServiceSoap Demo via ASP.net core
Source code can be found my Github
Open solution WebServiceSoapMaster-Demo and create a project SP.net core application and call it WebIdentiryDemo (if it is not created before).
I- Add WebServiceSoapDemo Reference for WebIdentiryDemo as following:
- Run WebServiceSoapDemo service, this will open the browser with service URL. In my case my soap URL is https://localhost:44399/SoapDemo.asmx.
- Now, navigate to WebIdentityDemo project solution. Then double click on the Connected Services then the new GUI shall be oppened as follow:’
Click on the Add Service References as shown above. In VS2019 shall the following Error comes up:
In this case open project file of WebIdentiryDemo and remove
<ItemGroup> <Reference Include="System.ServiceModel" /> </ItemGroup>
Then the error disapears and you can see:
select WCF Web service and press to next and give the URL and as you have found in the step 1: (https://localhost:44399/SoapDemo.asmx.) and give change the namespace to WebServiceSoapDemo as shown below:
Press to Next button. a new GUI appears which the checkbox: Reuse types in Reference Assembly, Press Next you see the following:
Press to Finish button:
When we have successfully generated the reference for our WebServiceSoapDemo.
II- Consume Soap Service on ASP.NET Core Application
Add the implementation for the Service on our project. Since the ASP.NET core is an existing project we will add the interface and implementation on the existing class IRepository and Repository classes.
This was the summary of implementation to get the instance of the WebServiceSoapDemo service that we added a while ago.
Before you start the steps below. Install the following NuGet packages.
System.ServiceModel.Http System.ServiceModel.Premitives
- From IRepository class we added a GetInstanceAsync method. This method will instantiate the soap service.
Task<SoapDemoClient> GetInstanceAsync();
2. Then on the implementation class which is Repository.cs. I added this code.
Then on the implementation class which is Repository.cs. I added this code.
public readonly string serviceUrl = "https://localhost:44399/WebServiceSoapDemo.asmx"; public readonly EndpointAddress endpointAddress; public readonly BasicHttpBinding basicHttpBinding;
Repository Contructor:
public Repository(IConfiguration configuration)
{
_configuration = configuration;
endpointAddress = new EndpointAddress(serviceUrl);
basicHttpBinding =
new BasicHttpBinding(endpointAddress.Uri.Scheme.ToLower() == "http" ?
BasicHttpSecurityMode.None : BasicHttpSecurityMode.Transport);
basicHttpBinding.OpenTimeout = TimeSpan.MaxValue;
basicHttpBinding.CloseTimeout = TimeSpan.MaxValue;
basicHttpBinding.ReceiveTimeout = TimeSpan.MaxValue;
basicHttpBinding.SendTimeout = TimeSpan.MaxValue;
}
GetInstanceAsync implementation:
public async Task<WebServiceSoapDemoWebServiceSoapClient> GetInstanceAsync() { return await Task.Run(() => new SoapDemoSoapClient(basicHttpBinding, endpointAddress)); }
3. To use this instance you can call it using the code snippet below.
var client = await GetInstanceAsync();
var result = await client.loginAsync(loginView.Email, loginView.Password);
LoginAsync is the Login method from the SoapDemo service that we added on the Connected Service.
This is now the new code in my LoginAsync method.
public async Task<Response<IdentityModel>> LoginAsync(LoginViewModel loginView)
{
Response<IdentityModel> response = new Response<IdentityModel>();
IdentityModel userModel = new IdentityModel();
try
{
var client = await GetInstanceAsync();
var result = await client.loginAsync(loginView.Email, loginView.Password);
DataTable dt = new DataTable();
dt = JsonConvert.DeserializeObject<DataTable>(result.Body.loginResult.Data);
IdentityModel user = new IdentityModel();
user.ID = int.Parse(dt.Rows[0]["ID"].ToString());
user.Email = dt.Rows[0]["Email"].ToString();
user.Role = dt.Rows[0]["Role"].ToString();
user.Reg_Date = dt.Rows[0]["Reg_Date"].ToString();
response.Data = user;
response.message = (result.Body.loginResult.resultCode == 500) ? "Login failed.Please check Username and / or password" : "data found";
response.code = result.Body.loginResult.resultCode;
}
catch (Exception ex)
{
response.code = 500;
response.message = ex.Message;
}
return response;
}
Run and Test
Run both the WebServiceSoapDemo project and the WebIdentityDemo project. You can use the register option to create a user for you to log in. then this user shall be saved in your database. After you can try to login with this user via login panel.
From VS right click on Solution and then properties, after that check checkboxt Multiple start up Projects and set both project to start and press to applay button as following:
start both project from VS menu: Start then you can see the both Web Service and Client application has been started as follow:
Now you can login with email and password to register a user that shall save in your database and after that you can login with this user to din application.
Source code can be found from GitHub
Conclusion
In this part of Web Services, I have created a Web Service (WebServiceSoapDemo) and a ASP.net Core web application (WebIdentityDemo) via Visual Studio 2019.
Visual Studio 2019 does not have a project template for the ASMX project instead we added a new item manually.Then I have showed how to consume the Web Service by WebIdentityDemo.
This post was part of Web and WCF Services step by step