本文整理汇总了C#中OwinContext.Write方法的典型用法代码示例。如果您正苦于以下问题:C# OwinContext.Write方法的具体用法?C# OwinContext.Write怎么用?C# OwinContext.Write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OwinContext
的用法示例。
在下文中一共展示了OwinContext.Write方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MaxConcurrentRequests
/// <summary>
/// Limits the number of concurrent requests that can be handled used by the subsequent stages in the owin pipeline.
/// </summary>
/// <param name="getMaxConcurrentRequests">A delegate to retrieve the maximum number of concurrent requests. Allows you
/// to supply different values at runtime. Use 0 or a negative number to specify unlimited number of concurrent requests.</param>
/// <returns>An OWIN middleware delegate.</returns>
/// <exception cref="System.ArgumentNullException">getMaxConcurrentRequests</exception>
public static MidFunc MaxConcurrentRequests(Func<RequestContext, int> getMaxConcurrentRequests)
{
getMaxConcurrentRequests.MustNotNull("getMaxConcurrentRequests");
var logger = LogProvider.GetLogger("LimitsMiddleware.MaxConcurrentRequests");
int concurrentRequestCounter = 0;
return
next =>
async env =>
{
var owinRequest = new OwinRequest(env);
var limitsRequestContext = new RequestContext(owinRequest);
int maxConcurrentRequests = getMaxConcurrentRequests(limitsRequestContext);
if (maxConcurrentRequests <= 0)
{
maxConcurrentRequests = int.MaxValue;
}
try
{
int concurrentRequests = Interlocked.Increment(ref concurrentRequestCounter);
logger.Debug("Concurrent request {0}/{1}.".FormatWith(concurrentRequests, maxConcurrentRequests));
if (concurrentRequests > maxConcurrentRequests)
{
logger.Info("Limit ({0}). Request rejected."
.FormatWith(maxConcurrentRequests, concurrentRequests));
IOwinResponse response = new OwinContext(env).Response;
response.StatusCode = 503;
response.ReasonPhrase = "Service Unavailable";
response.Write(response.ReasonPhrase);
return;
}
await next(env);
}
finally
{
int concurrentRequests = Interlocked.Decrement(ref concurrentRequestCounter);
logger.Debug("Concurrent request {0}/{1}.".FormatWith(concurrentRequests, maxConcurrentRequests));
}
};
}