Search code examples
c#.netwcfwcf-bindingself-hosting

Consume a HTTP POST from a self hosted service


I have a self hosted service that needs to listen for upload notifications coming from a BITS server (they are a simple HTTP POST request with custom headers). If I was not self hosting my service and was using IIS I would just make a ASPX page and I could handle the incoming requests, but I am using self hosted WCF and I can not switch to IIS.

I looked in to using WebInvokeAttribute, however that appears to only be for sending JSON or XML as a reply and I need to follow the protocol spec. Also I did not see a way of pulling out the custom headers.

The next thing I looked in to was HttpListener and it appears to do what I need, however I did not see if there is a way to configure it via my app.config file like normal WCF endpoints.

Do I just add the address to my applicationSettings section or is there a better way to achieve what I am trying to do?


Solution

  • I ended up just using the Properties class and storing the url there.

    //This is run on it's own thread
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add(Properties.Settings.Default.BitsReplierAddress);
    listener.Start();
    
    while (_running)
    {
        // Note: The GetContext method blocks while waiting for a request. 
        // Could be done with BeginGetContext but I was having trouble 
        // cleanly shutting down
        HttpListenerContext context = listener.GetContext();
        HttpListenerRequest request = context.Request;
    
        var requestUrl = request.Headers["BITS-Original-Request-URL"];
        var requestDatafileName = request.Headers["BITS-Request-DataFile-Name"];
    
        //(Snip...) Deal with the file that was uploaded
    }
    
    listener.Stop();