Web
Services:
A Web
Service is a software service used to communicate between multiple devices on a
network. More specifically, a Web service is a software application with a
standardized way of proving interoperability between disparate applications. It
does so over HTTP using technologies such as XML, SOAP, WSDL and UDDI
- Web service is a communication between the client and server applications on the World Wide Web.
- Web Services is nothing, but it is one type of service we can be apply to the web application send through soap message. We are using web services through are hosted in IIS Server
- Web service even other languages like java, php application can access the service created by .net this is advantage in web service.
- Web service also one disadvantage Only use for Http protocol not for any other protocol and its only sending data by XML way.
- XML also display by SOAP body
How to
create Web Service:
Right
click on WebApplication project -> Add -> New Item -> web -> Web
Services -> Name (WebService1.asmx) -> Add
It generated like below
In WebService1.asmx.cs file, the code generated in it is the basic
Hello World service. The default web service code behind file looks like the
following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow
this Web Service to be called from script, using ASP.NET AJAX, uncomment the
following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string
HelloWorld()
{
return "Hello World";
}
}
}
WebMethod: it is nothing
but to expose the webservice
Then add one method like below:
[WebMethod]
public int
Addition(int a, int b)
{
return a + b;
}
If you want to Run the web service, Right
click on WebService1.asmx -> click the View
in Browse option
Then click on an Addition method link and check its runs
properly or not.
Then click Invoke button, it’s get
the XML permeate
Access
Web service in Another application:
Go another project like
NewWebApplication -> Right click on References -> Add Service Reference
Now, after clicking on the Service
Reference option, it will show the window given below.
Now click the Discover button.it
will show the window given below
The web services added like below in
ASP.NET MVC application
Add Controller in NewWebApplication
project like HomeController.cs copy
the below code in HomeController page
public
ActionResult GetAdditionResult()
{
ServiceReference1.WebService1SoapClient obj = new
ServiceReference1.WebService1SoapClient();
ViewBag.Result = obj.Addition(45, 25);
return
View();
}
WebService1SoapClient is
a web service class
Add view for GetAdditionResult method
Click Add button then view is added
Then call the viewbage in view page
@ ViewBag.Result
Then build the
application the output comes like below:
No comments:
Post a Comment