0
저는 초보자 인 C# 개발자입니다. 간단한 win7 서비스를 작성하려고합니다.
서비스는 HTTPListener를 시작하고 들어오는 브라우저 요청을 수신해야합니다. 요청이 수신되면 응답을 반환하고 추가 요청을 수신 대기합니다. 한 번에 두 개 이상의 요청이 없기 때문에 병렬 처리는 필요하지 않습니다.HTTPListener가있는 Win7 서비스가 한 요청 후에 응답을하지 않습니다.
다음 코드를 사용했지만 첫 번째 응답 후에 서비스가 응답하지 않습니다. 어딘가에 루프가 필요할 수도 있지만 API에 익숙하지 않아서 내가하고있는 일에 잘못 될 수도 있습니다.
도움 주셔서 감사합니다.
protected override void OnStart(string[] args)
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:9999/");
listener.Start();
listener.BeginGetContext(new AsyncCallback(OnRequestReceive), listener);
}
private void OnRequestReceive(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
HttpListenerContext context = listener.EndGetContext(result);
HttpListenerResponse response = context.Response;
byte[] buff = {1,2,3};
response.Close(buff, true);
}
고맙습니다! 마술처럼 일해. 한 줄의 코드에 너무 많은 좌절감을 안겨줍니다. :) – user1283002