首页 > 解决方案 > 每当尝试在 C# 中启动 HttpListener 时出现 HttpListenerException

问题描述

今天我开始更多地研究HttpListener它是什么,所以如果我对这个或它如何工作的知识不是很广泛,我很抱歉。但是当尝试使用该Start()方法启动我时,HttpListener我得到了这个异常:

Can't start the agent to listen transactionSystem.Net.HttpListenerException (32): The process cannot access the file because it is being used by another process.

而且这个例外也与前缀本身有关。

这是我正在使用的代码,这是我第三次不得不重做它,但我仍然遇到同样的异常。

static void ListenTraces()
        {
            try
            {
                var httpListener = new HttpListener();

                httpListener.Prefixes.Add("https://discord.com/");
                try
                {
                    httpListener.Start(); //this line is where the exception happens
                }
                catch (HttpListenerException hlex)
                {
                    Console.WriteLine("Can't start the agent to listen transaction" + hlex);
                    return;
                }
                Console.WriteLine("Now ready to receive traces...");
                while (true)
                {
                    var context = httpListener.GetContext(); 

                    Console.WriteLine(context.Response);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

感谢所有帮助:)

标签: c#httpexceptionproxyhttplistener

解决方案


可能已经在该端口上监听了其他东西(您是否正在运行任何其他开发服务器?)

尝试通过更改注册表值将代理端口配置为其他端口。

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MsDepSvc\Parameters\ListenUrl

默认情况下,此值应为http://+:80/MSDEPLOYAGENTSERVICE/. 您可以将 80 更改为例如 81。然后重新启动您的服务。


推荐阅读