public MetaWeblogMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, string urlEndpoint, MetaWeblogService service) { _next = next; _logger = loggerFactory.CreateLogger<MetaWeblogMiddleware>(); ; _urlEndpoint = urlEndpoint; _service = service; }
/// <summary> /// Porcess a request /// </summary> /// <param name="context"></param> /// <returns></returns> private static async Task Run(HttpContext context) { try { // Get a MetaWeblogService MetaWeblogService servrice = (MetaWeblogService)context.RequestServices.GetService(typeof(MetaWeblogService)); var sourceload = new System.Threading.CancellationTokenSource(); // Read the boy of the request to use Stream body = await ReadAsync(context.Request.Body); // Read the request XDocument input = await XDocument.LoadAsync(body, LoadOptions.None, sourceload.Token); XDocument output = servrice.Process(input); // Add header en encoding context.Response.ContentType = "text/xml"; // Write await context.Response.WriteAsync(output.ToString()); } catch (Exception ex) { await context.Response.WriteAsync( MetaWeblogService.ProcessError(ex.Message) .ToString()); } }
/// <summary> /// Invokes the service for the specified request. /// </summary> /// <param name="context">The context.</param> /// <param name="service">The service.</param> /// <returns>A <see cref="Task"/>.</returns> public async Task Invoke(HttpContext context, MetaWeblogService service) { if (context.Request.Method == "POST" && context.Request.Path.StartsWithSegments(this.urlEndpoint) && context.Request != null && context.Request.ContentType.ToLower().Contains("text/xml")) { context.Response.ContentType = "text/xml"; using var rdr = new StreamReader(context.Request.Body); var xml = await rdr.ReadToEndAsync(); this.logger.LogInformation($"Request XMLRPC: {xml}"); var result = await service.InvokeAsync(xml); this.logger.LogInformation($"Result XMLRPC: {result}"); await context.Response.WriteAsync(result, Encoding.UTF8); return; } // Continue On await this.next.Invoke(context); }
public MetaWeblogServiceTest() { // loggers var loggerMetaSvc = _loggerFactory.CreateLogger <MetaWeblogService>(); // metaweblog svc var context = new Mock <HttpContext>(); var contextAccessor = new Mock <IHttpContextAccessor>(); contextAccessor.Setup(x => x.HttpContext).Returns(context.Object); _svc = new MetaWeblogService(new FakeUserManager(), new FakeSignInManager(contextAccessor.Object), _blogSvc, _settingSvcMock.Object, _mediaSvcMock.Object, loggerMetaSvc); }