diff --git a/Modules/Webservices/Extensions.cs b/Modules/Webservices/Extensions.cs index 97440fca..846d1454 100644 --- a/Modules/Webservices/Extensions.cs +++ b/Modules/Webservices/Extensions.cs @@ -1,6 +1,11 @@ using System.Diagnostics.CodeAnalysis; +using GenHTTP.Api.Infrastructure; + +using GenHTTP.Modules.Conversion.Providers; using GenHTTP.Modules.Layouting.Provider; +using GenHTTP.Modules.Reflection.Injectors; +using GenHTTP.Modules.Webservices.Provider; namespace GenHTTP.Modules.Webservices { @@ -17,9 +22,11 @@ public static class Extensions /// /// The type of the resource to be added /// The path the resource should be available at - public static LayoutBuilder AddService<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder layout, string path) where T : new() + /// Optionally the injectors to be used by this service + /// Optionally the formats to be used by this service + public static LayoutBuilder AddService<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder layout, string path, IBuilder? injectors = null, IBuilder? formats = null) where T : new() { - return layout.Add(path, ServiceResource.From()); + return layout.Add(path, ServiceResource.From().Configured(injectors, formats)); } /// @@ -28,9 +35,26 @@ public static class Extensions /// /// The path the resource should be available at /// The webservice resource instance - public static LayoutBuilder AddService(this LayoutBuilder layout, string path, object instance) + /// Optionally the injectors to be used by this service + /// Optionally the formats to be used by this service + public static LayoutBuilder AddService(this LayoutBuilder layout, string path, object instance, IBuilder? injectors = null, IBuilder? formats = null) + { + return layout.Add(path, ServiceResource.From(instance).Configured(injectors, formats)); + } + + private static ServiceResourceBuilder Configured(this ServiceResourceBuilder builder, IBuilder? injectors = null, IBuilder? formats = null) { - return layout.Add(path, ServiceResource.From(instance)); + if (injectors != null) + { + builder.Injectors(injectors); + } + + if (formats != null) + { + builder.Formats(formats); + } + + return builder; } } diff --git a/Testing/Acceptance/Modules/Webservices/ExtensionTests.cs b/Testing/Acceptance/Modules/Webservices/ExtensionTests.cs new file mode 100644 index 00000000..b65a5dd2 --- /dev/null +++ b/Testing/Acceptance/Modules/Webservices/ExtensionTests.cs @@ -0,0 +1,58 @@ +using System.Net; +using System.Threading.Tasks; + +using GenHTTP.Modules.Conversion; +using GenHTTP.Modules.Layouting; +using GenHTTP.Modules.Reflection; +using GenHTTP.Modules.Webservices; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace GenHTTP.Testing.Acceptance.Modules.Webservices +{ + + [TestClass] + public sealed class ExtensionTests + { + + #region Supporting data structures + + public class TestService + { + + [ResourceMethod] + public int DoWork() => 42; + + } + + #endregion + + #region Tests + + [TestMethod] + public async Task TestConfiguration() + { + var injectors = Injection.Default(); + + var formats = Serialization.Default(); + + var app = Layout.Create() + .AddService("by-type", injectors, formats) + .AddService("by-instance", new TestService(), injectors, formats); + + using var host = TestHost.Run(app); + + using var r1 = await host.GetResponseAsync("/by-type"); + + await r1.AssertStatusAsync(HttpStatusCode.OK); + + using var r2 = await host.GetResponseAsync("/by-instance"); + + await r2.AssertStatusAsync(HttpStatusCode.OK); + } + + #endregion + + } + +}