Why are Static Methods not Usable as Web Service Operations in ASMX Web Services?

The answer is: because you can't. It's not designed that way. The design is that an instance of the web service class will be created, and then an instance method will be called.

I can only guess why Microsoft designed it that way. To know for sure, you'd have to ask them. Consider: There's no particular benefit to permitting static methods.

Anything you can do with a static method, you can also do with an instance method. A WebService class is not meant to be some arbitrary class that happens to be used as a web service. It's meant to be a class that you created for the purpose of exposing web service operations.As such, there is no need to support classes that already exist and already have static methods.

The SOAP Header implementation permits your class to contain an instance field of a type deriving from the SoapHeader class. This field will be filled with an incoming SOAP header and/or will contain the SOAP Header to be returned. You could not do this with a static field, as it would be overwritten with each request.

As I said, these are all guesses. The correct answer to the question is, "you can't because that's how Microsoft designed it. If you want to know why they designed it that way, you need to ask them".

FWIW, I just checked, and it does not appear that WCF permits static methods to be operations either.

1 Good answer! :-) – REA_ANDREW Aug 11 '09 at 22:45 1 But I am asking why? They wouldn't simply say "Well guys we are not gonna design this thing in that way, instead, it is gonna be designed in this way.

Blah blah". What I am asking is the reason behind this. – Braveyard Aug 11 '09 at 23:30 Thanks for clean and elaborate answer.

– Braveyard Aug 12 '09 at 4:31.

When a client creates an object for your web service, what they are really creating is a proxy object to that web service. This proxy object handles things like opening and closing your connections for you as well as all the overhead of actually working with the web service. A static method call would be difficult to manage.

The "static proxy" for lack of a better word would have to do all of things that the instance of the proxy object is doing each and every time a client called one of the static methods, thus adding massive overhead.

I'm pretty sure he's talking about the server side, not the client side. – John Saunders Aug 11 '09 at 22:47 My answer answers his question in way I think helps explain why static methods on the server side would not work. Just saying that web services are designed so they can't does not say why they were designed that way in the first place.

– Joshua Hudson Aug 11 '09 at 22:49 But that's not the reason. What happens on the server has little to do with what happens on the client. There is no correspondence between the client-side proxy and the server-side web service instance.

The server side is not instantiated simply because the client proxy is. Based on this reasoning, I'm going to have to downvote. – John Saunders Aug 11 '09 at 22:53 1 "There is no correspondence between the client-side proxy and the server-side web service instance."

If this is the case how do the two even speak to each other... – Joshua Hudson Aug 11 '09 at 23:01 1 I believe so atarikg. I don't work for Microsoft, nor am I a member of the . NET team, but I would think anything to keep web services simple while avoiding overhead would be a good reason to make this restriction.

– Joshua Hudson Aug 11 '09 at 23:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions