3

我有一个非常简单的 WCF 单向服务(托管在 IIS 中),它使用 basicHttpBinding:

        <service name="OptumRx.Formulary.ServiceLayer.UserRequestService" behaviorConfiguration="UserRequestBehaviors">
            <endpoint
                address=""
                binding="basicHttpBinding" bindingConfiguration="WindowsAuthBinding"
                contract="OptumRx.Formulary.ServiceLayer.IUserRequestService">
            </endpoint>
        </service>
        <client>
            <endpoint address="....."
                  binding="basicHttpBinding" bindingConfiguration="WindowsAuthBinding"
                  contract="OptumRx.Formulary.ServiceLayer.IUserRequestService">
            </endpoint>
        </client>
        <basicHttpBinding>
            <binding name="WindowsAuthBinding" sendTimeout="00:00:15" >
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
        </basicHttpBinding>
        <serviceBehaviors>
            <behavior name="UserRequestBehaviors">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true"/>
                <serviceThrottling maxConcurrentCalls="2" maxConcurrentInstances="2"/>
            </behavior>
        </serviceBehaviors>

代码片段:

    // interface, declarying one way contract
    [OperationContract(IsOneWay = true)]
    void SubmitUserRequest(RequestServiceObject request);

// setting per call instancing and multiple concurrency
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, 
                InstanceContextMode = InstanceContextMode.PerCall)]
public class UserRequestService : IUserRequestService {
}

    // how the client calls the service (client is asp.net page hosted within same site as service)
    ChannelFactory<IUserRequestService> factory = new ChannelFactory<IUserRequestService>("*");
    IUserRequestService proxyUserRequest = factory.CreateChannel();
    try {
      proxyUserRequest.SubmitUserRequest(so);
    } catch {
      // timeout error caught
    } finally {
      ((IClientChannel)proxyUserRequest).Close();
      factory.Close();
    }

该调用会执行一些可能很繁重的数据库/IO 工作,我们可能希望限制最大并发调用。如果我们已经达到了这个限制,是否有办法阻止呼叫排队?

使用上述配置,处理了 2 个调用,第 3 个调用超时(抛出并捕获异常)。在服务跟踪查看器中,我什至看到:

 The system hit the limit set for throttle 'MaxConcurrentCalls'. Limit for this throttle was set to 2. ....

 The HTTP request to '' has exceeded the allotted timeout of 00:00:15. The time allotted to this operation may have been a portion of a longer timeout.

但是一旦前 2 个调用完成,我猜第三条消息仍然存在于侦听器通道中并被执行。

有没有办法在仍然使用单向配置的同时中止呼叫?我希望用户知道服务“忙”并稍后再试......

谢谢

4

0 回答 0