Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example to get HubConnection from a interface property #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/TypedSignalR.Client.Tests.Server/Hubs/UnaryHub3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.SignalR;
using TypedSignalR.Client.Tests.Shared;

namespace TypedSignalR.Client.Tests.Server.Hubs;

public class UnaryHub3 : Hub, IUnaryHub3
{
private readonly ILogger<UnaryHub3> _logger;

public UnaryHub3(ILogger<UnaryHub3> logger)
{
_logger = logger;
}

public Task<int> Add(int x, int y)
{
_logger.Log(LogLevel.Information, "UnaryHub.Add");

return Task.FromResult(x + y);
}
}
1 change: 1 addition & 0 deletions tests/TypedSignalR.Client.Tests.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
app.MapControllers();

app.MapHub<UnaryHub>("/Hubs/UnaryHub");
app.MapHub<UnaryHub3>("/Hubs/UnaryHub3");
app.MapHub<SideEffectHub>("/Hubs/SideEffectHub");
app.MapHub<ReceiverTestHub>("/Hubs/ReceiverTestHub");
app.MapHub<StreamingHub>("/Hubs/StreamingHub");
Expand Down
6 changes: 6 additions & 0 deletions tests/TypedSignalR.Client.Tests.Shared/IUnaryHub3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TypedSignalR.Client.Tests.Shared;

public interface IUnaryHub3
{
Task<int> Add(int x, int y);
}
61 changes: 61 additions & 0 deletions tests/TypedSignalR.Client.Tests/UnaryTest3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using TypedSignalR.Client.Tests.Shared;
using Xunit;

namespace TypedSignalR.Client.Tests;

// Lunch TypedSignalR.Client.Tests.Server.csproj before test!
public class UnaryTest3
{
[Fact]
public async Task Add()
{
// case 1. holder's type is ConnectionHolder
//var holder = new ConnectionHolder();

//case 2. holder's type is IConnectionHolder
var holder = ConnectionHolder.Create();

// ---------

// case 1. use var
//var connection = holder.HubConnection;
//var hubProxy = connection.CreateHubProxy<IUnaryHub3>();

// case 2. use property directly
var hubProxy = holder.HubConnection.CreateHubProxy<IUnaryHub3>();

await holder.HubConnection.StartAsync();

var x = Random.Shared.Next();
var y = Random.Shared.Next();

var added = await hubProxy.Add(x, y);

Assert.Equal(added, x + y);

await holder.HubConnection.StopAsync();
await holder.HubConnection.DisposeAsync();
}
}

interface IConnectionHolder
{
HubConnection HubConnection { get; }
}

class ConnectionHolder : IConnectionHolder
{
public HubConnection HubConnection { get; }

public ConnectionHolder()
{
this.HubConnection = new HubConnectionBuilder()
.WithUrl("http://localhost:5105/Hubs/UnaryHub3")
.Build();
}

public static IConnectionHolder Create() => new ConnectionHolder();
}