namrata's profileNashaPhotosBlogListsMore ![]() | Help |
|
|
December 23 Article : WebServices Series Part - 5 ( Sync & Async Communication )Yesterday we saw how we can communicate with web services using callbacks. Today we will discuss the other two techniques Polling and Waithandles. Polling : When we made a call to our web service using callback we passed it reference to our fucntion and in return we get an IAsyncResult object back which acted as a token. WaitHandles : This method also uses one of the properties of IAsyncResult object called AsyncWaitHandle. Article : WebServices Series Part - 4 ( Sync & Async Communication )Today we will checkout how to consume our web service using SOAP in Synchronous and Asynchronous modes. WebServices Series Part - 3 (Accessing with HTTP POSTLOCALHOST, POST and GET)Today we will look how we can access web services with HTTP-GET and HTTP-POST protocols. .Net Framework 1.0 had all the three protocols i.e. HTTP-POST,HTTP-GET AND HTTP-SOAP enabled by default. But in version 1.1 only HTTP-SOAP is enabled by default. Hence if you want to access the web service my HTTP-POST or GET then you will have to enable it explicitly. Since HTTP-POST is not enabled by default in .Net framework 1.1 to test web service on the local machine there is a new protocol that has been added i.e. HTTPPOSTLOCALHOST. It is because of this protocol that while testing a web service you are able to view a test form on your local machine and not on a remote computer. Let us check out each of these protocols individually: - HTTPPOSTLOCALHOST: - This is enabled by default in .Net Framework version 1.1. Hence if you access your website as http://localhost you will get the test form but if you test the same web service with http://<name of your computer> from some other computer than the test form will not be displayed. e.g. Try and access our trigonometry web service from your machine and then access it from any other machine. You will notice that the test form will not be displayed. Before we look at HTTP-POST and HTTP-GET we need to checkout DefaultWsdlHelpGenerator.aspx. DefaultWsdlHelpGenerator.aspx is the default wsdl help file located at <drive>:\WINNT\Microsoft.NET\Framework\v1.1.4322\CONFIG. Open DefaultWsdlHelpGenerator.aspx and checkout showPost flag. bool showPost = true; // search for this Depending the value of showPost flag HTTP-GET or HTTP-POST protocol is used.Set its value to true if you want to see a POST test form and false to view a GET test form. Along with this you will have to include the appropriate protocols in the web services tag in Web.config. HTTP-POST: - To enable our web service with HTTP-POST we will have to make the following changes. 1. Open the web.config and add the <webServices> tag under <system.web> as follows to enable the HTTP-POST protocol. <webServices> 2. Open DefaultWsdlHelpGenerator.aspx and set the value of showPost to true. By default it is set to true. http://namrathas/Trigonometery/MyService.asmx/CoSine 5. Execute your web service from some other computer and you will be able to view HTTP-POST test form. HTTP-GET: - To enable our web service with HTTP-GET we will have to make the following changes. 1. Open the web.config and add <add name="HttpGet"/> to protocols tage s follows to enable our webservice with HTTP-GET protocol also. <webServices> 2. Open DefaultWsdlHelpGenerator.aspx and set the value of showPost to false. So now HTTP-POST form will be displayed. All the parameters will be passed as querystring. http://namratas/Trigo/MyService.asmx/CoSine?angel=5 3. Run the web service and checkout the HTTP-GET form. Its looks the same as HTTP-POST form. You will notice the difference on execution. HTTP-SOAP: - Whenever our webservice is accessed from any application HTTP-SOAP protocol is used. HTTP-SOAP is enabled by default. We will look in to more details of HTTP-SOAP when we look into consuming webservices. Hence if want our webservice to be enabled for all the four protocols we need to add them in the <protocols> tag. <webServices> WebServices Series Part - 2 (WebMethod and WebService Properties)After creating our basic web service yesterday we will look into the web method properties and supported data types. Well Whenever we create a web service all those methods of the web service which we want to be web-callable have to be marked with the web method attribute and should have a public access modifier. Web Method Attribute is basically used to make a method web callable. WebMethod attribute has the following properties like :-
Description :- It is used for commenting the web method. The description that is added here is included in WSDL. Add the webmethod description property above any method as shown below. When you call the webservice from a browser you view the web service.
e.g. [WebMethod(Description = "My WebMethod Description comes here")]
Enable Session :- WebServices are a type of ASP.NET applications hence you can access your ASP.NET application Session from your webservice. By default the session is disabled i.e. set to false. You can enable it by setting its value to true.
e.g. [WebMethod(EnableSession ="true")]
Message Name :- Message Name property is used give an alias to a method name. This is esp. useful when you are using overloaded methods.
e.g. Yesterday we created 3 method in our webservice for cos,sin and tan which took double as an input parameter. Today we will create their overloads that will take int as an input parameter
[WebMethod]
If you compile this code it will compile successfully. It is on execution that you we view the below error message
Error Message: Both Double Tan(Int32) and Double Tan(Double) use the message name 'Tan'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.
Web service needs to identify each method uniquely hence we will give alias to the methods by setting the MessageName property.
e.g. [WebMethod(MessageName = "SinInt")]
TransactionOption : - If our web service is a part if any COM/MTS transaction application then we can enable the Transaction Option of the webservice thus making it a part of the 2 phase commit protocol of MTS.
e.g. [WebMethod(TransactionOption = "Supported")]
The dafault is same as COM/MTS transactions i.e. Required.
CacheDuration :- Like any other ASP.NET applications WebServices also support output caching. Output caching gives a performance boost coz the same request is not executed repeatedly instead the result is return from the cache itself. This feature is esp. useful when you are retrieving chunks of data from the database for listing and other purposes. But caching should preferably used with only those methods where we know that the data will not change frequently.
e.g. [WebMethod(CacheDuration = "180")]
BuffferResponse :- We all are familiar with the buffering property rite from our ASP days where used Response.Buffer = true so that the response is only sent when the Response for the request is completely generated or when the buffer is full. WebServices also use the same logic. If we want our response to be sent across only after its complete processing is done we need to buffer our response by setting its property to true.
e.g. [WebMethod(BufferResponse = "true")]
By default it is set to true.
These properties were webmethod properties. We also have some web service properties like
Description :- This is same as web method property. It is used to comment web service and give it a description that is included in WSDL.
e.g. [WebService(Description = "My WebService Description comes here")] {
e.g. [WebService(Name = "MyNewName")]
NameSpace :- The default name space is http://tempuri.org. You can change this to http://localhost/MyWebServices. You should set namespace to any unique value.
e.g. [WebService(Namespace = "http://localhost/MyWebServices")] WebServices Series Part - 1 [ Basic Creation ]Web services are distributed asp.net applications that are designed to share business logic over the network. |
|
|