简体   繁体   English

WCF最佳性能设置

[英]WCF Optimal performance settings

Hi i am creating an API using WCF. 您好我正在使用WCF创建API。 My question can be broken down into two seperate ones 我的问题可以分为两个单独的问题

1) I have quite a few calls, for instance i have calls relating to the customer, products, orders, employees. 1)我有很多电话,例如我打电话给客户,产品,订单,员工。

My question is should all this go into one public interface class eg 我的问题是所有这些应该进入一个公共接口类,例如

public interface IRestService
public class RestService : IRestService

Or should I have one for each call eg 或者我应该为每个电话有一个,例如

public interface ICustomer
public class Customer : ICustomer

public interface IProducts
public class Products: IProducts

2) If you have an API which will be accessed by tens of thousands of users and thousands of users concurrently, how would you set up, what will your web config settings be for instance in terms of throttling. 2)如果你有一个API,将由成千上万的用户和数千个用户同时访问,你将如何设置,你的web配置设置将在例如限制方面。 Also what setting would you give your InstanceContextMode , or ConcurrencyMode . 您还将为InstanceContextModeConcurrencyMode提供什么设置。 Finally what type of binding would it be, bearing in mind websites and mobile phones can access the api. 最后是什么类型的绑定,记住网站和手机可以访问api。

  1. If you really have few operations, single service can be used. 如果您的操作很少,可以使用单一服务。 Generally services are logical collection of related operations but the number of operations should be limited. 通常,服务是相关操作的逻辑集合,但操作的数量应该是有限的。 Usually if your service have more than 20 operations you should think about refactoring. 通常,如果您的服务有超过20个操作,您应该考虑重构。

  2. Do you plan to use REST service? 您打算使用REST服务吗? I guess you do because of your first interface example. 我猜你是因为你的第一个界面示例。 In such case you need WebHttpBinding (or similar custom binding) with default InstanceContextMode ( PerCall ) and ConcurrencyMode ( Single ) values. 在这种情况下,您需要具有默认InstanceContextModePerCall )和ConcurrencyModeSingle )值的WebHttpBinding (或类似的自定义绑定)。 Only other meaningful combination for REST service is InstanceContextMode.Single and ConcurrencyMode.Multiple but it will create your service as singleton which can have impact on your service implementation. 对于REST服务,只有其他有意义的组合是InstanceContextMode.SingleConcurrencyMode.Multiple但它会将您的服务创建为单例,这可能会对您的服务实现产生影响。 My rule of thumb: Don't use singleton service unless you really need it. 我的经验法则: 除非你真的需要,否则不要使用单件服务。

  3. Throttling configuration is dependend on your service implementation and on performance of your servers. 限制配置取决于您的服务实现和服务器的性能。 What does thousands concurrent users really mean for you? 成千上万的并发用户对您意味着什么? Processing thousands of requests concurrently requires good server cluster with load balancer or hosting in Azure (cloud). 同时处理数千个请求需要具有负载均衡器或Azure(云)托管的良好服务器群集。 All is also dependend on the speed of processing (operation implementation) and size of messages. 所有这些还取决于处理速度(操作实现)和消息大小。 The correct setting for MaxConcurrentInstances and MaxConcurrentCalls (should be same for PerCall instancing) should be revealed by performance testing. 应通过性能测试揭示MaxConcurrentInstancesMaxConcurrentCalls的正确设置(对于PerCall实例应该是相同的)。 Default values for service throttling have changed in WCF 4. 服务限制的默认值在WCF 4中已更改。

For the sake of good practice, I would break up the API into separate interfaces so you have the option of splitting them into separate implementations in the future. 为了良好的实践,我会将API分解为单独的接口,因此您可以选择将它们拆分为未来的单独实现。 You can still have just one service class implement all of the interfaces, like this: 您仍然可以只有一个服务类实现所有接口,如下所示:

public class RestService : ICustomer, IProducts, IOrders

However, it sounds as if you'd probably want to make them separate implementations anyway. 但是,听起来好像你可能想要让它们单独实现。

In terms of concurrency settings, ask yourself what resources need to be used on each call. 在并发设置方面,请问自己每次调用需要使用哪些资源。 If your service class's constructor can be written without any lengthy startup, then use PerCall. 如果您的服务类的构造函数可以在没有任何冗长启动的情况下编写,那么请使用PerCall。 If you need to initialize expensive resources, then I'd recommend InstanceContextMode.Single with ConcurrencytMode.Multiple and make sure you write thread-safe code. 如果您需要初始化昂贵的资源,那么我建议使用ConcurrencytMode.Multiple的InstanceContextMode.Single,并确保编写线程安全的代码。 Eg: make sure you lock() on any class properties or other shared resources before you use them. 例如:在使用它们之前,请确保在任何类属性或其他共享资源上使用lock()。

Database connections would not count as "expensive to initialize", though, because ADO will do connection pooling for you and eliminate that overhead. 但是,数据库连接不会算作“初始化成本很高”,因为ADO会为您进行连接池并消除这种开销。

Your throttling settings will be revealed by testing, as Ladislav mentions. 正如Ladislav提到的那样,您的限制设置将通过测试显示出来。 You'd want to stress-test your service and use the results to get an idea of how many machines you'd need to service your anticipated load. 您需要对服务进行压力测试并使用结果来了解您需要为预期负载提供多少台机器。 Then you'll need a dedicated load balancer to route requests as either round-robin, or something that checks the health of each server. 然后,您需要一个专用的负载均衡器来将请求路由为循环,或者检查每个服务器运行状况的东西。 Load balancers can be set up to GET a "systemhealth.asp" page and check the results. 可以将负载平衡器设置为GET“systemhealth.asp”页面并检查结果。 If you return an "OK" then that machine stays in the pool, or can be temporarily removed from the pool if it times out or returns any other status. 如果您返回“确定”,则该计算机将保留在池中,或者如果超时或返回任何其他状态,则可以暂时从池中删除。

Your binding would need to be WebHTTPBinding for REST. 您的绑定需要是REST的WebHTTPBinding。 BasicHTTPBinding is meant for SOAP interfaces and doesn't support [WebGet], for example. BasicHTTPBinding用于SOAP接口,例如不支持[WebGet]。

If it doesn't have to be a REST service, then you can get a bit more performance by using NetTcpBinding. 如果它不必是REST服务,那么使用NetTcpBinding可以获得更高的性能。

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

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