为什么每次调用 webmethod 时都会调用我的 WebService 构造函数?

发布于 2024-07-18 20:29:59 字数 547 浏览 3 评论 0原文

每次我调用 webmethod 时,我的 webservice 构造函数都会被调用。 这导致我添加的一些新功能出现一些问题,我无法弄清楚我做错了什么导致了这种情况。 我新建 Web 服务的唯一位置是在 global.asax.cs 的 Application_Start 中,但是如果我删除了 new Web 服务的代码,则每次我调用 Web 方法时它仍然会调用构造函数。

我通过浏览 .asmx 文件并以这种方式调用 webmethod 对此进行了测试,并且我还创建了一个测试控制台应用程序,该应用程序引用了 webservice 并将以这种方式调用方法。 这两者给出相同的结果。

我在 ASP.NET 1.1 中使用 C#。 有任何想法吗?

编辑:
我正在尝试创建一个轮询 Windows 服务的心跳线程。 我试图将结果保存在哈希表中(多个线程轮询多个服务)。 我已经更改了 webmethod(因为它用于轮询 Windows 服务)以从哈希表中获取结果。 我们正在尝试提高此 Webmethod 调用的速度。

My webservice constructor is getting called each time that I call a webmethod. This is causing some problems with some new functionality that I am adding and I cannot figure out what I am doing wrong to cause this. The only place that I am newing the webservice is in global.asax.cs's Application_Start, but if I remove the code to new the webservice, it still calls the constructor each time that I call a webmethod.

I have tested this by browsing to the .asmx file and calling the webmethod's that way and I have also created a test console application that has a reference to the webservice and will call the methods that way. Both of these give the same results.

I am using c# in ASP.NET 1.1. Any ideas?

Edit:

I am trying to create a heartbeat thread that polls a windows service. I am attempting to save the result in a hash table (multiple threads polling multiple services). I have changed the webmethod (as it used to poll the windows service) to get the results from the hashtable. We are trying to increase the speed of this webmethod call.

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

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

发布评论

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

评论(3

和影子一齐双人舞 2024-07-25 20:29:59

每当您调用 Web 方法时,都会创建 Web 服务的新实例 - 这是预期的行为。 Web 服务是无状态的,这意味着服务实例在 Web 方法调用之间不会保持活动状态,因此您无法在 Web 服务的成员(字段、属性)中存储任何状态。

如果要在 Web 方法调用之间保存某些值/状态,则必须使用 ASP.NET 会话或数据库来实现此目的。

有关详细信息,请参阅此问题:这应该这样工作吗?

Whenever you call a web method, a new instance of the web service is created - this is the intended behaviour. Web services are stateless, which means that an instance of the service is not kept alive between web method calls, and therefore you cannot store any state in members (fields, properties) of the web service.

If you want to save some values/state between web method calls, you have to use the ASP.NET session or a database for that purpose.

See this question for details: Is this supposed to work this way?

七婞 2024-07-25 20:29:59

呃……这恐怕是设计使然。 您能描述一下您遇到的问题以及您想要实现的目标吗?我认为您需要在这里稍微重构一下您的设计。

Err... this is kind of by design I'm afraid. Can you describe the problems you are getting and what you are trying to acheive, I think you need to refactor your design a little here.

极致的悲 2024-07-25 20:29:59

您还可以切换到支持有状态服务的WCF

You can also switch to WCF which supports stateful services

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