Customizing the Webmethod Attribute
Create and consume an XML Web service.
You've seen how easy it is to generate a Web service by using Visual Studio .NET, simply by marking published methods with the WebMethod attribute. But there are also a number of ways to customize your Web service by adding properties to that attribute. Figure shows the properties that you can use to customize the WebMethod attribute.
Figure Properties of the WebMethod Attribute|
BufferResponse | Indicates whether the response should be buffered. If set to true (the default) the entire response is buffered until the message is completely serialized and is then sent to the client. If set to false, the response is sent to the client as it is serialized. | CacheDuration | Specifies the number of seconds that ASP.NET should cache results on the server. The default is 0, which disables caching. | Description | Supplies a description for the Web method. | EnableSession | Indicates whether the session state should be enabled. The default is false. | MessageName | Specifies the name by which the method will be called in SOAP messages. The default is the method name. | TransactionOption | If set to TransactionOption.Required or TransactionsOption.RequiredNew, enables the Web method to participate as the root object of a transaction. The default is TransactionOption.Disabled. |
In the Step-by-Step 4.4, you'll see how you can use several of these properties to customize a Web method.
|
4.4 Customizing a Web Service
Open the StringProc Web Service project that you created in Step-by-Step 4.2. Add this Web methods definition to the existing Strings class definition:
[WebMethod(CacheDuration = 60,
Description = "Method to demonstrate caching")]
public String CachedString(int intInput)
{
return "The time is " +
DateTime.Now.ToLongTimeString();
}
[WebMethod(Description = "Method without caching")]
public String UncachedString(int intInput)
{
return "The time is " +
DateTime.Now.ToLongTimeString();
}
[WebMethod(EnableSession = true,
Description = "Store a value in session state")]
public String SetSession(int intInput)
{
Session["StoredInt"] = intInput;
return "Value " + intInput +
" stored in session state";
}
[WebMethod(EnableSession = true,
MessageName = "Get Session",
Description=
"Retrieve a value from session state")]
public String GetSession()
{
if (Session["StoredInt"] != null)
{
int intOutput = (int) Session["StoredInt"];
return "Value " + intOutput +
" retreived from session state";
}
return "";
}
Run the project. This opens the test page shown in Figure. Notice that the test page displays the description property of each Web method.

Click on the UncachedString link to open a test page for the UncachedString() method. Enter a value for the input parameter and click Invoke. You should see a return message with the current time. Click Invoke again. You should now see the updated current time. Click the link to return to the complete list of operations. Click on the CachedString link to open a test page for the CachedString() method. Enter a value for the input parameter and click Invoke. You should see a return message with the current time. Click Invoke again. You should now see the same current time. That's because the result from the CachedString() method is cached for sixty seconds. ASP.NET returns the cached value during that time without calling the original method. Change the input parameter and click Invoke again. You should see a return message with the current time. When an input parameter changes, ASP.NET does not use the cached result. Click the link to return to the complete list of operations. Click the link for the SetSession() method. Enter an input value and click Invoke. You should see a confirmation message. Click the link to return to the complete list of operations. Click on the link for the GetSession() method. Notice that this link uses the MessageName property of the WebMethod attribute rather than the name of the underlying method. Click Invoke. You should now see the value retrieved from the session state.
|
Step-by-Step 4.4 demonstrates all the WebMethod attribute properties except for BufferResponse and TransactionOption. The only time that you'll want to set BufferResponse to true is when you're returning more than 16KB of data, and that data is being constructed on the fly—for example, when you are retrieving and formatting records from a database.
WARNING
Be Wary of Session State
You should be cautious about using session state in a Web service. Session state can tie up server resources for an extended period of time. If you use session state with a popular Web service, you will apply heavy demands to Random Access Memory (RAM) on your Web server.
If you're interested in performing transactions within a Web service, you should look at the WS-Transaction portion of the new Global XML Architecture (GXA) specification, rather than use the TransactionOption property. See msdn.microsoft.com/library/default.asp?url=/library/en-us/dngxa/html/gloxmlws500.asp for more information on this advanced topic, which you will not find on the exam.
|