ConcurrencyMode.Multiple在无状态WCF服务中 [英] ConcurrencyMode.Multiple in stateless WCF services

查看:92
本文介绍了ConcurrencyMode.Multiple在无状态WCF服务中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前有多个WCF服务,它们使用默认的 ServiceBehavior 。由于可伸缩性问题,我们正在考虑应用 ConcurrencyMode = ConcurrencyMode.Multiple 属性以提高吞吐量。我们所有的服务调用都是完全无状态的,例如:

We currently have multiple WCF services which are using the default ServiceBehavior. Due to issues with scalability, we are looking at applying the ConcurrencyMode = ConcurrencyMode.Multiple attribute to improve throughput. All of our service calls are completely stateless, for example:

PersonService.cs:

PersonService.cs:

public class PersonService : IPersonService
{
  public GetPersonResponse GetPerson(GetPersonRequest request)
  {
    GetPersonResponse response = new GetPersonResponse();

    try
    {
      response.Person = Person.GetPerson(request.PersonID);

      return response;
    }
    catch (Exception ex)
    {
      return (GetPersonResponse) response.SetException(ex);
    }
  }
}

Person.cs:

Person.cs:

public static class Person
{
  public static PersonDataContract GetPerson(int personID)
  {
    PersonDataContract pdc = null;

    // load contract from db...
    pdc = Database.Load<PersonDataContract>(personID);

    // Address is another static class in the same pattern as Person
    pdc.Addresses = Address.GetAddressesForPerson(personID);

    return pdc;
  }
}

Person中的所有方法类是静态的以提高性能,而无状态的则确保线程安全。 数据库类也是静态的,但是其方法引用静态变量。

All methods in the Person class are static to help performance, and stateless for thread safety. The Database class is also static, but its methods reference static variables.

在这种情况下,需要使线程安全,以便 ConcurrencyMode.Multiple 不会引起多线程问题?我只考虑 Database 类,但是 Person 类(以及遵循相同模式的所有其他类)却没有

In this context, what needs to be made thread-safe in order for ConcurrencyMode.Multiple to not cause multithreading issues? I'm thinking only the Database class, but does the Person class (and all other classes that follow the same pattern) need to be locked as well?

我知道所有类都应经过防弹处理,以确保最大的安全性,但不幸的是,时间限制不允许这...我们需要尽快交付代码。

I know that all classes should be bulletproofed for maximum safety, but unfortunately time constraints don't allow this... we need to get code delivered ASAP.

推荐答案

如果您使用默认的每次调用激活机制(如果您的服务完全是无状态的,则效果很好),添加 ConcurrencyMode.Multiple 绝对没有意义,因为每个传入请求都将获得自己的服务类实例来处理其请求。这是 InstanceContextMode 的首选和推荐设置。

If you use the default "per call" activation mechanism (which works great if your services are completely stateless), there's absolutely no point in adding the ConcurrencyMode.Multiple since each incoming request will get its own instance of the service class to handle its request. This is the preferred and recommended setting for InstanceContextMode.

在MSDN Magazine上了解有关WCF中实例管理的更多信息:< a href = http://msdn.microsoft.com/en-us/magazine/cc163590.aspx rel = nofollow>发现用于开发WCF应用程序的强大实例管理技术

Read more about instance management in WCF at MSDN Magazine: Discover Mighty Instance Management Techniques For Developing WCF Apps

只有当您拥有单例WCF服务时,才可以使用 ConcurrencyMode.Multiple 受益-强烈建议不要这样做,因为它是)对于可伸缩性是一个很大的障碍,并且b)正确编程非常棘手。

The only time when you benefit from using ConcurrencyMode.Multiple is when you have a singleton WCF service - but this is highly discouraged, since it's a) a big hindrance for scalability, and b) extremely tricky to program properly.

我的建议是:尝试更详细地缩小内容确实会导致性能问题。只是跳入 ConcurrencyMode.Multiple 似乎是错误的方法-它非常凌乱,非常费力,大量代码,并且有很多出错的机会......

My recommendation would be: try to narrow down more in detail what really causes the performance problems. Just jumping into ConcurrencyMode.Multiple seems like the wrong approach - it's very messy, very labor-intensive, lots of code, lots of chance of getting it wrong......

这篇关于ConcurrencyMode.Multiple在无状态WCF服务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆