Skip to content

Commit 4df84dd

Browse files
committed
AC-339091: SignalR example updated
1 parent 4a5aff1 commit 4df84dd

2 files changed

Lines changed: 89 additions & 60 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Microsoft.AspNetCore.Http.Connections;
2+
using Microsoft.AspNetCore.SignalR.Client;
3+
using Microsoft.Extensions.Logging;
4+
using System.Text;
5+
6+
class Program
7+
{
8+
static async Task Main(string[] args)
9+
{
10+
// Specify credentials to sign in to Acumatica ERP.
11+
// If you use a multi-tenant Acumatica ERP instance,
12+
// append the tenant name to the login as follows: "admin@MyTenant".
13+
var login = "admin";
14+
var password = "123";
15+
// Set up a Basic authentication token.
16+
var basicAuthToken = Convert.ToBase64String(
17+
Encoding.UTF8.GetBytes(login + ":" + password));
18+
19+
// Specify the URL of an Acumatica ERP instance.
20+
string acumaticaErpUrl = "http://localhost:80/AcumaticaDB_SignalR/";
21+
// Instead of "TestSignalR", specify the name
22+
// that you specified on the Push Notifications (SM302000) form.
23+
var notification = "TestSignalR";
24+
25+
// Create and configure a SignalR Hub connection.
26+
HubConnection connection = new HubConnectionBuilder()
27+
// Specify the URL of the Push Notifications Hub.
28+
// It must end with "signalr/hubs/PushNotificationsHub".
29+
.WithUrl(acumaticaErpUrl + "signalr/hubs/PushNotificationsHub",
30+
options =>
31+
{
32+
options.Headers.Add("Authorization", "Basic " + basicAuthToken);
33+
options.Transports = HttpTransportType.WebSockets;
34+
options.SkipNegotiation = true;
35+
}).ConfigureLogging(logging =>
36+
{
37+
logging.SetMinimumLevel(LogLevel.Trace);
38+
logging.AddProvider(new CustomConsoleLoggerProvider());
39+
}).Build();
40+
41+
try
42+
{
43+
await connection.StartAsync();
44+
// Subscribe to the specified notification.
45+
await connection.InvokeAsync("Subscribe", notification);
46+
// Handle the received notifications.
47+
connection.On<object>("ReceiveNotification", notificationReceived =>
48+
Console.WriteLine(notificationReceived.ToString()));
49+
while (Console.Read() != '3')
50+
{
51+
}
52+
}
53+
catch (Exception ex)
54+
{
55+
Console.WriteLine($"Error during connection: {ex.Message}");
56+
Console.Read();
57+
}
58+
finally
59+
{
60+
await connection.StopAsync();
61+
Console.WriteLine("Connection stopped.");
62+
}
63+
}
64+
65+
// A logger to display SignalR logs in the console.
66+
public class CustomConsoleLoggerProvider : ILoggerProvider
67+
{
68+
public ILogger CreateLogger(string categoryName) =>
69+
new CustomConsoleLogger();
70+
public void Dispose() { }
71+
}
72+
73+
public class CustomConsoleLogger : ILogger
74+
{
75+
public IDisposable BeginScope<TState>(TState state) => null;
76+
public bool IsEnabled(LogLevel logLevel) => true;
77+
78+
public void Log<TState>(LogLevel logLevel, EventId eventId,
79+
TState state, Exception exception,
80+
Func<TState, Exception, string> formatter)
81+
{
82+
Console.WriteLine($"[{logLevel}] {formatter(state, exception)}");
83+
if (exception != null)
84+
{
85+
Console.WriteLine(exception);
86+
}
87+
}
88+
}
89+
}

IntegrationDevelopment/Help/ConfiguringPushNotifications/SignalRHub/TestSignalR.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)