Skip to content

Commit a0e262b

Browse files
committed
add test app.
1 parent 5ffac30 commit a0e262b

10 files changed

Lines changed: 330 additions & 0 deletions

CometD.NetCore.Salesforce.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
Directory.Build.props = Directory.Build.props
1414
LICENSE = LICENSE
1515
README.md = README.md
16+
src\TestApp\TestApp.csproj = src\TestApp\TestApp.csproj
1617
EndProjectSection
1718
EndProject
19+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApp", "src\TestApp\TestApp.csproj", "{6FACF1B8-5D3D-48BF-B2A9-FA06E195D1B6}"
20+
EndProject
1821
Global
1922
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2023
Debug|Any CPU = Debug|Any CPU
@@ -25,6 +28,10 @@ Global
2528
{CDF94744-1E4D-41D4-A3AC-C0D1AFA6FD40}.Debug|Any CPU.Build.0 = Debug|Any CPU
2629
{CDF94744-1E4D-41D4-A3AC-C0D1AFA6FD40}.Release|Any CPU.ActiveCfg = Release|Any CPU
2730
{CDF94744-1E4D-41D4-A3AC-C0D1AFA6FD40}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{6FACF1B8-5D3D-48BF-B2A9-FA06E195D1B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{6FACF1B8-5D3D-48BF-B2A9-FA06E195D1B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{6FACF1B8-5D3D-48BF-B2A9-FA06E195D1B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{6FACF1B8-5D3D-48BF-B2A9-FA06E195D1B6}.Release|Any CPU.Build.0 = Release|Any CPU
2835
EndGlobalSection
2936
GlobalSection(SolutionProperties) = preSolution
3037
HideSolutionNode = FALSE
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using CometD.NetCore.Salesforce.Messaging;
2+
3+
namespace TestApp.EventBus.Messages
4+
{
5+
/// <summary>
6+
/// The <see cref="CustomMessageEnvelope"/> receives by <see cref="CustomMessageListener"/>
7+
/// </summary>
8+
public class CustomMessageEnvelope : MessageEnvelope<CustomMessagePayload>
9+
{
10+
}
11+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using CometD.NetCore.Bayeux;
2+
using CometD.NetCore.Bayeux.Client;
3+
using Microsoft.Extensions.Logging;
4+
using Newtonsoft.Json;
5+
6+
namespace TestApp.EventBus.Messages
7+
{
8+
/// <summary>
9+
/// The <see cref="CustomMessageListener"/> implements <see cref="IMessageListener"/>
10+
/// </summary>
11+
public class CustomMessageListener : IMessageListener
12+
{
13+
private readonly ILogger<CustomMessageListener> _logger;
14+
15+
/// <summary>
16+
/// Constructor for <see cref="CustomMessageListener"/>.
17+
/// </summary>
18+
/// <param name="logger">Instance of the <see cref="ILogger{CustomMessageListener}"/>.</param>
19+
public CustomMessageListener(ILogger<CustomMessageListener> logger)
20+
{
21+
_logger = logger;
22+
}
23+
24+
/// <summary>
25+
/// Receives salesforce message from Platform Event
26+
/// </summary>
27+
/// <param name="channel"></param>
28+
/// <param name="message"></param>
29+
public void OnMessage(IClientSessionChannel channel, IMessage message)
30+
{
31+
var msg = JsonConvert.DeserializeObject<CustomMessageEnvelope>(message.Json);
32+
33+
_logger.LogDebug($"{nameof(CustomMessageListener)} payload: {message.Json}");
34+
35+
var custName = msg.Data.Payload.CustomerName;
36+
var replayId = msg.Data.Event.ReplayId;
37+
38+
_logger.LogDebug($"Customer Name: {custName} - ReplayId: {replayId}");
39+
}
40+
}
41+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using CometD.NetCore.Salesforce.Messaging;
3+
using Newtonsoft.Json;
4+
5+
namespace TestApp.EventBus.Messages
6+
{
7+
/// <summary>
8+
/// The <see cref="CustomMessagePayload"/>
9+
/// <example>
10+
///{
11+
/// "data": {
12+
/// "schema": "1qUPELmVz7qUv3ntwyN1eA",
13+
/// "payload": {
14+
/// "CreatedDate": "2018-07-21T02:26:10.433Z",
15+
/// "CreatedById": "005f2000008xN7VAAU",
16+
/// "Customer_Name__c": "test"
17+
/// },
18+
/// "event": {
19+
/// "replayId": 1
20+
/// }
21+
/// },
22+
/// "channel": "/event/Custom_Event__e"
23+
///}
24+
/// </example>
25+
/// </summary>
26+
public class CustomMessagePayload : MessagePayload
27+
{
28+
[JsonProperty("Customer_Name__c")]
29+
public string CustomerName { get; set; }
30+
}
31+
}

src/TestApp/HostExtensions.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using CometD.NetCore.Bayeux.Client;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.ObjectPool;
5+
using TestApp.EventBus.Messages;
6+
using TestApp.Services;
7+
8+
namespace TestApp
9+
{
10+
public static class HostExtensions
11+
{
12+
public static IHostBuilder ConfigureHost(this IHostBuilder builder)
13+
{
14+
15+
return builder.ConfigureServices((bulderContext, services) =>
16+
{
17+
services.AddLogging();
18+
services.AddHostedService<SalesforceEventBusHostedService>();
19+
20+
services.AddTransient<IMessageListener,CustomMessageListener>();
21+
22+
// Conjure up a RequestServices
23+
services.AddTransient<IServiceProviderFactory<IServiceCollection>, DefaultServiceProviderFactory>();
24+
25+
// Ensure object pooling is available everywhere.
26+
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
27+
28+
// no need to create instance = services.AddStreamingClient();
29+
services.AddSalesforceEventBus();
30+
31+
});
32+
}
33+
}
34+
}

src/TestApp/Program.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace TestApp
8+
{
9+
public class Program
10+
{
11+
public static async Task Main(string[] args)
12+
{
13+
14+
var host = new HostBuilder()
15+
.ConfigureHostConfiguration(configHost =>
16+
{
17+
configHost.SetBasePath(Directory.GetCurrentDirectory());
18+
configHost.AddJsonFile("hostsettings.json", optional: true);
19+
configHost.AddEnvironmentVariables(prefix: "PREFIX_");
20+
configHost.AddCommandLine(args);
21+
})
22+
.ConfigureAppConfiguration((hostContext, configApp) =>
23+
{
24+
configApp.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
25+
configApp.AddJsonFile(
26+
$"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
27+
optional: true);
28+
configApp.AddEnvironmentVariables(prefix: "PREFIX_");
29+
configApp.AddCommandLine(args);
30+
})
31+
.ConfigureHost()
32+
.ConfigureLogging((hostContext, configLogging) =>
33+
{
34+
configLogging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
35+
configLogging.AddConsole();
36+
configLogging.AddDebug();
37+
})
38+
.UseConsoleLifetime()
39+
.Build();
40+
41+
var srv = host.Services;
42+
43+
await host.RunAsync();
44+
}
45+
46+
47+
}
48+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using CometD.NetCore.Salesforce;
4+
using KDCLLC.BuildingBlocks.Abstractions;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.Extensions.Logging;
7+
using TestApp.EventBus.Messages;
8+
9+
namespace TestApp.Services
10+
{
11+
/// <summary>
12+
/// Provides with LifetimeEventsHostedService
13+
/// </summary>
14+
internal class SalesforceEventBusHostedService : IHostedService
15+
{
16+
private readonly ILogger _logger;
17+
private readonly IApplicationLifetime _appLifetime;
18+
private readonly SalesforceConfiguration _options;
19+
private readonly IEventBus _eventBus;
20+
21+
public SalesforceEventBusHostedService(
22+
ILogger<SalesforceEventBusHostedService> logger,
23+
IApplicationLifetime appLifetime,
24+
SalesforceConfiguration options,
25+
IEventBus eventBus)
26+
{
27+
_logger = logger;
28+
_appLifetime = appLifetime;
29+
_options = options;
30+
_eventBus = eventBus;
31+
}
32+
33+
public async Task StartAsync(CancellationToken cancellationToken)
34+
{
35+
_appLifetime.ApplicationStarted.Register(OnStarted);
36+
_appLifetime.ApplicationStopping.Register(OnStopping);
37+
_appLifetime.ApplicationStopped.Register(OnStopped);
38+
39+
_logger.LogInformation("StartAsync has been called.");
40+
41+
await _eventBus.Subscribe<CustomMessageListener>(
42+
new PlatformEvent() { Name = _options.CustomEvent, ReplayId= _options.ReplayId });
43+
44+
//return Task.CompletedTask;
45+
}
46+
47+
public async Task StopAsync(CancellationToken cancellationToken)
48+
{
49+
_logger.LogInformation("StopAsync has been called.");
50+
51+
await _eventBus.Unsubscribe<CustomMessageListener>(
52+
new PlatformEvent() { Name = _options.CustomEvent, ReplayId=_options.ReplayId});
53+
}
54+
55+
private void OnStarted()
56+
{
57+
_logger.LogInformation("OnStarted has been called.");
58+
59+
// Perform post-startup activities here
60+
}
61+
62+
private void OnStopping()
63+
{
64+
_logger.LogInformation("OnStopping has been called.");
65+
66+
// Perform on-stopping activities here
67+
}
68+
69+
private void OnStopped()
70+
{
71+
_logger.LogInformation("OnStopped has been called.");
72+
73+
// Perform post-stopped activities here
74+
}
75+
}
76+
}

src/TestApp/TestApp.csproj

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<LangVersion>latest</LangVersion>
7+
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="CometD.NetCore2" Version="1.0.2" />
12+
<PackageReference Include="KDCLLC.BuildingBlocks.SalesforceEventBus" Version="1.0.2" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.1" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.1.1" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
17+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.1" />
18+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.1" />
19+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
20+
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.1.1" />
21+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
22+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
23+
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="2.1.1" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<Content Include="appsettings*.json" CopyToOutputDirectory="PreserveNewest" />
28+
<Content Include="hostsettings*.json" CopyToOutputDirectory="PreserveNewest" />
29+
</ItemGroup>
30+
31+
<ItemGroup>
32+
<None Remove="appsettings.Development.json" />
33+
</ItemGroup>
34+
35+
<ItemGroup>
36+
<Content Update="appsettings.Development.json">
37+
<DependentUpon>appsettings.json</DependentUpon>
38+
</Content>
39+
</ItemGroup>
40+
41+
</Project>

src/TestApp/appsettings.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": true,
4+
"Debug": {
5+
"LogLevel": {
6+
"Default": "Debug",
7+
"System": "Warning",
8+
"Microsoft": "Warning",
9+
"SetMinimumLevel": "Debug"
10+
}
11+
},
12+
"Console": {
13+
"LogLevel": {
14+
"Default": "Debug",
15+
"System": "Warning",
16+
"Microsoft": "Warning",
17+
"SetMinimumLevel": "Debug"
18+
}
19+
}
20+
},
21+
"AllowedHosts": "*",
22+
"Salesforce": {
23+
"ClientId": "",
24+
"ClientSecret": "",
25+
"RefreshToken": "",
26+
"AccessToken": "",
27+
"RedirectUri": "http://localhost:5050/",
28+
"OrganizationUrl": "",
29+
"LoginUrl": "https://login.salesforce.com",
30+
"OAuthUri": "/services/oauth2/token",
31+
"PublishEndpoint": "/services/data/v42.0/sobjects/",
32+
"EventOrTopicUri": "/event",
33+
"CometDUri": "/cometd/42.0",
34+
"Retry": 2,
35+
"CustomEvent": "Custom_Event__e",
36+
"ReplayId": "-2"
37+
}
38+
}

src/TestApp/hostsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"environment": "Development"
3+
}

0 commit comments

Comments
 (0)