Webservices introduction
[Webservices, an introduction]
Webservices are used to share data between applications. Webservices are not .Net specific, but in .Net they are very easy to create. Webservices use XML to transfer data. In this example I will explain a webservice which returns the country based on an IP Address. It uses the code described in the article: Visitor's Country.
You can see the webservice in action by opening the following link: GetCountryFromIP.asmx
[Creating the Webservice]
If you create a new webservice using Visual Studio you will have your first working webservice. The code added by Visual Studio is basically all you need. It even contains a webmethod for the famous "Hello World". You can see the Hello World webservice by opening the following link: HelloWorld.asmx
[GetCountry Method]
The webservice that is build uses the following code, you might recognize it from the article Visitor's Country. If added some extra input checks, because we cannot use RegularExpressionValidator on the input.
The current method isn't a webmethod yet. To do this you'll need to add the WebMethod attribute ([WebMethod]).
[GetCountry Method #2]
I've created an overload for the GetCountry Method, which doesn't need an IP Address as input. It uses the visitors IP Address (using HttpContext.Request.ServerVariables["REMOTE_ADDR"]) as input.
Both methods aren't available as WebMethod, because they miss the [WebMethod] attribute. But if we add the attribute to both methods, you will receive an error when you open the webservice (it will compile without any warnings/errors). This is because there are two GetCountry methods. In a normal application they are distinguished by its signature (which variables (or of which type) it need as input, or which return type it has), but a WebService can't.
Therefore we have to describe the separate methods. The WebMethod attribute uses some properties for this; you can add a description (which is just what it says it is) and a MessageName. The MessageName property allows the method to be accessed by a different name. For the overload method (which returns your own country I've added the following attribute (with properties):
For the original method, I've added the webmethod with different properties (with only a description):
[Adding it all together]
When we add this all into one class we'll end up with the following code:
Webservices are used to share data between applications. Webservices are not .Net specific, but in .Net they are very easy to create. Webservices use XML to transfer data. In this example I will explain a webservice which returns the country based on an IP Address. It uses the code described in the article: Visitor's Country.
You can see the webservice in action by opening the following link: GetCountryFromIP.asmx
[Creating the Webservice]
If you create a new webservice using Visual Studio you will have your first working webservice. The code added by Visual Studio is basically all you need. It even contains a webmethod for the famous "Hello World". You can see the Hello World webservice by opening the following link: HelloWorld.asmx
1: using System;
2: using System.Collections;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Diagnostics;
6: using System.Web;
7: using System.Web.Services;
8:
9: namespace markberck.WebServices
10: {
11: ///
12: /// Summary description for HelloWorld.
13: ///
14: public class HelloWorld : System.Web.Services.WebService
15: {
16: public HelloWorld()
17: {
18: //CODEGEN: This call is required by the ASP.NET Web Services Designer
19: InitializeComponent();
20: }
21:
22: Component Designer generated code
48:
49: // WEB SERVICE EXAMPLE
50: // The HelloWorld() example service returns the string Hello World
51: // To build, uncomment the following lines then save and build the project
52: // To test this web service, press F5
53:
54: [WebMethod]
55: public string HelloWorld()
56: {
57: return "Hello World";
58: }
59: }
60: }
[GetCountry Method]
The webservice that is build uses the following code, you might recognize it from the article Visitor's Country. If added some extra input checks, because we cannot use RegularExpressionValidator on the input.
1: public string GetCountry(string IPAddress)
2: {
3: if (IPAddress.IndexOf(".") > -1)
4: if (IPAddress.Split('.').GetUpperBound(0) == 3)
5: {
6: string ConnectionString =
7: ConfigurationSettings.AppSettings["sqlConString"];
8: string sqlQuery = "GetCountryFromIP ";
9: sqlQuery += IPAddress.Split('.')[0] + ", ";
10: sqlQuery += IPAddress.Split('.')[1] + ", ";
11: sqlQuery += IPAddress.Split('.')[2] + ", ";
12: sqlQuery += IPAddress.Split('.')[3];
13:
14: SqlConnection sqlCon = new SqlConnection(ConnectionString);
15: SqlCommand sqlCom = new SqlCommand(sqlQuery, sqlCon);
16:
17: string country;
18: try
19: {
20: sqlCon.Open();
21: country = sqlCom.ExecuteScalar().ToString();
22: }
23: catch
24: {
25: country = "Unknown (IP Address could be local network)";
26: }
27: finally
28: {
29: sqlCon.Close();
30: }
31: return country;
32: }
33: return "Please enter a correct IP Address!";
34: }
The current method isn't a webmethod yet. To do this you'll need to add the WebMethod attribute ([WebMethod]).
[GetCountry Method #2]
I've created an overload for the GetCountry Method, which doesn't need an IP Address as input. It uses the visitors IP Address (using HttpContext.Request.ServerVariables["REMOTE_ADDR"]) as input.
1: public string GetCountry()
2: {
3: string ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
4: return this.GetCountry(ipAddress);
5: }
Both methods aren't available as WebMethod, because they miss the [WebMethod] attribute. But if we add the attribute to both methods, you will receive an error when you open the webservice (it will compile without any warnings/errors). This is because there are two GetCountry methods. In a normal application they are distinguished by its signature (which variables (or of which type) it need as input, or which return type it has), but a WebService can't.
Therefore we have to describe the separate methods. The WebMethod attribute uses some properties for this; you can add a description (which is just what it says it is) and a MessageName. The MessageName property allows the method to be accessed by a different name. For the overload method (which returns your own country I've added the following attribute (with properties):
[WebMethod( Description="Get Your Country", MessageName="GetOwnCountry")]
For the original method, I've added the webmethod with different properties (with only a description):
[WebMethod(Description="Get Country for IP Address")]
[Adding it all together]
When we add this all into one class we'll end up with the following code:
1: using System;
2: using System.Web;
3: using System.Web.Services;
4: using System.Configuration;
5: using System.Data.SqlClient;
6:
7: namespace markberck.WebServices
8: {
9: [WebService(
10: Description="Get Country based on IP Address",
11: Namespace="http://markberck.nl/webservices/")]
12: public class GetCountryFromIP : System.Web.Services.WebService
13: {
14: [WebMethod(
15: Description="Get Your Country",
16: MessageName="GetOwnCountry")]
17: public string GetCountry()
18: {
19: string ipAddress =
20: HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
21: return this.GetCountry(ipAddress);
22: }
23: [WebMethod(Description="Get Country for IP Address")]
24: public string GetCountry(string IPAddress)
25: {
26: if (IPAddress.IndexOf(".") > -1)
27: if (IPAddress.Split('.').GetUpperBound(0) == 3)
28: {
29: string ConnectionString =
30: ConfigurationSettings.AppSettings["sqlConString"];
31: string sqlQuery = "GetCountryFromIP ";
32: sqlQuery += IPAddress.Split('.')[0] + ", ";
33: sqlQuery += IPAddress.Split('.')[1] + ", ";
34: sqlQuery += IPAddress.Split('.')[2] + ", ";
35: sqlQuery += IPAddress.Split('.')[3];
36:
37: SqlConnection sqlCon = new SqlConnection(ConnectionString);
38: SqlCommand sqlCom = new SqlCommand(sqlQuery, sqlCon);
39:
40: string country;
41: try
42: {
43: sqlCon.Open();
44: country = sqlCom.ExecuteScalar().ToString();
45: }
46: catch
47: {
48: country = "Unknown (IP Address could be local network)";
49: }
50: finally
51: {
52: sqlCon.Close();
53: }
54: return country;
55: }
56: return "Please enter a correct IP Address!";
57: }
58: }
59: }