Angular 2 Token Based Authentication with Auth0
Two part series where I implement token based authentication using Auth0 in 20 minutes, enjoy!
Part I: Signup, Login and Logout
Part II: ExpressJS backend
Stay tuned for more screencasts, cheers!
Two part series where I implement token based authentication using Auth0 in 20 minutes, enjoy!
Stay tuned for more screencasts, cheers!
See my screencast explaining problem instead:
When flowing a transaction from a client to a service Transaction.Current becomes null after awaiting a service to service call.
Unless of course you create a new TransactionScope in your service method as follows:
[OperationBehavior(TransactionScopeRequired = true)] public async Task CallAsync() { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await _service.WriteAsync(); await _service.WriteAsync(); scope.Complete(); } }
It doesn’t even have to be a service to service call, an await to a local async method also nulls Transaction.Current. To clearify with an example
[OperationBehavior(TransactionScopeRequired = true)] public async Task CallAsync() { await WriteAsync(); // Transaction.Current is now null await WriteAsync(); }
Why TransactionScopeAsyncFlowOption isn’t enabled by default I don’t know, but I don’t like to repeat myself so I figured I’d always create an inner transactionscope with that option using a custom behavior.
I created a Message Inspector, implementing IDispatchMessageInspector and attached it as a service behavior, code executes and everyting no problem there, but it doesn’t have the same effect as declaring the transactionscope in the service method.
public class TransactionScopeMessageInspector : IDispatchMessageInspector { public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { var transactionMessage = (TransactionMessageProperty)OperationContext.Current.IncomingMessageProperties["TransactionMessageProperty"]; var scope = new TransactionScope(transactionMessage.Transaction, TransactionScopeAsyncFlowOption.Enabled); return scope; } public void BeforeSendReply(ref Message reply, object correlationState) { var transaction = correlationState as TransactionScope; if (transaction != null) { transaction.Complete(); transaction.Dispose(); } } }
by looking at the identifiers when debugging I can see that it in fact is the same transaction in the message inspector as in the service but after the first call, i.e.
await _service_WriteAsync();
Transaction.Current becomes null. Same thing if not getting the current transaction from OperationContext.Current in the message inspector as well so it’s unlikely that is the problem.
public async Task CallAsync() { var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); await _service.WriteAsync(); await _service.WriteAsync(); scope.Complete(); }
with the following service contract it’s obvious that we get an exception on the second service call if transaction.current became null inbetween
[OperationContract, TransactionFlow(TransactionFlowOption.Mandatory)] Task WriteAsync();
Got a link to a book posing the exact same question on my stackoverflow question. The conclusion is basically that it can’t be done in a clean way. Quoting the book:
We consider the lack of parity with standard WCF behavior introduces by async service operations a design flaw of WCF…
And then a far from ideal / insane solution is proposed.
It seems like the only way to make this work is to create an inner transaction, if you have a better solution feel free to comment or contact me or why not answer my stackoverflow question http://stackoverflow.com/questions/34767978.
Until next time, have an excellent day!
Started a new series on efficiency in visual studio with resharper, here’s the first part that’s on navigating the code. What’s your favorite navigation shortcut?
Hope you enjoy the video and make sure to subscribe if you do, cheers!
Ljunggatan 6
603 36 Norrköping
Sweden
Phone: +46 736 46 08 48
Mail: ajden@towfeek.se