Labels

Wednesday, September 26, 2007

Attributes to consider while defining Web Method

Attributes to consider while defining Web Method

The 6 properties of the WebMethod attribute are as follows:

§ BufferResponse

§ CacheDuration

§ Description

§ EnableSession

§ MessageName

§ TransactionOption

BufferResponse

§ This property is used to determine if the Web Service Response will be buffered.

§ True -

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

· Very efficient and helps improve performance by minimizing communication.

· 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.

[WebMethod (BufferResponse=true)]

CacheDuration

§ This property allows caching a web service result for a specified period of time (mentioned as seconds).

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

· 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.

· 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)]

[WebMethod(CacheDuration=5)]

public string CreateContentURL(int mediaId, string userIP, int format, int provider, string type)

{

return DateTime.Now.Second.ToString() + ":" + DateTime.Now.Millisecond.ToString();

}

Description

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

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

EnableSession

§ 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)]

void EnterBtn_Click(Object Src, EventArgs E)

{

ServerUsage su = new ServerUsage();

CookieContainer cookieJar;

// Check to see if the cookies have already been saved for this session.

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

cookieJar= new CookieContainer();

else

cookieJar = (CookieContainer) Session["CookieJar"];

// Assign the CookieContainer to the proxy class.

su.CookieContainer = cookieJar;

// Invoke an XML Web service method that uses session state and thus cookies.

int count = su.PerSessionServiceUsage();

// Store the cookies received in the session state for future retrieval by this session.

Session["CookieJar"] = cookieJar;

// Populate the text box with the results from the call to the XML Web service method.

SessionCount.Text = count.ToString();

}

TransactionOption

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

§ Disabled

§ NotSupported

§ Supported

§ Required

§ RequiresNew

Thanks & Regards,

Arun Manglick || Tech Lead

No comments:

Post a Comment