C# 从3.5迁移到.net 4.5.1后,HttpListener出现问题
我们有一个在端口80上使用HttpListener的项目。 它在.NET3.5的生产和开发中都能完美地工作。 出于依赖性原因,我们必须将其迁移到.NET4.5.1。该项目在开发过程中运行良好,但在我们投入Azure时却无法运行 升级的另一部分是从IIS 7.5迁移到IIS 8 应用程序的主代码。首先是基本侦听器:C# 从3.5迁移到.net 4.5.1后,HttpListener出现问题,c#,asp.net,iis-7.5,iis-8,httplistener,C#,Asp.net,Iis 7.5,Iis 8,Httplistener,我们有一个在端口80上使用HttpListener的项目。 它在.NET3.5的生产和开发中都能完美地工作。 出于依赖性原因,我们必须将其迁移到.NET4.5.1。该项目在开发过程中运行良好,但在我们投入Azure时却无法运行 升级的另一部分是从IIS 7.5迁移到IIS 8 应用程序的主代码。首先是基本侦听器: public abstract class BaseHttpListener { private IPEndPoint HttpEndPoint; private IP
public abstract class BaseHttpListener
{
private IPEndPoint HttpEndPoint;
private IPEndPoint HttpsEndPoint;
private HttpListener Listener;
protected BaseHttpListener(IPEndPoint http, IPEndPoint https = null)
{
HttpEndPoint = http;
HttpsEndPoint = https;
}
public void Start()
{
var thread = new System.Threading.Thread(new System.Threading.ThreadStart(Listen));
thread.Start();
}
private void Listen()
{
// Create a HTTP listener.
Listener = new System.Net.HttpListener();
Listener.Prefixes.Add(String.Format("http://{0}/", HttpEndPoint));
if (HttpsEndPoint != null)
{
Listener.Prefixes.Add(String.Format("https://{0}/", HttpsEndPoint));
}
Listener.Start();
while (Listener.IsListening)
{
var context = Listener.BeginGetContext(ListenerCallback, Listener);
context.AsyncWaitHandle.WaitOne();
}
}
private void ListenerCallback(IAsyncResult ar)
{
var httpListener = ar.AsyncState as HttpListener;
if (httpListener.IsListening == false)
return;
var context = httpListener.EndGetContext(ar);
try
{
Process(context);
}
catch (Exception exception)
{
LogManager.Instance.Fatal(exception, String.Concat("Fatal exception occurs when try to process '", context.Request.Url.ToString(), "'"));
//We should try to send a error to the client
context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
context.Response.StatusDescription = System.Net.HttpStatusCode.InternalServerError.ToString();
context.Response.Close();
}
}
protected abstract void Process(HttpListenerContext context);
public void Stop()
{
Listener.Prefixes.Clear();
Listener.Close();
}
简单索引处理程序:
public class IndexRequestHandler : RequestHandler
{
public IndexRequestHandler(String handledPath)
: base(handledPath)
{
}
public override void HandleRequest(System.Net.HttpListenerContext context)
{
var html = @"<!doctype html>
<html lang='en>'
<head>
<meta charset='utf-8'>
<title>It's ready</title>
</head>
<body>
<h1>It's ready</h1>
</body>
</html>";
var bytes = System.Text.Encoding.UTF8.GetBytes(html);
context.Response.ContentLength64 = bytes.LongLength;
context.Response.ContentType = "text/html;charset=UTF-8";
try
{
context.Response.OutputStream.Write(bytes, 0, bytes.Length);
context.Response.Close();
}
catch (Exception ex)
{
LogManager.Instance.Error(ex, "IndexRequestHandler.HandleRequest");
}
}
}
如果需要的话,我可以给你其他的例子和细节
另一件重要的事情是我运行了一个cmd命令:telnet X.X.X.X 80,它会返回一些东西,但当我在chrome中测试时,我什么也得不到
谢谢你的帮助 有点困惑。您有一个HttpListener,它与IIS有什么关系?我原以为您正在使用HttpListener代替IIS。我也不认为这是与Azure相关的问题。是的,你是对的,可能是IIS的升级没有影响。但我不知道怎么了。。。因为IIS在默认情况下是web服务器,所以我更愿意这么说。我敢肯定的是:它在我的笔记本电脑win 7上工作正常,但在服务器win server 2012 R2上工作正常启动httplistener时是否收到异常?如果是,请复制粘贴在这里。。。。如果IIS使用端口80或443,则无法启动httplistener侦听端口。启动时没有异常。我检查IIS是否不使用端口80和端口443