c# – 使用.Net HttpListener时更改HTTP.sys内核队列限制?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用.Net HttpListener时更改HTTP.sys内核队列限制?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2445字,纯文字阅读大概需要4分钟。
内容图文
我有一个应用程序,它使用.Net 4.0中的HttpListener类来为http请求提供服务.
在负载下我注意到我在日志中报告了503 – QueueFull – 错误.搜索此错误表示当超过http.sys将排队的最大请求数时会发生此错误.
默认队列长度为1000.如果您使用的是IIS,则可以通过应用程序池上的“高级设置”中的“队列长度”参数进行调整.
如果你不使用IIS有什么方法可以调整这个值?或者这个参数的控件是隐藏在HttpListener类中的,而不是暴露给开发人员?
解决方法:
似乎HttpListener不允许直接更改HttpServerQueueLengthProperty属性.和by default this property is set to 1000.
但是你可以尝试在HttpListener启动后手动设置它.它是黑客,因为它使用HttpListener的内部属性RequestQueueHandle,所以使用它是危险的.
哈克:
using System;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Network.Utils
{
public static class HttpApi
{
public unsafe static void SetRequestQueueLength(HttpListener listener, long len)
{
var listenerType = typeof (HttpListener);
var requestQueueHandleProperty = listenerType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).First(p => p.Name == "RequestQueueHandle");
var requestQueueHandle = (CriticalHandle)requestQueueHandleProperty.GetValue(listener);
var result = HttpSetRequestQueueProperty(requestQueueHandle, HTTP_SERVER_PROPERTY.HttpServerQueueLengthProperty, new IntPtr((void*)&len), (uint)Marshal.SizeOf(len), 0, IntPtr.Zero);
if (result != 0)
{
throw new HttpListenerException((int) result);
}
}
internal enum HTTP_SERVER_PROPERTY
{
HttpServerAuthenticationProperty,
HttpServerLoggingProperty,
HttpServerQosProperty,
HttpServerTimeoutsProperty,
HttpServerQueueLengthProperty,
HttpServerStateProperty,
HttpServer503VerbosityProperty,
HttpServerBindingProperty,
HttpServerExtendedAuthenticationProperty,
HttpServerListenEndpointProperty,
HttpServerChannelBindProperty,
HttpServerProtectionLevelProperty,
}
[DllImport("httpapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern uint HttpSetRequestQueueProperty(
CriticalHandle requestQueueHandle,
HTTP_SERVER_PROPERTY serverProperty,
IntPtr pPropertyInfo,
uint propertyInfoLength,
uint reserved,
IntPtr pReserved);
}
}
用法示例:
using (var listener = new HttpListener())
{
listener.Prefixes.Add("http://*:8080/your/service/");
listener.Start();
Network.Utils.HttpApi.SetRequestQueueLength(listener, 5000);
// ...
}
应用程序启动后,您可以通过运行以下命令来检查队列长度:
netsh http show servicestate
检查流程的“最大请求数”属性.现在它必须等于5000.
内容总结
以上是互联网集市为您收集整理的c# – 使用.Net HttpListener时更改HTTP.sys内核队列限制?全部内容,希望文章能够帮你解决c# – 使用.Net HttpListener时更改HTTP.sys内核队列限制?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。