Program start HTTP server in C #?

I am new to C #, ASP.NET.

Say I have an existing C # project (from the Console Application template, I work with Visual Studio). I want to be able to run a simple HTTP server and serve .aspx pages (or plain text even then I also look for a good ^^ template library), but only if a specific command is provided to the program via the command line interface. (Therefore, the default server is down.)

How could I do this best?

Thanks so much for any help!

Edit: To clarify, I would like all of these features to be embedded in a single project, not related to a website, not related to a website. That is, the project consists of three parts: a command line interface, an optional web interface (HTTP server), and a kernel that waits and responds to requests from one of these two interfaces. This is the current state of an existing project, without a web interface.

+5
source share
4 answers

You can host the ASP.NET runtime inside a console application. Here is an example:

public class SimpleHost : MarshalByRefObject
{
    public void ProcessRequest(string page, string query, TextWriter writer)
    {
        SimpleWorkerRequest swr = new SimpleWorkerRequest(page, query, writer);
        HttpRuntime.ProcessRequest(swr);
    }
}

class Program
{
    static void Main(string[] args)
    {
        // TODO: Check to see if a given argument has been passed on the command-line

        SimpleHost host = (SimpleHost)ApplicationHost.CreateApplicationHost(
            typeof(SimpleHost), "/", Directory.GetCurrentDirectory());

        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://localhost:9999/");
        listener.Start();
        Console.WriteLine("Listening for requests on http://localhost:9999/");

        while (true)
        {
            HttpListenerContext context = listener.GetContext();
            string page = context.Request.Url.LocalPath.Replace("/", "");
            string query = context.Request.Url.Query.Replace("?", "");
            using (var writer = new StreamWriter(context.Response.OutputStream))
            {
                host.ProcessRequest(page, query, writer);
            }
            context.Response.Close();
        }

    }
}

TypeLoadException. bin . , ASP.NET bin. - SimpleHost , GAC.

+9

: http://msdn.microsoft.com/en-us/library/aa529311.aspx

Microsoft.Web.Services3

, :

public partial class WindowsServiceToHostASMXWebService  : ServiceBase
{
    protected override void OnStart(string[] args)
    {
        Uri address = new Uri("soap.tcp://localhost/TestService");
        SoapReceivers.Add(new EndpointReference(address), typeof(Service ));
    }
    protected override void OnStop()
    {
        SoapReceivers.Clear();
    }
}

:

static void Main()
{
    System.ServiceProcess.ServiceBase[] ServicesToRun;
    // Change the following line to match.
    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WindowsServiceToHostASMXWebService() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
+2

"net" , , IIS (- ).

, IIS , ASP.Net. "World Wide Web Publishing" , . , ​​ ( Process.Start()):

net start w3svc

Now you will have problems with this if you are thinking more about just dropping this application onto any old computer. But if the application is designed to control a specific system, everything will be in order.

+2
source

You can use the HttpRuntime class. If you need, I can provide a short demonstration.

0
source

All Articles