【问题标题】:How to host a simple ASP.NET web interface in a Windows Services如何在 Windows 服务中托管一个简单的 ASP.NET Web 界面
【发布时间】:2011-03-15 05:35:19
【问题描述】:

对于在 Windows 和 .NET 操作系统上运行的简单设备,我们需要创建一个简单的配置 Web 界面来控制它。就像你的路由器的配置页面一样,没有比这更复杂的了。

应避免安装 IIS 或任何其他 Web 服务器,我们需要的是在基本的 windows XP 安装 + .NET 上的 windows 服务中的自支持进程。

单声道兼容性是一个优点。

谢谢一百万

【问题讨论】:

    标签: c# .net asp.net http mono


    【解决方案1】:

    实际上最简单的方法是使用内置的 WCF 东西(.Net 3.5)...为此,您为“WCF”服务创建一个接口,其中包含一个或多个返回 Stream 的方法:

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "/{*arguments}", Method="GET", BodyStyle=WebMessageBodyStyle.Bare)]
        Stream Get(string arguments);
    }
    

    您可以定义多个方法和参数并让 WFC 完成工作,或者如上例所示,将所有内容都放入一个方法中。生成的实现可以访问完整的 Uri 和查询参数,如下所示:

    public class ServiceType : IService
    {
        public Stream Get(string arguments)
        {
            UriTemplateMatch uriInfo = WebOperationContext.Current.IncomingRequest.UriTemplateMatch;
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
    
            MemoryStream rawResponse = new MemoryStream();
            TextWriter response = new StreamWriter(rawResponse, Encoding.UTF8);
            response.Write("<html><head><title>Hello</title></head><body>");
            response.Write("<b>Path</b>: {0}<br/>", arguments);
            response.Write("<b>RequestUri</b>: {0}<br/>", uriInfo.RequestUri);
            response.Write("<b>QueryParameters</b>: {0}<br/>", uriInfo.QueryParameters.ToString());
            response.Write("</body></html>");
            response.Flush();
    
            rawResponse.Position = 0;
            return rawResponse;
        }
    }
    

    现在您要做的就是启动 WCF web/http 自托管 ...

    static void Main()
    {
        Uri baseAddress = new Uri("http://localhost:8000/");
        WebServiceHost svcHost = new WebServiceHost(typeof(ServiceType));
    
        ServiceEndpoint svcEndpoint = svcHost.AddServiceEndpoint(typeof(IService),
          new WebHttpBinding(), baseAddress);
        svcEndpoint.Behaviors.Add(new WebHttpBehavior());
    
        svcHost.Open();
        Console.WriteLine("Press enter to quit...");
        Console.ReadLine();
    
        svcHost.Close();
    }
    

    注意:要让上述示例在 Vista/Win7 上运行,您需要使用以下命令行授予权限:

    netsh http add urlacl url=http://+:8000/ user=DOMAIN\USER
    

    【讨论】:

      【解决方案2】:

      您可以在自己的进程中托管 ASP.Net 运行时。 Rick Strahl 有一篇关于它的旧文章,名为“Using the ASP.Net Runtime for extending desktop applications with dynamic HTML Scripts”。

      它应该适用于 Windows XP、.Net 2.0 及更高版本。如果您将此与@csharptest.net 答案中的 WCF 代码结合使用,您应该能够使用 ASP.Net 页面的强大功能并为其提供端点。

      【讨论】:

        【解决方案3】:

        如果你想要一个简单的解决方案,我建议你试试Kayak

        来自网站:

        Kayak HTTP 是一个简单的网络服务器。它 监听连接,创建一个 请求的内存表示, 并允许您轻松生成 回应。它可以在任何 C# 中使用 程序。您的代码将 Kayak 加载到 它的进程空间——而不是相反 周围!

        它也适用于单声道。试一试! :)

        更新

        你也可以试试aspnet serve

        【讨论】:

        • 皮划艇是我的解决方案。
        【解决方案4】:

        您可以将UtilDev Cassini 与您的Windows 服务一起使用。它基于内置于 Visual Studio 的原始 MS casini,免费且可再分发。

        【讨论】:

          【解决方案5】:

          如果您使用的是 Windows 7,则可以使用 IIS7 的 Hostable Web Core 功能在您的服务中托管 IIS 的子集,而无需安装 IIS7 本身。

          您正在寻找的是嵌入式 Web 服务器。虽然您可以自己编写,但我建议您查看C# WebServer,这是一个用 C# 编写的嵌入式 Web 服务器。

          【讨论】:

            【解决方案6】:

            考虑Cassini 构建或新的Hostable Web Core HWC 之一

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-12-16
              • 1970-01-01
              • 2011-08-10
              • 1970-01-01
              • 2016-01-21
              • 1970-01-01
              相关资源
              最近更新 更多