为什么静态方法不能用作 ASMX Web 服务中的 Web 服务操作?

发布于 2024-07-30 20:23:28 字数 80 浏览 2 评论 0原文

我只是想了解为什么我不能在 Web 服务中使用静态 Web 方法? 为什么会受到限制?

有人可以给我对此进行简洁的解释吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-08-06 20:23:47

当客户端为您的 Web 服务创建对象时,他们真正创建的是该 Web 服务的代理对象。 该代理对象会为您处理打开和关闭连接之类的事情,以及实际使用 Web 服务的所有开销。 静态方法调用将很难管理。 由于缺乏更好的词,“静态代理”必须执行每次客户端调用静态方法之一时代理对象实例正在执行的所有操作,从而增加大量开销。

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.

奈何桥上唱咆哮 2024-08-06 20:23:43

答案是:因为你不能。

它不是这样设计的。 其设计是,将创建 Web 服务类的实例,然后调用实例方法。

我只能猜测微软为什么这样设计。 要确定,你必须问他们。 考虑一下:

  1. 允许静态方法没有什么特别的好处。 任何可以使用静态方法执行的操作,也可以使用实例方法执行。
  2. [WebService] 类并不意味着是恰好用作 Web 服务的任意类。 它是您为了公开 Web 服务操作而创建的类。 因此,不需要支持已经存在并且已经具有静态方法的类。
  3. SOAP 标头实现允许您的类包含派生自 SoapHeader 类的类型的实例字段。 该字段将填充传入的 SOAP 标头和/或将包含要返回的 SOAP 标头。 您无法使用静态字段执行此操作,因为它会被每个请求覆盖。

正如我所说,这些都是猜测。 这个问题的正确答案是,“你不能,因为微软就是这样设计的。如果你想知道他们为什么这样设计,你需要问他们”。


FWIW,我刚刚检查过,WCF 似乎也不允许静态方法作为操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文