/// <summary> /// Expose the registered service to MEF parts as an export. /// </summary> /// <param name="registration">The component being registered.</param> /// <param name="configurationAction">Action on an object that configures the export.</param> /// <returns>A registration allowing registration to continue.</returns> public static IRegistrationBuilder <TLimit, TActivatorData, TSingleRegistrationStyle> Exported <TLimit, TActivatorData, TSingleRegistrationStyle>(this IRegistrationBuilder <TLimit, TActivatorData, TSingleRegistrationStyle> registration, Action <ExportConfigurationBuilder> configurationAction) where TSingleRegistrationStyle : SingleRegistrationStyle { if (registration == null) { throw new ArgumentNullException(nameof(registration)); } if (configurationAction == null) { throw new ArgumentNullException(nameof(configurationAction)); } var configuration = new ExportConfigurationBuilder(); configurationAction(configuration); registration.OnRegistered(e => AttachExport(e.ComponentRegistryBuilder, e.ComponentRegistration, configuration)); return(registration); }
private static void AttachExport(IComponentRegistryBuilder registry, IComponentRegistration registration, ExportConfigurationBuilder exportConfiguration) { var contractService = new ContractBasedService(exportConfiguration.ContractName, exportConfiguration.ExportTypeIdentity); var rb = RegistrationBuilder.ForDelegate((c, p) => { var ctx = c.Resolve <IComponentContext>(); return(new Export( new ExportDefinition(exportConfiguration.ContractName, exportConfiguration.Metadata), () => ctx.ResolveComponent(new ResolveRequest(contractService, registration, Array.Empty <Parameter>())))); }) .As(contractService) .ExternallyOwned() .WithMetadata((IEnumerable <KeyValuePair <string, object> >)exportConfiguration.Metadata); registry.Register(rb.CreateRegistration()); }
static void AttachExport(IComponentRegistry registry, IComponentRegistration registration, ExportConfigurationBuilder exportConfiguration) { var contractService = new ContractBasedService(exportConfiguration.ContractName, exportConfiguration.ExportTypeIdentity); var rb = RegistrationBuilder.ForDelegate((c, p) => { var ctx = c.Resolve <IComponentContext>(); return(new Export( new ExportDefinition(exportConfiguration.ContractName, exportConfiguration.Metadata), () => ctx.ResolveComponent(registration, new Parameter[0]))); }) .As(contractService) .ExternallyOwned() .WithMetadata(exportConfiguration.Metadata); registry.Register(rb.CreateRegistration()); }