public class GreetingOptions
{
public string GreetAt { get; set; }
public string GreetTo { get; set; }
}
public class GreetingMiddleware
{
private readonly RequestDelegate _next;
private readonly GreetingOptions _options;
public GreetingMiddleware(RequestDelegate next, GreetingOptions options)
{
_next = next;
_options = options;
}
public async Task Invoke(HttpContext context)
{
var message = $"Good {_options.GreetAt} {_options.GreetTo}";
await context.Response.WriteAsync(message);
}
}
public static IApplicationBuilder UseGreetingMiddleware(this IApplicationBuilder app, GreetingOptions options)
{
return app.UseMiddleware<GreetingMiddleware>(options);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseGreetingMiddleware(new GreetingOptions {
GreetAt = "Morning",
GreetTo = "Tahir"
});
}
public static IApplicationBuilder UseGreetingMiddlewareAction(this IApplicationBuilder app, Action<GreetingOptions> optionsAction)
{
var options = new GreetingOptions();
optionsAction(options);
return app.UseMiddleware<GreetingMiddleware>(options);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseGreetingMiddlewareAction(options =>
{
options.GreetAt = "Morning";
options.GreetTo = "Tahir";
});
}
public interface IMessageService
{ string FormatMessage(string message);
}
public class MessageService : IMessageService
{
private readonly GreetingOptions _options;
public MessageService(GreetingOptions options)
{
_options = options;
}
public string FormatMessage(string message)
{
return $"Good {_options.GreetAt} {_options.GreetTo} - {message}";
}
}
public static IServiceCollection AddMessageService(this IServiceCollection services, GreetingOptions options)
{ return services.AddScoped<IMessageService>(factory => new MessageService(options));
}
public static IServiceCollection AddMessageServiceAction(this IServiceCollection services, Action<GreetingOptions> optionsAction)
{
var options = new GreetingOptions();
optionsAction(options);
return services.AddScoped<IMessageService>(factory => new MessageService(options));
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMessageService(new GreetingOptions
{
GreetAt = "Morning",
GreetTo = "Tahir"
});
services.AddMessageServiceAction(options =>
{
options.GreetAt = "Morning";
options.GreetTo = "Tahir";
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessageService msg)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync(msg.FormatMessage("by sanshi"));
});
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有