private static void Main(string[] args) { Trace.Listeners.Add(new ConsoleTraceListener()); // можно в конфиг файле Trace.TraceInformation("Console Worker entry point called"); var converters = new Dictionary <string, IFileProcessor>(); converters["ToUpper"] = new ToUpperFileProcessor(); ILifetimeAwareComponent <IFileProcessingWorker> app = AppComposition.CreateFileProcessingWorker( converters, ConfigurationManager.ConnectionStrings["redisConnectionString"].ConnectionString); using (app.LifetimeScope) { while (true) { try { // нельзя использовать await здесь, метод должен быть блокирующимся Task.Delay(10000).GetAwaiter().GetResult(); app.Service.DoWorkAsync().GetAwaiter().GetResult(); } catch (Exception e) { Trace.TraceError("Ошибка обработки {0}", JsonConvert.SerializeObject(e)); } } } }
public override void Run() { // This is a sample worker implementation. Replace with your logic. Trace.TraceInformation("AzureService.Worker entry point called"); var converters = new Dictionary <string, IFileProcessor>(); converters["ToUpper"] = new ToUpperFileProcessor(); ILifetimeAwareComponent <IFileProcessingWorker> app = AppComposition.CreateFileProcessingWorker( converters, CloudConfigurationManager.GetSetting("redisConnectionString")); using (app.LifetimeScope) { while (true) { // нельзя использовать await здесь, метод должен быть блокирующимся Task.Delay(10000).GetAwaiter().GetResult(); app.Service.DoWorkAsync().GetAwaiter().GetResult(); } } }
public void TaskPipelineTest() { var operationContent = new StringContent("ToUpper"); var cd = new ContentDispositionHeaderValue("inline"); cd.Name = "operation"; operationContent.Headers.ContentDisposition = cd; var fileContent = new ByteArrayContent(File.ReadAllBytes(@"..\..\Source.txt")); cd = new ContentDispositionHeaderValue("attachment"); cd.Name = "file"; cd.FileName = "Source.txt"; fileContent.Headers.ContentDisposition = cd; var md = new MediaTypeHeaderValue("text/plain"); fileContent.Headers.ContentType = md; var multipartContent = new MultipartContent(); multipartContent.Add(operationContent); multipartContent.Add(fileContent); using (var server = MockWebApi.CreateServer()) { var response = server.CreateRequest("/api/ProcessingTask") .And(r => r.Content = multipartContent) .PostAsync() .GetAwaiter() .GetResult(); Assert.IsTrue(HttpStatusCode.OK == response.StatusCode, "StatusCode should be OK"); ProcessingTask task = response.Content.ReadAsAsync <ProcessingTask>().GetAwaiter().GetResult(); string taskId = task.Id; Assert.IsTrue(TaskStatus.Enqueued == task.Status, "Task should be enqueued"); response = server.CreateRequest("/api/ProcessingTaskStatus?taskId=" + taskId) .GetAsync() .GetAwaiter() .GetResult(); Assert.IsTrue(HttpStatusCode.OK == response.StatusCode, "StatusCode should be OK"); task = response.Content.ReadAsAsync <ProcessingTask>().GetAwaiter().GetResult(); Assert.IsTrue(TaskStatus.Enqueued == task.Status, "Task should be still enqueued"); var converters = new Dictionary <string, IFileProcessor>(); converters["ToUpper"] = new ToUpperFileProcessor(); ILifetimeAwareComponent <IFileProcessingWorker> app = MockWebApi.CreateFileProcessingWorker(converters); using (app.LifetimeScope) { app.Service.DoWorkAsync().GetAwaiter().GetResult(); } response = server.CreateRequest("/api/ProcessingTaskStatus?taskId=" + taskId) .GetAsync() .GetAwaiter() .GetResult(); Assert.IsTrue(HttpStatusCode.OK == response.StatusCode, "StatusCode should be OK"); task = response.Content.ReadAsAsync <ProcessingTask>().GetAwaiter().GetResult(); Assert.IsTrue(TaskStatus.Success == task.Status, "Task should be succeeded"); response = server.CreateRequest("/api/ProcessingResult?taskId=" + taskId) .GetAsync() .GetAwaiter() .GetResult(); Assert.IsTrue(HttpStatusCode.OK == response.StatusCode, "StatusCode should be OK"); } }