Labels

Monday, July 7, 2008

05 - Web Method Using Attributes

Hi,

When you create an XML Web service, you can use WebMethod attributes to define the behavior of the methods exposed by the Web service

Attributes of a Web Method

WebMethod attributes can be applied to any method that we want to expose as a method of an XML Web service. The WebMethod attribute provides the following features that enable you to customize and control the behavior of the methods that you expose in your Web service.

BufferResponse

Boolean

Specifies whether the response of a Web method is buffered in memory.

CacheDuration

Integer

Specifies the number of seconds the response of a Web method is cached in memory

Description

String

This property describes a Web method

EnableSession

Boolean

Specifies whether the session state is enabled for a Web method. If the session state is enabled, the Web method can access the HttpSessionState object of ASP.NET

MessageName

String

Specifies an alias name to a Web method

TransactionOption

TransactionOption Enum

Specifies the transaction support for a Web method

BufferResponse: Default = True

Very efficient and helps improve performance by minimizing communication between the worker process and the IIS process

True -

· When set, ASP.NET buffers all the responses before communicating them to the client.

· In actual, ASP.NET buffers the entire response after serializing and before sending it to the client

· Use when small amounts of data is sent to the client.

False –

· Response to the XML Web service method is sent back to the client as it is serialized.

· Use when large amounts of data is sent to the client.

Syntax –

[WebMethod (BufferResponse=true)]

CacheDuration: Default = 0

· Caches a Web Service result for a specified period of time (mentioned as seconds).

· The default value of the CacheDuration property is zero

· There are two issues that can affect output caching in an ASP.NET 2.0 Web service application.

o In ASP.NET 2.0 the HTTP method of the test page has changed from GET to POST. However, POSTs are not normally cached. If you change the test page in an ASP.NET 2.0 Web service application to use GET, caching works properly.

o In addition, HTTP indicates that a user agent (the browser or calling application) should be able to override server caching by setting the "Cache-Control" to "no-cache". ASP.NET applications, therefore, ignore cached results when they find a "no-cache" header.

[WebMethod (CacheDuration=60)]

Description

· This property provides a brief description to the web method.

[WebMethod (Description =”<Anything>”)]

EnableSession: Default = False

· This enables a Web method to maintain the state of objects across sessions.

· If you set the EnableSession property to True, the XML Web service accesses the session state collection directly from the HttpContext.Current.Session session or the WebService.Session property if it is inherited from the WebService base class.

HTTP is a stateless protocol. EnableSession property helps to maintain session state between server and the client.

· In order for an XML Web service to maintain session state for a client, the client must persist the cookie.

· Clients can receive the HTTP cookie by creating a new instance of CookieContainer and assigning that to the CookieContainer property of the proxy class before calling the XML Web service method.

· If you need to maintain session state beyond when the proxy class instance goes out of scope, the client must persist the HTTP cookie between calls to the XML Web service. For instance, a Web Forms client can persist the HTTP cookie by saving the CookieContainer in its own session state.

[WebMethod (EnableSession = true)]

E.g

void EnterBtn_Click(Object Src, EventArgs E)

{

Service service = new Service();

CookieContainer cookieContainer;

if (Session["Cookie"] == null)

{

cookieContainer = new CookieContainer();

}

else

{

cookieContainer = (CookieContainer)Session["Cookie"];

}

service.CookieContainer = cookieContainer; // Set before calling Web Service

int count = service.PerSessionServiceUsage(); //Invoke an XML Web service method that uses session state and thus cookies.

Session["Cookie"] = cookieContainer;// Store the cookies received in the session state for future retrieval by this session.

Textbox1.Text = count.ToString();

}

MessageName

· By default Web Services do not support ‘Method Overloading’.

Reason - When data is passed to an XML Web service it is sent in a request and when it is returned it is sent in a response. Therefore, if an XML Web service contains two or more XML Web service methods with the same name, no uniquely identification will be there. Hence it will produce error.

· To resolve this, MessageName property is required to be attached to Web Method - To uniquely identify polymorphic/ overloaded methods.

· When we specify a value for the MessageName property, the SOAP messages Uses This Value As The Method Name instead of the actual method name.

[WebMethod(MessageName="AddIntegers")]
public int AddNumbers(int num1, int num2)
{
return (num1+num2);
}


[WebMethod(
MessageName="AddLongs")]
public long AddNumbers(long num1, long num2)
{
return (num1+num2);
}

TransactionOption

· Enable Web method to participate in a transaction or not.

· You can use a Web method to support transactions and enable a Web method to participate as the Root Object of a transaction.

· The TransactionOption property can take the following values:

To disable transaction support:

o Disabled

o NotSupported

o Supported

To enable transaction support and enable a Web method to participate as the root object of a transaction:

o Required

o RequiresNew

· To enable Web methods to support transactions, in addition to setting a value for the TransactionOption you need to do below:

o Need to add a reference to the System.EnterpriseServices.dll file and

o Include the System.EnterpriseServices name-space in addition to setting a value for the TransactionOption property.

This name-space contains the methods and properties that expose the distributed transaction model in COM+ services.

The ContextUtil class of the System.EnterpriseServices namespace enables you to control a transaction using the SetAbort or SetComplete method

[WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public string DoSomethingTransactional()
{
//The transaction was successful...
ContextUtil.SetComplete();
return ContextUtil.TransactionId.ToString();
}

Thanks & Regards,

Arun Manglick || Senior Tech Lead

No comments:

Post a Comment