I wrote a Windows service using HttpListener. The service needs to send responses for every request which currently is done using HttpListenerResponse.
Unfortunately, a temporary file (with the response as content) is created and left behind under %userprofile%\AppData\Local\Temp for every response.
I'm basically using Microsofts example code from here https://msdn.microsoft.com/en-us/library/system.net.httplistenerresponse(v=vs.110).aspx which shows the same behavior.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
string[] pre = { "http://localhost:8080/" };
SimpleListenerExample(pre);
}
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();
}
}
}
I want to write a long running Windows Service and believe these temporary files could be a problem after some time.
How can I send responses without temp. file creation?
Ouch, I figured it out. There's no problem with the code. The temp files were created by the tool I'm Only Resting (http://www.swensensoftware.com/im-only-resting) I used to send POST responses to test my service.
Sorry for the confusion and thanks for your help.