Photo Gallery

Sunday, July 9, 2017

How to consume Web Service ? OR How to use WSDL ?


In this post, I will explain how to use WSDL file to consume web service with example. If you want to know WSDL element then please click below link.

WSDL - Element Description

Follow the below step to use WSDL file.

1) Open up Visual Studio.

2) Create a Project (web project or console app : ClientApp - doesn't matter )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFpractice
{ public class FirstWCFService : IFirstWCFService
{
public string Message()
{
return "Hello World, this is my first WCF Service";
}
}
}
3) Right click on Project and click "Add Service Reference"


4) Enter the path and name into the Address to import the WSDL file or to consume web service. 

This will generate WCF client for you to use all method available in that file or service.




5) You should find a "FirstWCFServiceClient" class which should have all the methods defined on the WSDL contract.

using ClientApp.FirstWCFServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ClientApp.Controllers
{
public class HomeController : Controller
{
FirstWCFServiceClient client = new FirstWCFServiceClient();
public ActionResult Index()
{
ViewBag.Message= client.Message();
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
 
6) Instantiate the client and call the methods.



7) You are done. That's all.



If you have any questions, post here. Also, provide your valuable suggestions and thoughts in form of comments in the section below.

Thank you !!!

No comments:

Post a Comment