Impossible to inherit from HttpWebRequest?
-
Yesterday I was trying to inherit from HttpWebRequest, to implement a protocol that extends HTTP. In the end, I think it is impossible...although the class itself can be inherited from, its main constructor is effectively private (probably declared "internal"), which means that my derived class cannot construct the base class. This is due to the nature of the HttpWebRequest, which is always created through the WebRequest.Create() method, never directly through its constructor. Does anyone have any experience with or insight into this?
-
Yesterday I was trying to inherit from HttpWebRequest, to implement a protocol that extends HTTP. In the end, I think it is impossible...although the class itself can be inherited from, its main constructor is effectively private (probably declared "internal"), which means that my derived class cannot construct the base class. This is due to the nature of the HttpWebRequest, which is always created through the WebRequest.Create() method, never directly through its constructor. Does anyone have any experience with or insight into this?
How do you want to extend HTTP? If you want to add headers, you can add them to the
Headers
collection. If you want to use a custom verb, you can do that too: set theMethod
property. If you want to customise these properties in a fixed way, and still plug in toWebRequest.Create
, I think you'll have to derive a new class fromWebRequest
, have anHttpWebRequest
as a member, and forward methods to yourHttpWebRequest
. Stability. What an interesting concept. -- Chris Maunder -
How do you want to extend HTTP? If you want to add headers, you can add them to the
Headers
collection. If you want to use a custom verb, you can do that too: set theMethod
property. If you want to customise these properties in a fixed way, and still plug in toWebRequest.Create
, I think you'll have to derive a new class fromWebRequest
, have anHttpWebRequest
as a member, and forward methods to yourHttpWebRequest
. Stability. What an interesting concept. -- Chris MaunderYou are right, I can add to the headers, and set the
Method
property. That is what I ended up doing. And it is a simpler design this way, so everything worked out for the best (so far at least). However, I found it strange that MS would create a non-sealed class that could not be inherited. A sealed class is faster after all...and I would think that they would want the framework to be as efficient as possible. So I figured it may just be that there is some technique that I am unaware of - I'm still a C# newbie in many ways :((. I'd still like to be able to inherit from it if the design evolves that way. Decorating objects (your 2nd suggestion) is usually a last resort for me, as it is always so repetitive. [Since you asked, I am writing a 100% managed C# component for working with the client side of WebDAV. (Yes, I know that I can use MSXML). WebDAV is an extension to HTML, so it shares all the same basic characteristics. Extension equates well to inheritance, so that's why my first thought was to inherit fromHttpWebRequest
.] -
You are right, I can add to the headers, and set the
Method
property. That is what I ended up doing. And it is a simpler design this way, so everything worked out for the best (so far at least). However, I found it strange that MS would create a non-sealed class that could not be inherited. A sealed class is faster after all...and I would think that they would want the framework to be as efficient as possible. So I figured it may just be that there is some technique that I am unaware of - I'm still a C# newbie in many ways :((. I'd still like to be able to inherit from it if the design evolves that way. Decorating objects (your 2nd suggestion) is usually a last resort for me, as it is always so repetitive. [Since you asked, I am writing a 100% managed C# component for working with the client side of WebDAV. (Yes, I know that I can use MSXML). WebDAV is an extension to HTML, so it shares all the same basic characteristics. Extension equates well to inheritance, so that's why my first thought was to inherit fromHttpWebRequest
.]The reason it cannot be inherited is because HttpWebRequest does not have a public default constructor that takes zero arguments. If you use code like this (note: not safe, you must provide proper arguments for
base(..,..)
) it will compile and work:public class SafeHttpWebRequest : HttpWebRequest
{
public SafeHttpWebRequest() : base(null, new StreamingContext()) { /* do something */ }
}