Presents your XML AND WEB SERVICES E-NEWSLETTER for November 24, 2003 <---------------Advertisement---------------> 64 POLICIES & PROCEDURES, 20 NEW IN THIS EDITION Put this expanded edition in your IT library and have 64 templates to help you create customized policies, guidelines, tools, and forms that are relevant, accessible, and usable. Meet the needs of your entire enterprise with IT Professional's Guide to Policies and Procedures, Third Edition. Whether you"re creating policies for management, training, personnel, support, privacy, Internet/e-mail usage, security, or inventory - you're covered with this one CD! http://ct.com.com/click?q=9c-xtUpQansDQso_2v5~soFtIFXpiIt <-------------------------------------------> DEFINE THE WEBMETHODATTRIBUTE CLASS When working with Web services in .NET, the most common practice for exposing a Web method is to denote that method with the default empty constructor attribute. However, there are several different constructors and attributes you can assign to your Web service methods that can radically affect performance and overall Web service layer design. We'll explore the six core attributes available when exposing a Web service method in Visual Studio .NET. THE ATTRIBUTES The WebMethodAttribute class defines six core public properties that you can set in the constructor: * BufferResponse * CacheDuration * Description * EnableSession * MessageName * TransactionOption Let's review each property and see how they affect the behavior of your Web service methods. BufferResponse (Default = True) This attribute instructs the Web method to either wait until the entire response is serialized or to return the response as it's serialized. When set to True, greater performance is achieved when returning fairly small amounts of data back to the client. CacheDuration (Default = 0) This attribute sets the amount of time in seconds that the response is to be held in cache. If the Web method receives a request containing the exact parameters as a previous request that still has its response cached, the cached response is returned to the client immediately and the method isn't executed. Be very careful when using this attribute, as it may cause adverse side effects by returning a cached response instead of a processed response. Description (Default = String.Empty) The Description property sets a string description for the Web method that service consumers may view when generating description documents for the Web service, such as the WSDL file or the service help page. EnableSession (Default = False) This property indicates whether access to the session state object is enabled. When set to True, you may access the session object as you would in any regular ASPX page. Enabling this property will likely cause performance to decrease. MessageName (Default = Name of the Web service method) If your Web service contains an overloaded method, you must use this property to set a unique name for each overloaded method; otherwise, the WSDL will not be able to differentiate between the overloaded methods. TransactionOption (Default = Disabled) This attribute allows Web services to participate in transactions, but only as the root object in the creation of a new transaction process. To gain access to the TransactionOption class, you must add the System.EnterpriseServices.dll reference to your project and import the System.EnterpriseServices class to your Web service. PROPERTIES IN ACTION LISTING 1.0 shows how to define each of these properties in the attribute. They can fall in any order or in any combination you desire. For the sake of simplicity, we'll define a separate method for each type of property. Listing 1.0 _ Public Function GetLargeDataSet() As DataSet ' code to retrieve and return a large amount of data End Function _ Public Function CachedFunction(ByVal Param1 As Integer, ByVal _ Param2 As Integer) As Integer Return Param1 + Param2 End Function _ Public Function GetString() As String Return "A String" End Function _ Public Function TestSession() As String If Session("UserState") Is Nothing Then Session("UserState") = True Return "This is the first time you have called this _ function" Else Return "You have already called this method once during _ your session" End If End Function _ Public Function Multiply(ByVal Param1 As Integer, ByVal param2 As _ Integer) As Integer End Function _ Public Function Multiply(ByVal Param1 As Long, ByVal Param2 As _ Long) As Long End Function _ Public Sub TransactionFunction() ' Code to perform multiple calls that form a single atomic transaction End Sub UNDERSTAND THE DEFAULTS By understanding each property and its default value, you'll have a greater grasp of what behavior is being applied to your Web service methods. Whether you like it or not, each of these six properties are being used on all of your Web methods with their default values when you denote the method with the empty attribute. Kevin Koch is a senior software engineer with extensive experience in both Microsoft's .NET platform and J2EE technologies. He is a cofounder and president of Task Solutions Inc. ----------------------------------------