public static async Task SendRequestMethod(IRequestWorker requestWorker) { var listener = new HttpListener(); listener.Prefixes.Add("http://localhost:8888/"); listener.Start(); Console.WriteLine("waiting connections..."); while (listener.IsListening) { var context = await listener.GetContextAsync().ConfigureAwait(false); var request = context.Request; var respones = context.Response; var responseString = await requestWorker.Worker(request); var buffer = Encoding.UTF8.GetBytes(responseString); respones.ContentLength64 = buffer.Length; var output = respones.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); } listener.Stop(); }
static void Main(string[] args) { var registartion = new Registration(); var ninjectKernel = new StandardKernel(registartion); requestWorker = ninjectKernel.Get <IRequestWorker>(); //GetRequestMethod(); SendRequestMethod(requestWorker).Wait(); }
public void RequestWorkerTest() { LogController.GlobalOutputLogCategory = LogController.LogCategoryAll; List <int> whatList1 = new List <int>(); IThreadUtil threadUtil = DependencyService.Get <IThreadUtil>(); IRequestWorker requestWorker1 = DependencyService.Get <IRequestWorkerBuilder>().Create(delegate(Request request) { int targetWhat = whatList1[0]; if (targetWhat != request.what) { Assert.Fail("order illegal"); } if (!threadUtil.IsMainThread()) { Assert.Fail("not work on main thread"); } if ((request.arg1 != 10) || (request.arg2 != 10) || !request.obj.Equals(whatList1)) { Assert.Fail("paramters are not enough"); } whatList1.RemoveAt(0); }); for (int i = 0; i < 10; i++) { whatList1.Add(i); requestWorker1.SendRequest(i, 10, 10, whatList1); } List <int> whatList2 = new List <int>(); IRequestWorker requestWorker2 = DependencyService.Get <IRequestWorkerBuilder>().Create(delegate(Request request) { int targetWhat = whatList2[0]; if (targetWhat != request.what) { Assert.Fail("order illegal"); } if (threadUtil.IsMainThread()) { Assert.Fail("not work on work thread"); } if ((request.arg1 != 10) || (request.arg2 != 10) || !request.obj.Equals(whatList2)) { Assert.Fail("paramters are not enough"); } whatList2.RemoveAt(0); }, ThreadType.Work); for (int i = 0; i < 10; i++) { whatList2.Add(i); requestWorker2.SendRequest(i, 10, 10, whatList2); } List <int> whatList3 = new List <int>(); IRequestWorker requestWorker3 = DependencyService.Get <IRequestWorkerBuilder>().Create(delegate(Request request) { int targetWhat = whatList3[0]; if (targetWhat != request.what) { Assert.Fail("order illegal"); } if (!threadUtil.IsMainThread()) { Assert.Fail("not work on main thread"); } if ((request.arg1 != 10) || (request.arg2 != 10) || !request.obj.Equals(whatList3)) { Assert.Fail("paramters are not enough"); } whatList3.RemoveAt(0); }); for (int i = 0; i < 10; i++) { whatList3.Add(i); requestWorker3.SendRequestDelayed(i, 10, 10, whatList3, 1000); } List <int> whatList4 = new List <int>(); IRequestWorker requestWorker4 = DependencyService.Get <IRequestWorkerBuilder>().Create(delegate(Request request) { int targetWhat = whatList4[0]; if (targetWhat != request.what) { Assert.Fail("order illegal"); } if (threadUtil.IsMainThread()) { Assert.Fail("not work on work thread"); } if ((request.arg1 != 10) || (request.arg2 != 10) || !request.obj.Equals(whatList4)) { Assert.Fail("paramters are not enough"); } whatList4.RemoveAt(0); }, ThreadType.Work); for (int i = 0; i < 10; i++) { whatList4.Add(i); requestWorker4.SendRequestDelayed(i, 10, 10, whatList4, 1000); } }