Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I just wanna learn why I can't static web methods in web services ? Why is it restricted ?

Can some body give me concise explanation of this.

like image 695
Tarik Avatar asked Aug 11 '09 22:08

Tarik


People also ask

What is Asmx file web service?

ASMX provides the ability to build web services that send messages using the Simple Object Access Protocol (SOAP). SOAP is a platform-independent and language-independent protocol for building and accessing web services.

How do I add Asmx to my web service?

Please refer to the document to add Web Service (ASMX) file in VS 2022: create ASP.NET Web Application(. NET Framework) project > right-click your project > Add > New Item… > search Web Service (ASMX) > click on Add .

How use Asmx webservice in asp net?

Step (1) : Select File -> New -> Web Site in Visual Studio, and then select ASP.NET Web Service. Step (2) : A web service file called Service. asmx and its code behind file, Service. cs is created in the App_Code directory of the project.


2 Answers

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:

  1. 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.
  2. 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.
  3. 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.

like image 145
John Saunders Avatar answered Nov 01 '22 08:11

John Saunders


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.

like image 42
Joshua Hudson Avatar answered Nov 01 '22 08:11

Joshua Hudson