From fd10deba6cf27b59ad4278d51cf35f33785bf542 Mon Sep 17 00:00:00 2001 From: moozzyk Date: Tue, 11 Oct 2016 17:55:36 -0700 Subject: [PATCH] Removing formatters --- .../SocketsSample/EndPoints/HubEndpoint.cs | 4 +-- .../SocketsSample/EndPoints/RpcEndpoint.cs | 7 +--- samples/SocketsSample/FormatterExtensions.cs | 28 +++++---------- samples/SocketsSample/IFormatter.cs | 12 ------- samples/SocketsSample/SocketFormatters.cs | 35 +------------------ samples/SocketsSample/Startup.cs | 10 +++--- .../IFormatter.cs | 12 ------- 7 files changed, 18 insertions(+), 90 deletions(-) delete mode 100644 samples/SocketsSample/IFormatter.cs delete mode 100644 src/Microsoft.AspNetCore.Sockets/IFormatter.cs diff --git a/samples/SocketsSample/EndPoints/HubEndpoint.cs b/samples/SocketsSample/EndPoints/HubEndpoint.cs index eb48626b9a..17d52ca9af 100644 --- a/samples/SocketsSample/EndPoints/HubEndpoint.cs +++ b/samples/SocketsSample/EndPoints/HubEndpoint.cs @@ -63,7 +63,7 @@ namespace SocketsSample foreach (var connection in _endPoint.Connections) { - var invocationAdapter = _endPoint._serviceProvider.GetRequiredService() + var invocationAdapter = _endPoint._serviceProvider.GetRequiredService() .GetInvocationAdapter((string)connection.Metadata["formatType"]); tasks.Add(invocationAdapter.InvokeClientMethod(connection.Channel.GetStream(), message)); @@ -88,7 +88,7 @@ namespace SocketsSample { var connection = _endPoint.Connections[_connectionId]; - var invocationAdapter = _endPoint._serviceProvider.GetRequiredService() + var invocationAdapter = _endPoint._serviceProvider.GetRequiredService() .GetInvocationAdapter((string)connection.Metadata["formatType"]); if (_endPoint._logger.IsEnabled(LogLevel.Debug)) diff --git a/samples/SocketsSample/EndPoints/RpcEndpoint.cs b/samples/SocketsSample/EndPoints/RpcEndpoint.cs index 602f9ef8e2..29f64884b9 100644 --- a/samples/SocketsSample/EndPoints/RpcEndpoint.cs +++ b/samples/SocketsSample/EndPoints/RpcEndpoint.cs @@ -44,13 +44,8 @@ namespace SocketsSample // TODO: Dispatch from the caller await Task.Yield(); - /* - var formatter = _serviceProvider.GetRequiredService() - .GetFormatter((string)connection.Metadata["formatType"]); - */ - var stream = connection.Channel.GetStream(); - var invocationAdapter = _serviceProvider.GetRequiredService() + var invocationAdapter = _serviceProvider.GetRequiredService() .GetInvocationAdapter((string)connection.Metadata["formatType"]); while (true) diff --git a/samples/SocketsSample/FormatterExtensions.cs b/samples/SocketsSample/FormatterExtensions.cs index 094ecbaada..8ce97b33ae 100644 --- a/samples/SocketsSample/FormatterExtensions.cs +++ b/samples/SocketsSample/FormatterExtensions.cs @@ -1,41 +1,31 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Sockets; using Microsoft.Extensions.DependencyInjection; namespace SocketsSample { - public static class FormatterExtensions + public static class RpcExtensions { - public static IApplicationBuilder UseFormatters(this IApplicationBuilder app, Action registerFormatters) + public static IApplicationBuilder UseRpc(this IApplicationBuilder app, Action registerAdapters) { - var formatters = app.ApplicationServices.GetRequiredService(); - registerFormatters(new FormatterBuilder(formatters)); + var adapters = app.ApplicationServices.GetRequiredService(); + registerAdapters(new RpcBuilder(adapters)); return app; } } - public class FormatterBuilder + public class RpcBuilder { - private SocketFormatters _socketFormatters; + private InvocationAdapterRegistry _invocationAdapters; - public FormatterBuilder(SocketFormatters socketFormatters) + public RpcBuilder(InvocationAdapterRegistry invocationAdapters) { - _socketFormatters = socketFormatters; - } - - public void MapFormatter(string format) - where TFormatterType : IFormatter - { - _socketFormatters.RegisterFormatter(format); + _invocationAdapters = invocationAdapters; } public void AddInvocationAdapter(string format, IInvocationAdapter adapter) { - _socketFormatters.RegisterInvocationAdapter(format, adapter); + _invocationAdapters.RegisterInvocationAdapter(format, adapter); } } } diff --git a/samples/SocketsSample/IFormatter.cs b/samples/SocketsSample/IFormatter.cs deleted file mode 100644 index 78eb11b4a2..0000000000 --- a/samples/SocketsSample/IFormatter.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.IO; -using System.Threading.Tasks; - -namespace SocketsSample -{ - // TODO: Is this name too generic? - public interface IFormatter - { - Task ReadAsync(Stream stream); - Task WriteAsync(T value, Stream stream); - } -} diff --git a/samples/SocketsSample/SocketFormatters.cs b/samples/SocketsSample/SocketFormatters.cs index 52bbe94f35..344c9b55b8 100644 --- a/samples/SocketsSample/SocketFormatters.cs +++ b/samples/SocketsSample/SocketFormatters.cs @@ -5,43 +5,10 @@ using Microsoft.Extensions.DependencyInjection; namespace SocketsSample { - public class SocketFormatters + public class InvocationAdapterRegistry { - private IServiceProvider _serviceProvider; - private Dictionary> _formatters = new Dictionary>(); private Dictionary _invocationAdapters = new Dictionary(); - public SocketFormatters(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } - - public void RegisterFormatter(string format) - where TFormatterType : IFormatter - { - Dictionary formatFormatters; - if (!_formatters.TryGetValue(format, out formatFormatters)) - { - formatFormatters = _formatters[format] = new Dictionary(); - } - - formatFormatters[typeof(T)] = typeof(TFormatterType); - } - - public IFormatter GetFormatter(string format) - { - Dictionary formatters; - Type targetFormatterType; - - if (_formatters.TryGetValue(format, out formatters) && formatters.TryGetValue(typeof(T), out targetFormatterType)) - { - return (IFormatter)_serviceProvider.GetRequiredService(targetFormatterType); - } - - return null; - // throw new InvalidOperationException($"No formatter register for format '{format}' and type '{typeof(T).GetType().FullName}'"); - } - public void RegisterInvocationAdapter(string format, IInvocationAdapter adapter) { _invocationAdapters[format] = adapter; diff --git a/samples/SocketsSample/Startup.cs b/samples/SocketsSample/Startup.cs index 2ba8bc7570..7677d6e56c 100644 --- a/samples/SocketsSample/Startup.cs +++ b/samples/SocketsSample/Startup.cs @@ -21,7 +21,7 @@ namespace SocketsSample services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -44,11 +44,11 @@ namespace SocketsSample routes.MapSocketEndpoint("/jsonrpc"); }); - app.UseFormatters(formatters=> + app.UseRpc(invocationAdapters => { - formatters.AddInvocationAdapter("protobuf", new Protobuf.ProtobufInvocationAdapter(app.ApplicationServices)); - formatters.AddInvocationAdapter("json", new JSonInvocationAdapter()); - formatters.AddInvocationAdapter("line", new LineInvocationAdapter()); + invocationAdapters.AddInvocationAdapter("protobuf", new Protobuf.ProtobufInvocationAdapter(app.ApplicationServices)); + invocationAdapters.AddInvocationAdapter("json", new JSonInvocationAdapter()); + invocationAdapters.AddInvocationAdapter("line", new LineInvocationAdapter()); }); } } diff --git a/src/Microsoft.AspNetCore.Sockets/IFormatter.cs b/src/Microsoft.AspNetCore.Sockets/IFormatter.cs deleted file mode 100644 index 0ffbff5d42..0000000000 --- a/src/Microsoft.AspNetCore.Sockets/IFormatter.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.IO; -using System.Threading.Tasks; - -namespace Microsoft.AspNetCore.Sockets -{ - // TODO: Is this name too generic? - public interface IFormatter - { - Task ReadAsync(Stream stream); - Task WriteAsync(T value, Stream stream); - } -}