Web Services – Using C# and
Web Services in C# and .Net
Web Services provide the most flexible infrastructure for creating distributed computing applications. A web service in its simplest form is an object oriented class residing on a web server, and allowing remote clients to invoke its methods. Prior to web services, programmers have relied on sockets, RPC (Remote Procedure Calls), distributed COM (DCOM), and RMI (Remote Method Invocation) technologies to create distributed applications. The major problem with the above solutions is that either they are difficult to program (e.g., sockets), or require both the client and server side to be using the same technology (e.g., DCOM is mostly windows based, and RMI is Java based requiring both the client and server programs to be written in Java). The web services architecture overcomes these limitations. The following are the main motivations behind the development of web services.
1. The client and the web service can be remote from each other.
2. The client and the web service can use a totally different operating system or programming language.
3. The web service can be made available through firewalls that allow port 80 but block other ports.
The above goals are easily accomplished by having the client and the web service use XML (or Http Get/Post protocols for simple type of parameters in method calls) to invoke methods on the service and receive back results. The format of XML used in invoking a web service is a W3C standard known as SOAP (Simple Object Access Protocol).
Here are some of the characteristics of a web service.
1. Web services do not contain any user interface, and are mainly a class or classes exposing some useful methods to a client.
2. A web server is used to host the web service which uses port 80 to make the web service available to clients.
3. The methods of a web service can be invoked either by Http Get, or Http Post, or using SOAP.
4. Http Get/Post techniques are used when the method parameters and results are simple data types. However, for passing back and forth structures and class objects in method parameters, SOAP is the solution. .Net provides HttpGetClientProtocol, HttpPostClientProtocol and SoapHttpClientProtocol classes for using Get, Post or SOAP protocols in the client code.
5. The client of a web service can either be a web browser, a web application or a desktop application (e.g., a windows form application).
6. All clients to a web service require a proxy (very similar to proxy in RPC or DCOM or RMI). The job of proxy is to marshal the parameters and results in method calls to a web service.
7. The client code simply invokes an object of the proxy class (which has same methods and their signatures as the methods in the web service), and calls the proxy methods. The proxy, serializes the method calls to the web service using either Http Get/Post or SOAP, and also deserializes the results back to the client.
8. If the client is a web browser, or a web application, the proxy code is downloaded from the web server hosting the web service. If the client application is a desktop application. The proxy code has to be generated in advance and resides on the client.
9. Since web services reside on the web server and are accessed by clients using Http (SOAP is also using Http to send back and forth XML packets) which is a stateless protocol, maintaining state between method calls is possible through application or session objects.
10. The client of a web service should be able to locate (discovery) and find out the prototypes of methods (description) in web service. For this purpose, the web service provides a discovery file and a description (wsdl format) file that lists all the public methods and their prototypes in XML format. Web services can be registered with a UDDI (Universal Description, Discovery and Integration) server which can act as the yellow pages for web services.
11. The web service methods can use any .Net primitive data type (e.g., int, float, Double, string, DateTime, Decimal, Boolean, Object) in parameters or return values. Arrays and ArrayLists can also be used. User defined classes or structures used in parameters or return types require special XML streaming in using SOAP.
Web Services in .Net have a file extension of .asmx. It is possible to type all the class code in an asmx file using a simple editor like notepad. Visual Studio uses the code behind technique for creating web services. The benefit of the code behind technique in general is that it separates the user interface from the underlying code. However, in case of web services, since there is no user interface, the code behind technique has the advantage that the class code is precompiled in advance. It is possible to create a web service where all the code is written in the .asmx file which will get compiled on its first access by the user.
A web service class is usually derived from System.Web.Services.WebService class. Each public method of the web service class that needs to be made available as a web service will be marked with an attribute of [WebMethod].
Creating a simple Web Service Using C#:
Create an Web Service type project. Name the project StInfo.
[pic]
Change the default name of the file from Service1.asmx to StInfo.asmx. Web services in .Net have files with an extension of .asmx.
Type the following code in the StInfo.asmx.cs file.
using System;
using System.Collections;
using ponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace StInfo
{
///
/// Summary description for Service1.
///
public class StInfo : System.Web.Services.WebService
{
string [,] students =
{
{"062987","Andrew", "Anserson","3.45","CS"},
{"062988","Jessica", "Johnson","3.75","CS"},
{"062989","Monica", "Marker","3.15","EE"},
{"062990","Michael", "Jordan","2.45","MBA"},
{"062991","Sally", "Simpson","3.12","EE"},
{"062987","Mark", "Mathews","2.85","CS"},
{"062987","Sara", "Sorenson","3.52","MBA"}
};
public StInfo()
{
//CODEGEN: This call is required by the Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent(){ }
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
[WebMethod]
public string GetFirstName(string StID)
{
// return first name for a given student ID
for (int i = 0; i < students.GetLength(0);i++)
{
if (pare(StID,students[i,0],true) == 0)
return students[i,1];
}
return "Student does not exist with this ID";
}
[WebMethod]
public string GetLastName(string StID)
{
// return first name for a given student ID
for (int i = 0; i < students.GetLength(0);i++)
{
if (pare(StID,students[i,0],true) == 0)
return students[i,2];
}
return "Student does not exist with this ID";
}
[WebMethod]
public float GetGPA(string StID)
{
// return first name for a given student ID
for (int i = 0; i < students.GetLength(0);i++)
{
if (pare(StID,students[i,0],true) == 0)
return float.Parse(students[i,3]);
}
return 0;
}
[WebMethod]
public string GetMajor(string StID)
{
// return first name for a given student ID
for (int i = 0; i < students.GetLength(0);i++)
{
if (pare(StID,students[i,0],true) == 0)
return students[i,4];
}
return "Student does not exist with this ID";
}
[WebMethod]
public string[] GetAllStudents()
{
// return first name for a given student ID
string [] sts = new string[students.GetLength(0)];
for (int i = 0; i < students.GetLength(0);i++)
{
sts[i]=students[i,0];
}
return sts;
}
}
}
Even though the Visual Studio does not show the contents of the asmx file, you can view it by opening it in notepad. For example, the contents of StInfo.asmx look as:
As you can see, the main job of the asmx file is to indicate that first of all, it is a web service using C# and to identify the code behind file (StInfo.asmx.cs in above case).
Choose Build->Build Solution to compile the web service. If there are no errors, you can test the web service by either choosing Debug-> Start, or directly typing in the browser.
You will see the following screen.
[pic]
If you click on the link GetLastName, you will see the following page which has a simple form that can be filled out to find out the student last name corresponding to the student ID (the form uses the Http GET method).
[pic]
If you type 62990 and click on invoke, you will get the following result.
[pic]
Similarly, you can test the GetAllStudents link. When you click on the invoke button, you will see the following result.
[pic]
The page that shows the Invoke button also gives hints about how you can use the SOAP or Http POST protocols to invoke the web service methods, and the general form of the request and response headers for these protocols. For example, to invoke the GetLastName method using SOAP, the format of request response headers is:
SOAP
The following is a sample SOAP request and response. The placeholders shown need to be replaced with actual values.
POST /stinfo/stinfo.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: ""
string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
string
To use POST method for invoking the GetLastName method, the format of request response headers is:
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
POST /stinfo/stinfo.asmx/GetLastName HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
StID=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
string
Using POST method to Invoke the Web Service Methods:
Using notepad, create a simple ASP page as shown below.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- list of topics
- introduction bing
- valdosta state university
- web services using c and
- introduction to c part 2
- learning objectives home autodesk community
- refactor person to use the domain model and repository
- partial comparison of java and c for the java programmer
- programing guidelines
- cheat sheet all cheat sheets in one page
Related searches
- amazon web services revenue
- amazon web services revenue 2018
- amazon web services profitability 2018
- amazon web services revenue history
- amazon web services financials
- amazon web services annual report
- amazon web services revenue 2019
- amazon web services strategic plan
- amazon web services cloud
- amazon web services growth
- amazon web services history
- amazon web services cloud platform