HttpListenerを使用してWindowsサービスを作成しました。サービスは、現在HttpListenerResponseを使用して行われているすべての要求に対して応答を送信する必要があります。C#HttpListenerResponseは一時ファイルを残す
残念なことに、(応答としてコンテンツとして)一時ファイルが作成され、応答ごとに%userprofile%\ AppData \ Local \ Tempに残されます。
私は基本的にここからhttps://msdn.microsoft.com/en-us/library/system.net.httplistenerresponse(v=vs.110).aspxのMicrosoftのサンプルコードを使用していますが、これは同じ動作を示しています。
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();
}
}
}
長時間実行しているWindowsサービスを作成しておき、しばらくしてからこれらの一時ファイルが問題になると考えています。
一時的な返信はどうすれば送信できますか?ファイルの作成?
あなたの質問を編集し、実際のコードを含めてください(実際のコードに基づいています)。 –
.netのWebサービスがこれらの一時ファイルを作成するのを簡単に停止することはできません。あなたができることは、24時間ごとのように定期的なチェックを行うサービスに一時ファイルを削除させることです。あなたはPath.GetTempPath()を見て始めることができます。https://msdn.microsoft.com/en-us/library/system.io.path.gettemppath(v=vs.110).aspx] – pidizzle
) - > response.Dispose();を閉じるのではなく、明示的に応答を閉じます。 –