简体   繁体   English

在 WCF C# 应用程序中模拟并发或多线程

[英]Emulating concurrency or multi threading in WCF C# application

I am developing an Service-Oriented based application for Hotel Reservation System using WCF architecture.我正在使用 WCF 架构为酒店预订系统开发基于服务的应用程序。 It has following 3 three components: 1. WCF Service that has Service and Data contracts for invoking moudules of room reservation.它具有以下 3 个三个组件: 1. WCF 服务,具有用于调用房间预订模块的服务和数据合同。 2. WCF Host that keeps log of service access by client 3. WCF client that has UI for Hotel Reservation. 2. WCF 保存客户端服务访问日志的主机 3. WCF 客户端,具有用于酒店预订的 UI。

I am looking for guidelines to implement concurrency in my application having following features: 1. Only one request shall be entertained at the time.我正在寻找在我的应用程序中实现并发性的指南,它具有以下特性: 1. 当时只能处理一个请求。 2. If more than one requests are received, then these should be synchronized using locks/mutex/semaphores. 2. 如果接收到多个请求,则应使用锁/互斥/信号量同步这些请求。

Coding guidelines shall be helpful.编码指南应该会有所帮助。

I think this is the allocation issue of the external resources, such as the database access, the I/O for on the single file.我认为这是外部资源的分配问题,例如数据库访问,单个文件的 I/O。 WCF supports transaction and transmitting it on the client-side. WCF 支持在客户端进行交易和传输。 We are capable of locking the access to the database by using transaction when another client attempt to access it.当另一个客户端尝试访问它时,我们能够通过使用事务来锁定对数据库的访问。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transactions-overview https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transactions-overview
Moreover, we can also use the shared lock which locks the service instance, occupying all the resource lest the other instance access the service, please refer to the below code.此外,我们还可以使用共享锁来锁定服务实例,占用所有资源以免其他实例访问服务,请参考以下代码。

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
 ConcurrencyMode = ConcurrencyMode.Multiple)]
class MyService : IMyContract
{
 public void MyMethod()
 {
 lock(typeof(MyService))
 {
 ...
 MyResource.DoWork();
 ...
 }
 }
}
static class MyResource
{
 public static void DoWork()
 {
 lock(typeof(MyService))
 {
 ...
 }
 }
}

Please refer to the below link, Chaper8(concurrency management), resources and services, deadlock avoidance.请参考以下链接,Chaper8(并发管理),资源和服务,避免死锁。
https://ashishmit99.files.wordpress.com/2013/01/oreilly-programming-wcf-services-3rd-edition-aug-2010.pdf https://ashishmit99.files.wordpress.com/2013/01/oreilly-programming-wcf-services-3rd-edition-aug-2010.Z4371094BA41917410EE0
Here is an official document relates to Concurrency mode, aiming to solve access conflict from the multiple users.这是一个关于并发模式的官方文档,旨在解决多用户的访问冲突。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/sessions-instancing-and-concurrency https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/sessions-instancing-and-concurrency

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM