我们的应用程序服务器体系结构经过设置,以便每个服务调用都经过自定义构建的 WCF 服务路由器 - 一个使用请求消息标头中嵌入的信息将传入请求分发到适当服务的服务。
我们在使用此 WCF 服务路由器时遇到性能问题(对并发用户进行负载测试时超时)。我们想知道这是否是由于路由器中的错误、我们错误地配置了服务/IIS,或者是否是预期的情况 - 每个通过单个服务的调用听起来都像是一个潜在的瓶颈。
如果没有路由,我们可以在出现超时错误之前处理大约 120 个并发用户,尽管出现超时,但 IIS 仍会继续处理请求。使用路由器,IIS 将停止处理大约 20 个并发用户的请求,并且在负载测试的其余过程中永远不会恢复处理任何请求。
我们的主要问题是,在使用服务路由器时是否会出现这种情况,或者 IIS 是否能够按照我们设置的方式处理此负载?
路由服务如下所示:
/// <summary>
/// Generic service contract which can accept any WCF message.
/// </summary>
[ServiceContract]
public interface IServiceRouter
{
[OperationContract(Action = "*", ReplyAction = "*", AsyncPattern=false)]
Message ProcessMessage(Message requestMessage);
}
实施:
[ServiceBehavior(
InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple,
AddressFilterMode = AddressFilterMode.Any,
ValidateMustUnderstand = false)]
public sealed class ServiceRouter : IServiceRouter
ProcessMessage操作:
public Message ProcessMessage(Message requestMessage)
{
//Figure out the url of the downstream service
string serviceName = requestMessage.Headers.GetHeader<String>(ServiceNameMessageHeader, String.Empty);
string url = String.Format(_destinationUrlFormat, _destinationAppName, _destinationAppVersion, serviceName);
EndpointAddress endpointAddress = new EndpointAddress(url);
using (ChannelFactory<IServiceRouter> factory = new ChannelFactory<IServiceRouter>(_binding, endpointAddress))
{
factory.Endpoint.Behaviors.Add(new MustUnderstandBehavior(false));
IServiceRouter proxy = factory.CreateChannel();
using (proxy as IDisposable)
{
try
{
IClientChannel clientChannel = proxy as IClientChannel;
// invoke service
Message responseMessage = proxy.ProcessMessage(requestMessage);
return responseMessage;
}
catch (Exception ex)
{
// ...
}
}
}
}