Skip to content

Commit

Permalink
Merge pull request #5136 from Particular/aspv4
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach authored Nov 25, 2020
2 parents e19024f + 4b2eea4 commit f4d0ad3
Show file tree
Hide file tree
Showing 93 changed files with 1,933 additions and 43 deletions.
4 changes: 4 additions & 0 deletions Snippets/ASP/ASP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASP_2", "ASP_2\ASP_2.csproj
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASTP_3", "ASTP_3\ASTP_3.csproj", "{F83ED6B8-C08C-4B80-9053-22E274507624}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASTP_4", "ASTP_4\ASTP_4.csproj", "{44FEE37B-B258-400E-92E2-DCFDF3537C0A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -19,6 +21,8 @@ Global
{D0DC3C7B-A506-4CED-B4D7-8BCDDC35506E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F83ED6B8-C08C-4B80-9053-22E274507624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F83ED6B8-C08C-4B80-9053-22E274507624}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44FEE37B-B258-400E-92E2-DCFDF3537C0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44FEE37B-B258-400E-92E2-DCFDF3537C0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 10 additions & 0 deletions Snippets/ASP/ASTP_4/ASTP_4.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<LangVersion>7.3</LangVersion>
<RootNamespace>ASP_3</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NServiceBus.Persistence.AzureTable" Version="4.0.0-alpha.335" />
</ItemGroup>
</Project>
79 changes: 79 additions & 0 deletions Snippets/ASP/ASTP_4/Behaviors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Persistence.AzureTable;
using NServiceBus.Pipeline;

#region ITransportReceiveContextBehavior
class PartitionKeyTransportReceiveContextBehavior
: Behavior<ITransportReceiveContext>
{
public override async Task Invoke(ITransportReceiveContext context, Func<Task> next)
{
context.Extensions.Set(new TableEntityPartitionKey("PartitionKeyValue"));

await next().ConfigureAwait(false);
}
}
#endregion

#region IIncomingLogicalMessageContextBehavior
class PartitionKeyIncomingLogicalMessageContextBehavior
: Behavior<IIncomingLogicalMessageContext>
{
public override async Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
{
context.Extensions.Set(new TableEntityPartitionKey("PartitionKeyValue"));

await next().ConfigureAwait(false);
}
}
#endregion

#region InsertBeforeLogicalOutbox
public class RegisterMyBehavior : RegisterStep
{
public RegisterMyBehavior() :
base(stepId: nameof(PartitionKeyIncomingLogicalMessageContextBehavior),
behavior: typeof(PartitionKeyIncomingLogicalMessageContextBehavior),
description: "Determines the PartitionKey from the logical message",
factoryMethod: b => new PartitionKeyIncomingLogicalMessageContextBehavior())
{
InsertBeforeIfExists(nameof(LogicalOutboxBehavior));
}
}
#endregion

#region CustomTableNameUsingITransportReceiveContextBehavior

class ContainerInfoTransportReceiveContextBehavior
: Behavior<ITransportReceiveContext>
{
public override async Task Invoke(ITransportReceiveContext context, Func<Task> next)
{
context.Extensions.Set(
new TableInformation(
tableName: "tableName"));

await next().ConfigureAwait(false);
}
}

#endregion

#region CustomTableNameUsingIIncomingLogicalMessageContextBehavior

class ContainerInfoLogicalReceiveContextBehavior
: Behavior<IIncomingLogicalMessageContext>
{
public override async Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
{
context.Extensions.Set(
new TableInformation(
tableName: "tableName"));

await next().ConfigureAwait(false);
}
}

#endregion
45 changes: 45 additions & 0 deletions Snippets/ASP/ASTP_4/CustomProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Extensions.DependencyInjection;
using NServiceBus;
using NServiceBus.Persistence.AzureTable;

#region CustomClientProvider

class CustomTableClientProvider
: IProvideCloudTableClient
{
// get fully configured via DI container
public CustomTableClientProvider(CloudTableClient tableClient)
{
Client = tableClient;
}
public CloudTableClient Client { get; }
}

// optionally when subscriptions used
class CustomSubscriptionTableClientProvider
: IProvideCloudTableClientForSubscriptions
{
// get fully configured via DI container
public CustomSubscriptionTableClientProvider(CloudTableClient tableClient)
{
Client = tableClient;
}
public CloudTableClient Client { get; }
}
#endregion

class CustomClientProviderRegistration
{
public CustomClientProviderRegistration(EndpointConfiguration endpointConfiguration)
{
#region CustomClientProviderRegistration

endpointConfiguration.RegisterComponents(services => services.AddSingleton<CustomTableClientProvider>());

// optionally when subscriptions used
endpointConfiguration.RegisterComponents(services => services.AddSingleton<CustomSubscriptionTableClientProvider>());

#endregion
}
}
106 changes: 106 additions & 0 deletions Snippets/ASP/ASTP_4/SharedTransactions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
using NServiceBus;
using NServiceBus.Persistence.AzureTable;

class MyMessage
{
public string ItemId { get; set; }
}

class UsageHandler : IHandleMessages<MyMessage>
{
#region HandlerSharedTransaction
public Task Handle(MyMessage message, IMessageHandlerContext context)
{
var session = context.SynchronizedStorageSession.AzureTablePersistenceSession();

var test1 = new ToDoActivity { PartitionKey = session.PartitionKey, RowKey = Guid.NewGuid().ToString() };
var test2 = new ToDoActivity { PartitionKey = session.PartitionKey, RowKey = Guid.NewGuid().ToString() };
var test3 = new ToDoActivity { PartitionKey = session.PartitionKey, RowKey = Guid.NewGuid().ToString() };
var test4 = new ToDoActivity { PartitionKey = session.PartitionKey, RowKey = Guid.NewGuid().ToString() };

session.Batch.Add(TableOperation.Insert(test1));
session.Batch.Add(TableOperation.Replace(test2));
session.Batch.Add(TableOperation.InsertOrReplace(test3));
session.Batch.Add(TableOperation.Delete(test4));

return Task.CompletedTask;
}
#endregion
}

#region TransactionalBatchRegisteredWithDependencyInjectionResolvedInHandler

public class ToDoActivity : TableEntity
{
public string Description { get; set; }
}

class MyHandler : IHandleMessages<MyMessage>
{
public MyHandler(IAzureTableStorageSession storageSession)
{
this.storageSession = storageSession;
}

public Task Handle(MyMessage message, IMessageHandlerContext context)
{
var entity = new ToDoActivity
{
PartitionKey = storageSession.PartitionKey,
RowKey = "RowKey"
};

storageSession.Batch.Add(TableOperation.Insert(entity));

return Task.CompletedTask;
}

private readonly IAzureTableStorageSession storageSession;
}

#endregion

#region TransactionalBatchRegisteredWithDependencyInjectionResolvedInCustomType

class MyCustomDependency
{
public MyCustomDependency(IAzureTableStorageSession storageSession)
{
this.storageSession = storageSession;
}

public void DeleteInAzureTable(string itemId)
{
var entity = new ToDoActivity
{
PartitionKey = storageSession.PartitionKey,
RowKey = itemId
};

storageSession.Batch.Add(TableOperation.Insert(entity));
}

private readonly IAzureTableStorageSession storageSession;
}

class MyHandlerWithCustomDependency : IHandleMessages<MyMessage>
{
public MyHandlerWithCustomDependency(MyCustomDependency customDependency)
{
this.customDependency = customDependency;
}

public Task Handle(MyMessage message, IMessageHandlerContext context)
{
customDependency.DeleteInAzureTable(message.ItemId);

return Task.CompletedTask;
}

private readonly MyCustomDependency customDependency;
}

#endregion
116 changes: 116 additions & 0 deletions Snippets/ASP/ASTP_4/Usage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using NServiceBus;

class Usage
{
Usage(EndpointConfiguration endpointConfiguration)
{
#region PersistenceWithAzure

var persistence = endpointConfiguration.UsePersistence<AzureTablePersistence>();
persistence.ConnectionString("DefaultEndpointsProtocol=https;AccountName=[ACCOUNT];AccountKey=[KEY];");

#endregion

#region RegisterLogicalBehavior

endpointConfiguration.Pipeline.Register(new RegisterMyBehavior());

#endregion
}

#region PersistenceWithAzureHost

public class EndpointConfig : IConfigureThisEndpoint
{
public void Customize(EndpointConfiguration endpointConfiguration)
{
var persistence = endpointConfiguration.UsePersistence<AzureTablePersistence>();
persistence.ConnectionString("DefaultEndpointsProtocol=https;AccountName=[ACCOUNT];AccountKey=[KEY];");
}
}

#endregion

void CustomizingAzurePersistenceAllConnections(EndpointConfiguration endpointConfiguration)
{
#region AzurePersistenceAllConnectionsCustomization

var persistence = endpointConfiguration.UsePersistence<AzureTablePersistence>();
persistence.ConnectionString("connectionString");

#endregion
}

void CustomizingAzurePersistenceSubscriptions(EndpointConfiguration endpointConfiguration)
{
#region AzurePersistenceSubscriptionsCustomization

var persistence = endpointConfiguration.UsePersistence<AzureTablePersistence, StorageType.Subscriptions>();
persistence.ConnectionString("connectionString");
persistence.TableName("tableName");

// Added in Version 1.3
persistence.CacheFor(TimeSpan.FromMinutes(1));

#endregion
}

void CustomizingAzurePersistenceSagas(EndpointConfiguration endpointConfiguration)
{
#region AzurePersistenceSagasCustomization

var persistence = endpointConfiguration.UsePersistence<AzureTablePersistence, StorageType.Sagas>();
persistence.ConnectionString("connectionString");

endpointConfiguration.EnableInstallers();

#endregion
}

void CustomizingDefaultTableName(EndpointConfiguration endpointConfiguration)
{
#region SetDefaultTable

var persistence = endpointConfiguration.UsePersistence<AzureTablePersistence>();
persistence.ConnectionString("connectionString");
persistence.DefaultTable("TableName");

endpointConfiguration.EnableInstallers();

#endregion
}

void EnableInstallersConfiguration(EndpointConfiguration endpointConfiguration)
{
#region EnableInstallersConfiguration

var persistence = endpointConfiguration.UsePersistence<AzureTablePersistence>();
persistence.ConnectionString("connectionString");
persistence.DefaultTable("TableName");

endpointConfiguration.EnableInstallers();

#endregion
}

void EnableInstallersConfigurationOptingOutFromTableCreation(EndpointConfiguration endpointConfiguration)
{
#region EnableInstallersConfigurationOptingOutFromTableCreation

var persistence = endpointConfiguration.UsePersistence<AzureTablePersistence>();
persistence.ConnectionString("connectionString");
persistence.DefaultTable("TableName");

// make sure the table name specified in the DefaultTable exists when calling DisableTableCreation
endpointConfiguration.EnableInstallers();
persistence.DisableTableCreation();

#endregion
}
}

// to avoid host reference
interface IConfigureThisEndpoint
{
}
Empty file.
Loading

0 comments on commit f4d0ad3

Please sign in to comment.