40
NServiceBus v6 API Welcome webinar

Async/Await: NServiceBus v6 API Update

Embed Size (px)

Citation preview

Page 1: Async/Await: NServiceBus v6 API Update

NServiceBus v6 APIWelcome

webinar

Page 2: Async/Await: NServiceBus v6 API Update

Solution Architect

Enthusiastic Software Engineer

Microsoft MVP for systems integration

@danielmarbach

particular.net/blog

planetgeek.ch

Page 3: Async/Await: NServiceBus v6 API Update

factory

chocolate

https://commons.wikimedia.org/wiki/File:Flag-map_of_Switzerland.svg

Page 4: Async/Await: NServiceBus v6 API Update

Facility

Grinding

Roasting

Blending

ProduceChocolateBar

ChocolateBarProducer (Policy)RoastBeans

GrindBeans

BlendChocolate

Page 6: Async/Await: NServiceBus v6 API Update

V5Demoawait

Page 7: Async/Await: NServiceBus v6 API Update

as-a-service

chocolate

Page 8: Async/Await: NServiceBus v6 API Update

class VanillaHandler : IHandleMessages<AcquireVanilla> {

void Handle(AcquireVanilla message)

{

AcquireVanillaFromGovernmentService();

StoreVanillaUsageInDatabase();

}

}

Page 9: Async/Await: NServiceBus v6 API Update

class VanillaHandler : IHandleMessages<AcquireVanilla> {

void Handle(AcquireVanilla message)

{

AcquireVanillaFromGovernmentAsync().Result;

DownloadRecipeFromBlobStorageAsync().Wait();

InsertVanillaUsageInDocumentDBAsync().Result;

StoreTelemetryDataInEventHubAsync().Wait();

}

}

Page 10: Async/Await: NServiceBus v6 API Update

class VanillaHandler : IHandleMessages<AcquireVanilla> {

void Handle(AcquireVanilla message)

{

await AcquireVanillaFromGovernmentServiceAsync();

await DownloadRecipeFromBlobStorageAsync();

await InsertVanillaUsageInDocumentDBAsync();

await StoreTelemetryDataInEventHubAsync();

}

}

Page 11: Async/Await: NServiceBus v6 API Update

Error CS4033 The 'await' operator can only be used within an

async method. Consider marking this method with the 'async'

modifier and changing its return type to 'Task'.

Page 12: Async/Await: NServiceBus v6 API Update

ALT + ENTER

Page 13: Async/Await: NServiceBus v6 API Update

class VanillaHandler : IHandleMessages<AcquireVanilla> {

async void Handle(AcquireVanilla message)

{

await AcquireVanillaFromGovernmentServiceAsync();

await DownloadRecipeFromBlobStorageAsync();

await InsertVanillaUsageInDocumentDBAsync();

await StoreTelemetryDataInEventHubAsync();

}

}

Page 14: Async/Await: NServiceBus v6 API Update

AcquireVanillaFromGovernmentServiceAsync().Result;

DownloadRecipeFromBlobStorageAsync().Wait();

InsertVanillaUsageInDocumentDBAsync().Result;

StoreTelemetryDataInEventHubAsync().Wait();

Void Handler

Page 15: Async/Await: NServiceBus v6 API Update

AcquireVanillaFromGovernmentServiceAsync().Result;

DownloadRecipeFromBlobStorageAsync().Wait();

InsertVanillaUsageInDocumentDBAsync().Result;

StoreTelemetryDataInEventHubAsync().Wait();

Void Handler

Page 16: Async/Await: NServiceBus v6 API Update

await AcquireVanillaFromGovernmentServiceAsync();

await DownloadRecipeFromBlobStorageAsync();

await InsertVanillaUsageInDocumentDBAsync();

await StoreTelemetryDataInEventHubAsync();

Async void Handler

Page 17: Async/Await: NServiceBus v6 API Update

await AcquireVanillaFromGovernmentServiceAsync();

await DownloadRecipeFromBlobStorageAsync();

await InsertVanillaUsageInDocumentDBAsync();

await StoreTelemetryDataInEventHubAsync();

Async void Handler

Page 18: Async/Await: NServiceBus v6 API Update

Version 5

Engine

Page 19: Async/Await: NServiceBus v6 API Update

it’s time

async

particular.net/blog/async-await-its-time

Page 20: Async/Await: NServiceBus v6 API Update

class VanillaHandler : IHandleMessages<AcquireVanilla> {

Task Handle(AcquireVanilla message)

{

}

}

Page 21: Async/Await: NServiceBus v6 API Update

class VanillaHandler : IHandleMessages<AcquireVanilla> {

async Task Handle(AcquireVanilla message)

{

await AcquireVanillaFromGovernmentServiceAsync();

}

}

Page 22: Async/Await: NServiceBus v6 API Update

class VanillaHandler : IHandleMessages<AcquireVanilla> {

async Task Handle(AcquireVanilla message)

{

await AcquireVanillaFromGovernmentServiceAsync();

await DownloadRecipeFromBlobStorageAsync();

await InsertVanillaUsageInDocumentDBAsync();

await StoreTelemetryDataInEventHubAsync();

}

}

Page 23: Async/Await: NServiceBus v6 API Update

class VanillaHandler : IHandleMessages<AcquireVanilla> {

Task Handle(AcquireVanilla message)

{

var t1 = AcquireVanillaFromGovernmentServiceAsync();

var t2 = DownloadRecipeFromBlobStorageAsync();

var t3 = InsertVanillaUsageInDocumentDBAsync();

var t4 = StoreTelemetryDataInEventHubAsync();

return Task.WhenAll(t1, t2, t3, t4);

}

}

Page 24: Async/Await: NServiceBus v6 API Update

Async Task Handler

await AcquireVanillaFromGovernmentServiceAsync();

await DownloadRecipeFromBlobStorageAsync();

await InsertVanillaUsageInDocumentDBAsync();

await StoreTelemetryDataInEventHubAsync();

Page 25: Async/Await: NServiceBus v6 API Update

But there are not only

asynchronous handlers

Page 26: Async/Await: NServiceBus v6 API Update

class AnotherHandler : IHandleMessages<AnotherMessage> {

Task Handle(AnotherMessage message)

{

DoSomethingSynchronous();

}

}

Page 27: Async/Await: NServiceBus v6 API Update

class AnotherHandler : IHandleMessages<AnotherMessage> {

async Task Handle(AnotherMessage message)

{

DoSomethingSynchronous();

}

}

Page 28: Async/Await: NServiceBus v6 API Update

Warning CS1998 This async method lacks 'await'

operators and will run synchronously. Consider using the

'await' operator to await non-blocking API calls, or 'await

Task.Run(...)' to do CPU-bound work on a background

thread.

Page 29: Async/Await: NServiceBus v6 API Update

class AnotherHandler : IHandleMessages<AnotherMessage> {

Task Handle(AnotherMessage message)

{

DoSomethingSynchronous();

return Task.CompletedTask; // for .NET 4.6

return Task.FromResult(0); // for .NET 4.5

}

}

Page 30: Async/Await: NServiceBus v6 API Update

state

Ambient

Page 31: Async/Await: NServiceBus v6 API Update
Page 32: Async/Await: NServiceBus v6 API Update

state

Ambient

Page 33: Async/Await: NServiceBus v6 API Update

floating state

Context

www.insideevs.com

Page 34: Async/Await: NServiceBus v6 API Update

class VanillaHandler : IHandleMessages<AcquireVanilla> {

async Task Handle(AcquireVanilla message, IMessageHandlerContext context)

{

var vanilla = await webService.AcquireVanilla(message.LotNumber);

await context.Publish(new VanillaAcquired());

}

}

Page 35: Async/Await: NServiceBus v6 API Update

Demoawait

APIs are in early preview

V6

Page 36: Async/Await: NServiceBus v6 API Update

docs.particular.net

Guidance

Page 37: Async/Await: NServiceBus v6 API Update

Version 6

Engine

www.hdwallbackgrounds.com

Page 38: Async/Await: NServiceBus v6 API Update

github.com/danielmarbach/03-10-2016-AsyncWebinar

Slides, Links…

Page 39: Async/Await: NServiceBus v6 API Update

Q & Aawait

Page 40: Async/Await: NServiceBus v6 API Update

Thanks