public static string WriteEndpointInterface(HttpEndpoint endpoint) { return ($@" {SharedWriter.GetObsolete(endpoint)} {GetInterfaceReturnType(endpoint.ReturnType, false)} {endpoint.Name} ( {string.Join($",{Environment.NewLine}", endpoint.GetParameters().Select(SharedWriter.GetParameter))} ); {SharedWriter.GetObsolete(endpoint)} {GetInterfaceReturnType(nameof(HttpResponseMessage), false)} {endpoint.Name}Raw ( {string.Join($",{Environment.NewLine}", endpoint.GetParametersWithoutResponseTypes().Select(SharedWriter.GetParameter))} ); {SharedWriter.GetObsolete(endpoint)} {GetInterfaceReturnType(endpoint.ReturnType, true)} {endpoint.Name}Async ( {string.Join($",{Environment.NewLine}", endpoint.GetParameters().Select(SharedWriter.GetParameter))} ); {SharedWriter.GetObsolete(endpoint)} {GetInterfaceReturnType(nameof(HttpResponseMessage), true)} {endpoint.Name}RawAsync ( {string.Join($",{Environment.NewLine}", endpoint.GetParametersWithoutResponseTypes().Select(SharedWriter.GetParameter))} ); "); }
private static string WriteConnection(HubController controller) { return($@" {SharedWriter.GetObsolete(controller)} public class {controller.Name}HubConnection : HubConnection {{ public {controller.Name}HubConnection(IConnectionFactory connectionFactory, IHubProtocol protocol, IServiceProvider serviceProvider, ILoggerFactory loggerFactory) : base(connectionFactory, protocol, serviceProvider, loggerFactory) {{ }} public {controller.Name}HubConnection(IConnectionFactory connectionFactory, IHubProtocol protocol, ILoggerFactory loggerFactory) : base(connectionFactory, protocol, loggerFactory) {{ }} {string.Join(Environment.NewLine, controller.GetEndpoints().Select(WriteEndpoint))} {string.Join(Environment.NewLine, controller.GetMessages().Select(WriteMessage))} }} "); }
private static string WriteConnectionBuilder(HubController controller) { return ($@" {SharedWriter.GetObsolete(controller)} public class {controller.Name}HubConnectionBuilder : HubConnectionBuilder {{ private bool _hubConnectionBuilt; public {controller.Name}HubConnectionBuilder(Uri host, HttpTransportType? transports = null, Action<HttpConnectionOptions> configureHttpConnection = null) : base() {{ //Remove default HubConnection to use custom one Services.Remove(Services.Where(x => x.ServiceType == typeof(HubConnection)).Single()); Services.AddSingleton<{controller.Name}HubConnection>(); Services.Configure<HttpConnectionOptions>(o => {{ o.Url = new Uri(host,""{controller.Route}""); if (transports != null) {{ o.Transports = transports.Value; }} }}); if (configureHttpConnection != null) {{ Services.Configure(configureHttpConnection); }} Services.AddSingleton<IConnectionFactory, HttpConnectionFactory>(); }} public new {controller.Name}HubConnection Build() {{ // Build can only be used once if (_hubConnectionBuilt) {{ throw new InvalidOperationException(""HubConnectionBuilder allows creation only of a single instance of HubConnection.""); }} _hubConnectionBuilt = true; // The service provider is disposed by the HubConnection var serviceProvider = Services.BuildServiceProvider(); var connectionFactory = serviceProvider.GetService<IConnectionFactory>(); if (connectionFactory == null) {{ throw new InvalidOperationException($""Cannot create {{nameof(HubConnection)}} instance.An {{nameof(IConnectionFactory)}} was not configured.""); }} return serviceProvider.GetService<{controller.Name}HubConnection>(); }} }} "); }
public static string WriteClassInterface(HttpController controller) { return ($@" {SharedWriter.GetObsolete(controller)} public interface I{controller.ClientName} : I{Settings.ClientInterfaceName} {{ {string.Join($"{Environment.NewLine}", controller.GetEndpoints().Select(WriteEndpointInterface))} }} "); }
private static string WriteEndpoint(HubEndpoint endpoint) { var parameters = endpoint.GetParameters().NotOfType <IParameter, CancellationTokenModifier>().Select(x => x.Name); var cancellationToken = endpoint.GetParameters().OfType <CancellationTokenModifier>().Select(x => x.Name).SingleOrDefault(); string parameterText = null; if (parameters.Any()) { parameterText = $"new object[]{{{string.Join(", ", parameters)}}}, "; } else { parameterText = $"null, "; } if (endpoint.Channel) { return($@" {SharedWriter.GetObsolete(endpoint)} public Task<ChannelReader<{endpoint.ChannelType}>> Stream{endpoint.Name}Async({string.Join(", ", endpoint.GetParameters().Select(SharedWriter.GetParameter))}) {{ return this.StreamAsChannelCoreAsync<{endpoint.ChannelType}>(""{endpoint.Name}"", {parameterText}{cancellationToken}); }} {SharedWriter.GetObsolete(endpoint)} public async Task<IEnumerable<{endpoint.ChannelType}>> Read{endpoint.Name}BlockingAsync({string.Join(", ", endpoint.GetParameters().Select(SharedWriter.GetParameter))}) {{ var channel = await this.StreamAsChannelCoreAsync<{endpoint.ChannelType}>(""{endpoint.Name}"", {parameterText}{cancellationToken}); IList<{endpoint.ChannelType}> items = new List<{endpoint.ChannelType}>(); while(await channel.WaitToReadAsync()) {{ while (channel.TryRead(out var item)) {{ items.Add(item); }} }} return items; }} "); } else { return($@" {SharedWriter.GetObsolete(endpoint)} public Task {endpoint.Name}Async({string.Join(", ", endpoint.GetParameters().Select(SharedWriter.GetParameter))}) {{ return this.InvokeCoreAsync(""{endpoint.Name}"", {parameterText}{cancellationToken}); }} "); } }
public static string WriteEndpointImplementation(HttpController controller, HttpEndpoint endpoint) { return ($@" {SharedWriter.GetObsolete(endpoint)} public {GetImplementationReturnType(endpoint.ReturnType, false)} {endpoint.Name} ( {string.Join($",{Environment.NewLine}", endpoint.GetParameters().Select(SharedWriter.GetParameter))} ) {{ {GetMethodDetails(controller, endpoint, false, false)} }} {SharedWriter.GetObsolete(endpoint)} public {GetImplementationReturnType(nameof(HttpResponseMessage), false)} {endpoint.Name}Raw ( {string.Join($",{Environment.NewLine}", endpoint.GetParametersWithoutResponseTypes().Select(SharedWriter.GetParameter))} ) {{ {GetMethodDetails(controller, endpoint, false, true)} }} {SharedWriter.GetObsolete(endpoint)} public {GetImplementationReturnType(endpoint.ReturnType, true)} {endpoint.Name}Async ( {string.Join($",{Environment.NewLine}", endpoint.GetParameters().Select(SharedWriter.GetParameter))} ) {{ {GetMethodDetails(controller, endpoint, true, false)} }} {SharedWriter.GetObsolete(endpoint)} public {GetImplementationReturnType(nameof(HttpResponseMessage), true)} {endpoint.Name}RawAsync ( {string.Join($",{Environment.NewLine}", endpoint.GetParametersWithoutResponseTypes().Select(SharedWriter.GetParameter))} ) {{ {GetMethodDetails(controller, endpoint, true, true)} }} "); }
public static string WriteClassImplementation(HttpController controller) { var dependencies = controller.GetInjectionDependencies().ToList(); dependencies.Insert(0, new ClientDependency($"I{Settings.ClientInterfaceName}Wrapper")); return ($@" {SharedWriter.GetObsolete(controller)} {(Settings.UseInternalClients ? "internal" : "public")} class {controller.ClientName} : I{controller.ClientName} {{ {string.Join($"{Environment.NewLine}", dependencies.Select(WriteDependenciesField))} public {controller.ClientName}( {string.Join($",{Environment.NewLine}", dependencies.Select(WriteDependenciesParameter))}) {{ {string.Join($"{Environment.NewLine}", dependencies.Select(WriteDependenciesAssignment))} }} {string.Join($"{Environment.NewLine}", controller.GetEndpoints().Select(x => WriteEndpointImplementation(controller, x)))} }} "); }