How to host a simple ASP.NET web interface in Windows Services

For a simple device running on Windows and .NET, we need to create a simple web-based configuration interface to manage it. Like your router configuration page, nothing is more complicated.

Avoid installing IIS or any other web server, we need a self-service process in the Windows service on a basic installation of Windows XP + .NET.

Monocompatibility is a plus.

Thanks a million

+5
source share
6 answers

- 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 self-host...

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
+10

, Kayak.

:

- -. , , . # . Kayak - !

. !:)

Update

aspnet service

+3

ASP.Net . Rick Strahl " ASP.Net HTML-".

Windows XP,.Net 2.0 . WCF @csharptest.net, ASP.Net .

+3

UtilDev Cassini Windows. MS-, .

+1

Windows 7, IIS7 Hostable Web Core IIS IIS7.

What you are looking for is an embedded web server. Although you can write your own, I suggest you check out C # WebServer , an embedded web server written in C #.

+1
source

Consider one of Cassini or the new Hostable Web Core HWC

+1
source

All Articles