Aug. 1, 2009, 11:27 p.m.
posted by angryuser
Customizing Transport InformationAt times, you may need to do some specific things to the underlying transport involved in the client side of the request from a Web service client. Or, on the server side, you may need to read or write certain HTTP-specific information. ASP.NET Web Services lets you do both of these things easily. Setting Exposed HTTP PropertiesIt is possible to set many of HTTP-specific properties on a client proxy, such as UserAgent. Also, on the server side, it is possible to read many of these properties. When a service class is derived from the WebService, the HttpContext object is easily accessed. Listing 6.7 shows how you can use the WebService base class to access the HttpContext. Accessing the HttpContext from a Web Service
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace WhatMyName
{
public class Service1 : System.Web.Services.WebService
{
public Service1()
{}
[WebMethod]
public string EchoUserAgent()
{
return this.Context.Request.UserAgent;
}
}
}
Setting the UserAgent on the client is simple, because this is an exposed property of any created proxy class. Use the following code:
localhost.Service1 s = new localhost.Service1();
s.UserAgent = this.textBox1.Text;
Overriding Proxy Class BehaviorIn some cases, you may want to modify the client's use of its underlying WebRequest object. Do this by overriding the WebClientProtocol method's GetWebRequest method. You can access other transport specific-information not specifically exposed on the client proxy class, by directly manipulating the underlying WebRequest object. For example, you may want to set whether pipelining is on or off, which is an HttpWebRequest-specific property. This kind of override is very easy to accomplish: Just add a method to the generated proxy class, as shown in Listing 6.8. Overriding Proxy Class Behavior by Adding a Method
protected override WebRequest GetWebRequest( Uri url )
{
HttpWebRequest wr = (HttpWebRequest)base.GetWebRequest( url );
wr.Pipelined = this.Pipelined;
return wr;
}
Notice that the base class method GetWebRequest is actually implemented. Therefore, all you really need to do is call the base class method, and then set the property that turns pipelining on and off. In this case, we also have a property on the class we've added that external users of the class can use to set their preference.
The generated proxy class just described will work fine, but if it is regenerated for any reason, via the Update Web Reference command, then all work and changes to the class will be lost. Instead, I highly recommend that you create a new class that derives from the proxy class. This derived class can contain any overridden methods or custom properties you want. For example, the class in Listing 6.9 does exactly that. Overriding Proxy Class Behavior by Using Inheritence
using System;
using System.Net;
namespace SendUserAgent
{
public class DerivedClientProxy : localhost.Service1
{
public DerivedClientProxy()
{
}
public bool Pipelined = false;
protected override WebRequest GetWebRequest( Uri url )
{
HttpWebRequest wr =
(HttpWebRequest)base.GetWebRequest( url );
wr.Pipelined = this.Pipelined;
return wr;
}
}
}
|
- Comment