Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Starting HubOptions #743

Merged
merged 5 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR.Features;
using Microsoft.AspNetCore.SignalR.Internal;
using Microsoft.AspNetCore.SignalR.Internal.Encoders;
using Microsoft.AspNetCore.SignalR.Internal.Protocol;
using Microsoft.AspNetCore.Sockets;
using Microsoft.AspNetCore.Sockets.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.SignalR.Internal.Encoders;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.SignalR
{
Expand All @@ -36,16 +37,19 @@ public class HubEndPoint<THub> : IInvocationBinder where THub : Hub
private readonly ILogger<HubEndPoint<THub>> _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IHubProtocolResolver _protocolResolver;
private readonly IOptions<HubOptions> _hubOptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any concrete things to add here for alpha?


public HubEndPoint(HubLifetimeManager<THub> lifetimeManager,
IHubProtocolResolver protocolResolver,
IHubContext<THub> hubContext,
IOptions<HubOptions> hubOptions,
ILogger<HubEndPoint<THub>> logger,
IServiceScopeFactory serviceScopeFactory)
{
_protocolResolver = protocolResolver;
_lifetimeManager = lifetimeManager;
_hubContext = hubContext;
_hubOptions = hubOptions;
_logger = logger;
_serviceScopeFactory = serviceScopeFactory;

Expand Down
12 changes: 12 additions & 0 deletions src/Microsoft.AspNetCore.SignalR/HubOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Newtonsoft.Json;

namespace Microsoft.AspNetCore.SignalR
{
public class HubOptions
{
public JsonSerializerSettings JsonSerializerSettings { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@

using System;
using Microsoft.AspNetCore.SignalR.Internal.Protocol;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace Microsoft.AspNetCore.SignalR.Internal
{
public class DefaultHubProtocolResolver : IHubProtocolResolver
{
private readonly IOptions<HubOptions> _options;

public DefaultHubProtocolResolver(IOptions<HubOptions> options)
{
_options = options;
}

public IHubProtocol GetProtocol(string protocolName, HubConnectionContext connection)
{
switch (protocolName?.ToLowerInvariant())
{
case "json":
return new JsonHubProtocol(new JsonSerializer());
return new JsonHubProtocol(JsonSerializer.Create(_options.Value.JsonSerializerSettings));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what settings are used if JsonSerializerSettings is null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default ones. Exact same as what we were doing before.

case "messagepack":
return new MessagePackHubProtocol();
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.SignalR.Internal;
using Microsoft.AspNetCore.SignalR.Internal.Protocol;
using Microsoft.AspNetCore.Sockets;
using Microsoft.Extensions.Options;
using Moq;
using Newtonsoft.Json;
using Xunit;
Expand All @@ -22,7 +23,7 @@ public void DefaultHubProtocolResolverTestsCanCreateSupportedProtocols(IHubProto
var mockConnection = new Mock<HubConnectionContext>(Channel.CreateUnbounded<HubMessage>().Out, new Mock<ConnectionContext>().Object);
Assert.IsType(
protocol.GetType(),
new DefaultHubProtocolResolver().GetProtocol(protocol.Name, mockConnection.Object));
new DefaultHubProtocolResolver(Options.Create(new HubOptions())).GetProtocol(protocol.Name, mockConnection.Object));
}

[Theory]
Expand All @@ -32,7 +33,7 @@ public void DefaultHubProtocolResolverThrowsForNotSupportedProtocol(string proto
{
var mockConnection = new Mock<HubConnectionContext>(Channel.CreateUnbounded<HubMessage>().Out, new Mock<ConnectionContext>().Object);
var exception = Assert.Throws<NotSupportedException>(
() => new DefaultHubProtocolResolver().GetProtocol(protocolName, mockConnection.Object));
() => new DefaultHubProtocolResolver(Options.Create(new HubOptions())).GetProtocol(protocolName, mockConnection.Object));

Assert.Equal($"The protocol '{protocolName ?? "(null)"}' is not supported.", exception.Message);
}
Expand Down