.NET WSE 客户端存根线程安全吗?
.NET WSE 从 WSDL 生成的客户端存根是否是线程安全的?
当然,“线程安全”不一定是严格定义的术语,因此我至少对以下内容感兴趣:
同一存根类的不同实例是否可以由不同线程同时访问,并且与单线程执行相同的有效行为?
同一存根类的单个实例是否可以被不同线程同时访问,并且与单线程执行中以某种任意方式交错的相同调用具有相同的有效行为?
Are client stubs generated from WSDL by .NET WSE thread-safe?
Of course, "thread-safe" isn't necessary a rigorously defined term, so I'm at least interested in the following:
Are different instances of the same stub class accessible concurrently by different threads, with the same effective behavior as single-threaded execution?
Is a single instance of the same stub class accessible concurrently by different threads, with the same effective behavior as the same calls interleaved in some arbitrary way in single-threaded execution?
You may also wish to use the terminology described here (and originating here) to discuss this more precisely.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,对于线程安全吗,简短的回答是肯定的。原因是服务的服务器端在线程功能方面比客户端连接有更多的发言权。客户端只是一个代理,以服务器可以理解的方式布置请求。它什么都不知道。它是一个基本类,除了与服务器的连接之外没有外部访问。因此,只要服务器允许多个连接就可以了。因此没有资源争用(除了服务器能够处理您的所有请求)。
在客户端,您可以让多个线程使用相同的类但不同的实例。这可能是首选方案,以便每个事务都可以是原子的。而共享实例则必须围绕类本身的访问来处理自己的线程锁定,否则您可能会遇到代码内部资源的竞争条件。
还可以进行异步调用。 wsdl 工具生成的存根将创建 begin、end 调用方法,以便您可以提供回调方法来有效地允许您提交请求并继续您的代码,而无需等待回复。这可能最适合您的第二个单实例场景。
然而,这也取决于服务器组件的编码方式。如果它是一个网络服务,您应该能够同时提交多个请求。但是,如果它是基于套接字的服务,您可能需要在端进行一些额外的编码,以便处理多个传入连接,甚至创建套接字等。
简而言之,是的,在服务器端能够处理多个并发连接的限制内,不同实例的行为与单线程执行相同。
至于单个实例,如果您使用回调过程,那么您可能能够得到您想要的东西而不会太头疼。然而它也受到服务器端代码的限制。
我之所以声明服务器限制,是因为有些公司会构建 Web 服务来限制来自出站主机的连接数量,因此您的吞吐量会受到限制。因此,您可以使用的有效线程数量将会减少或被淘汰。
Well, for the short answer of is it thread safe, is yes. The reason is that the server side of the service will have more to say then the client connection as to threading capabilities. The client is just a proxy that lays out the request in a fashion that the server can understand. It knows nothing. It is a basic class, no outside access other than the connection to a server. So as long as the server allows multiple connections you would be fine. Thus no resource contention (Except for the server being able to handle all your requests).
On the client side you can have multiple threads use the same class but different instances. This would probably be the preferred scenario so that each transaction can be atomic. Whereas the shared instance you would have to handle your own thread locking around the access of the class itself otherwise you may run into a race condition on the resource internal to your code.
There is also the ability to have a asynchronous call. The stubs generated by wsdl tool will create the begin, end invoke methods so that you can provide a callback method to effectively allow you to submit your request and continue your code without waiting for a reply. This would probably be the best for your second scenario with the single instance.
However it also depends on how the server component is coded. If it's a webservice you should be able to submit multiple requests simultaneously. However if it's a socket based service you may need to do some additional coding on your end in order to handle multiple incoming connections or even to create sockets for example.
So in short yes the different instances behave the same as single threaded execution within the limits of the server side being able to handle multiple concurrent connections.
As for the single instance if you use a callback process, which is provided you may be able to get what you are after without too much headache. However it is also restricted to the limits of the server side code.
The reason I state the server limits is that there are companies that will build webservices that restrict the number of connections coming from outbound hosts so your throughput is limited by this. Thus the number of effective threads you could use would be reduced or made obsolete.