Last active
August 29, 2015 14:23
-
-
Save MrAntix/8a9a887ea3d30fcf0f75 to your computer and use it in GitHub Desktop.
Use a service resolved from a DI container with Owin Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CreateQRService : | |
ICreateQRService | |
{ | |
readonly Log.Delegate _log; | |
byte[] _qr; | |
public CreateQRService( | |
Log.Delegate log) | |
{ | |
_log = log; | |
} | |
public async Task InvokeAsync( | |
IOwinContext context) | |
{ | |
await context.Response.WriteAsync( | |
await GetQR("http://antix.co.uk/")); | |
} | |
async Task<byte[]> GetQR(string text) | |
{ | |
if (_qr != null) return _qr; | |
_log.Debug(m => m("CreateQRService creating qr for {0}", text)); | |
var encoder = new QrEncoder(); | |
var qrCode = encoder.Encode(text); | |
var renderer = new SVGRenderer( | |
new FixedModuleSize(6, QuietZoneModules.Two), | |
new FormColor(Color.FromArgb(255, 16, 16, 16)), | |
new FormColor(Color.FromArgb(160, 255, 255, 255))); | |
using (var ms = new MemoryStream()) | |
{ | |
renderer.WriteToStream(qrCode.Matrix, ms, false); | |
_qr = ms.ToArray(); | |
} | |
return _qr; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ICreateQRService : | |
IServiceMiddlewareService | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IServiceMiddlewareService | |
{ | |
Task InvokeAsync(IOwinContext context); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ServiceMiddleware<T> : | |
OwinMiddleware | |
where T : IServiceMiddlewareService | |
{ | |
readonly Func<T> _resolve; | |
readonly Action<T> _release; | |
public ServiceMiddleware( | |
OwinMiddleware next, | |
Func<T> resolve, | |
Action<T> release) : base(next) | |
{ | |
_resolve = resolve; | |
_release = release; | |
} | |
public override async Task Invoke(IOwinContext context) | |
{ | |
var service = _resolve(); | |
try | |
{ | |
await service.InvokeAsync(context); | |
await Next.Invoke(context); | |
} | |
finally | |
{ | |
_release(service); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ServiceMiddlewareExtensions | |
{ | |
public static IAppBuilder UseService<T>( | |
this IAppBuilder appBuilder, | |
Func<T> resolve, | |
Action<T> release) | |
where T : IServiceMiddlewareService | |
{ | |
appBuilder.Use<ServiceMiddleware<T>>(resolve, release); | |
return appBuilder; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
var container = new WindsorContainer() | |
.Configure(); | |
app.Map("/connect", map => | |
{ | |
map.UseService( | |
container.Resolve<ICreateQRService>, | |
container.Release | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment