In-Sights into Web Services - Part 2

Introduction

In the first part of my article we discussed about the various basic elements of Web Services. In this article let me try and explain the Web Services Attributes and Methods in a simpler manner.

Web Services in .NET:

As told earlier, in .Net a web service is a ASP.NET application with a file that has <.asmx> extension. The <.asmx> file is like the main function in a C program. In other words it is the starting point of the web service application. It can either contain code within itself or code in a code-behind file <.asmx.cs or .asmx.vb> which is the default style followed in Visual Studio.Net.

The <asmx> file has the “WebService” processing directive, which provides information as to which code-behind file and class implement the functionality of the web service. This is implemented by the following attributes:

Using Syntax:

<%@ WebService Language=”C#” Codebehind=”Service1.asmx.cs” Class=”WebService1.Service1” %>

The code-behind file <eg: .asmx.cs> defines the web service class, which inherits from “System.Web.Services.WebService”. All the methods of the class are not accessible over the web. Optionally the WebService attribute can be used to specify certain properties for the web service.

The WebService attribute has four properties, which provide information about the web service. We now have a web service class but then what is the purpose of it when it doesn’t expose its functionality over the web?

The WebMethod attribute is used to expose a public method of a web service class over the web. The WebMethod attribute has six properties, which control the way the web method operates over the web.

WebService Attribute:

The four properties of the WebService attribute are as follows:


Each of the above properties is explained in detail below:

Namespace: In order for an XML Web Service to be distinguishable over the web from other web services, the web services should use a unique namespace. In VS.NET the default namespace is http://tempuri.org . This is fine during the development phase. But a unique namespace should be provided when the web service is published in the UDDI. The namespace does not have to be an existing URL. The main purpose of the namespace is only to uniquely identify the Webservice. The ‘Namespace’ property of the WebService attribute is used to specify a namespace for the web service.

Usage syntax:

<WebService(Namespace:= http://www.vadivel.com/
Name:= “VadiWebServiceforMathFunction”)>_

Public Class Service1
Inherits System.Web.Services.WebService

Description: This property is used for providing a brief description to a web service.

Usage Syntax:

<WebService(Description:= “This is Math Function Webservice:, Name:=”Vadivel Mathematical Functions Service”)>_ Public Class Service1
Inherits System.Web.Services.WebService


TypeId: It is used to uniquely identity the type of the attribute in WebMethod Attribute. But to my knowledge it is very rarely used.

WebMethod Attribute


The 6 properties of the WebMethod attribute are as follows:

  • Description
  • MessageName
  • EnableSession
  • CacheDuration
  • TransactionOption
  • BufferResponse

Each of the above properties is explained in detail below:

Description: This property provides a brief description to the web method.

Usage syntax:

<WebMethod(Decription:=”This method adds two integer”)>
Public Function Add2Integer(ByVal Num1 As Integer, ByVal Num2 As Integer) As Integer
Return Num1 + Num2
End Function

The web method described above takes 2 integers as parameters and returns their sum also as an integer.

MessageName: This property is useful particularly when web methods need to be overloaded.

Function overloading is accomplished when functions have the same name but different implementation.

Usage Syntax:

<WebMethod (MessageName:=”SayHello”)>
Public Function SayHello() As String
Return “Hi buddy ... Happy new year”
End Function

<WebMethod()>
Public Function SayHello (ByVal sName As String) As String
Return “Hi” & “ “ & sName
End Function


In the above example, the SayHello method is overloaded. One method takes parameters and other does not.

EnableSession:

HTTP is a stateless protocol. EnableSession property helps to maintain session state between server and the client. By default the EnableSession property is false. By setting this property to true the session between the first and subsequent requests can be maintained. If EnableSession is false, session state cannot be maintained at any point.

Usage Syntax:

<WebMethod (EnableSession:=true)>

CacheDuration:

CacheDuration property allows caching a web service result for a specified period of time (mentioned as milliseconds). Its nothing but a memory buffer which holds the result of a previously accessed method by a web services consumer. It increases the performance of the web service.

Let us say we have a webservice, which provides the list of trains starting from a particular location. When a user accesses this Web Service for the first time the result of this request is cached in memory. If during the next time some other user request the same service the result will be served from cache. This speeds up web service requests.

But caching output blindly for all web services doesn’t make sense. Let us consider a web service, which provides stock quotes. In this case, caching is not worth because stock quotes change every second.

Usage Syntax:

<WebMethod(CacheDuration:=60)>
Public Function PrintTime() As String
Return Now().ToString
End Function

In the above example, the cache duration is 60 milliseconds. If the same method is invoked within 60 milliseconds, both requests will print the same time.

TransactionOption:

A transaction is a set of events that are committed or rolled back as a unit. Either all of the events happen or none of them do.

For instance, transferring Rs. 10000 from your savings account and adding it to your checking account. To achieve a consistent result, either both operations should succeed or fail.

The transaction options supported by the TransactionOption property are listed below:

Transaction Options
Description
Disabled
Ignore any transaction in the current context
NotSupported
Creates the component in a context with no governing transaction
Required
Shares a transaction if one exists and creates a new transaction if necessary.
RequiresNew
Creates the component with a new transaction regardless of the state of the current context
Supported
Shares a transaction if one exists

Usage Syntax:

<WebMethod (TransactionOption:=TransactionOption.Requiresnew)>
Public Function DoSomeTrans() As String
...
ContextUtil.SetComplete
DoSomeTrans.Transactional = ContextUtil.TransactionId.ToString()
End Function

BufferResponse:

This property is used to determine if the Web Service Response will be buffered. When set to true (which is the default), ASP.NET buffers the entire response before sending it to the client. This type of buffering is very efficient and helps improve performance by minimizing communication between the worker process and the IIS process.

When set to false, ASP.NET buffers the response in chunks of 16 KB. Typically you set this property to false only if you do not want the entire contents of the response to be stored in memory at one time.

Usage Syntax:

<WebMethod (BufferResponse:=true)>

Conclusion


I guess this article gave a decent introduction about the various Attributes of Web Services and WebMethod. As I always say, feel free to ping me at vmvadivel@gmail.com to discuss further on this or catch me at http://vadivel.thinkingms.com .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值