diff --git a/src/Autofac/Builder/RegistrationBuilder{TLimit,TActivatorData,TRegistrationStyle}.cs b/src/Autofac/Builder/RegistrationBuilder{TLimit,TActivatorData,TRegistrationStyle}.cs index cf2ed5f15..d4c6fab29 100644 --- a/src/Autofac/Builder/RegistrationBuilder{TLimit,TActivatorData,TRegistrationStyle}.cs +++ b/src/Autofac/Builder/RegistrationBuilder{TLimit,TActivatorData,TRegistrationStyle}.cs @@ -241,11 +241,32 @@ public RegistrationBuilder(Service defaultService, TActivatorData activatorData, /// A registration builder allowing further configuration of the component. public IRegistrationBuilder As(params Type[] services) { - return As(services.Select(t => - t.FullName != null - ? new TypedService(t) - : new TypedService(t.GetGenericTypeDefinition())) - .Cast().ToArray()); + // Issue #919: Use arrays and iteration rather than LINQ to reduce memory allocation. + Service[] argArray = new Service[services.Length]; + for (int i = 0; i < services.Length; i++) + { + if (services[i].FullName != null) + { + argArray[i] = new TypedService(services[i]); + } + else + { + argArray[i] = new TypedService(services[i].GetGenericTypeDefinition()); + } + } + + return As(argArray); + } + + /// + /// Configure a single service that the component will provide. + /// + /// Service type to expose. + /// A registration builder allowing further configuration of the component. + public IRegistrationBuilder As(Type service) + { + // Issue #919: Avoid allocating the array for params if there's only one item. + return As(new TypedService(service)); } /// @@ -262,6 +283,20 @@ public RegistrationBuilder(Service defaultService, TActivatorData activatorData, return this; } + /// + /// Configure a single service that the component will provide. + /// + /// Service to expose. + /// A registration builder allowing further configuration of the component. + public IRegistrationBuilder As(Service service) + { + if (service == null) throw new ArgumentNullException(nameof(service)); + + RegistrationData.AddService(service); + + return this; + } + /// /// Provide a textual name that can be used to retrieve the component. ///