public IOwned <RpcObjectRef <TService> > PublishInstance <TService>(IOwned <TService> serviceInstance) where TService : class { if (serviceInstance is null) { throw new ArgumentNullException(nameof(serviceInstance)); } var allServices = RpcBuilderUtil.GetAllServices(serviceInstance.Value.GetType(), true); this.TryRegisterServiceDefinitions(allServices, null); var connectionInfo = this.RetrieveConnectionInfo(); lock (this.syncRoot) { var serviceInstanceId = RpcObjectId.NewId(); var publishedServices = this.PublishInstanceCore_Locked(allServices, serviceInstance, serviceInstanceId, false); Func <ValueTask> disposeAction = () => this.UnpublishInstanceAsync(serviceInstanceId); return(OwnedObject.Create(new RpcObjectRef <TService>( connectionInfo, serviceInstanceId, publishedServices.ToArray()), disposeAction)); } }
public static IOwned <RpcObjectRef <TService> > PublishInstance <TService>(this IRpcServer server, TService serviceInstance, bool takeOwnership = false) where TService : class { if (server is null) { throw new ArgumentNullException(nameof(server)); } return(server.ServicePublisher.PublishInstance(takeOwnership ? OwnedObject.Create(serviceInstance) : OwnedObject.CreateUnowned(serviceInstance))); }
public IOwned <RpcSingletonRef <TService> > PublishSingleton <TService>(Func <IServiceProvider?, IOwned <TService> > factory) where TService : class { this.PublishSingletonFactoryCore(factory); return(OwnedObject.Create(new RpcSingletonRef <TService>( this.RetrieveConnectionInfo()), () => this.UnpublishSingletonAsync <TService>())); }
public static IOwned <RpcSingletonRef <TService> > PublishSingleton <TService>(this IRpcServicePublisher servicePublisher, Func <TService> factory) where TService : class { if (servicePublisher is null) { throw new ArgumentNullException(nameof(servicePublisher)); } IOwned <TService> CreateActivatedService(IServiceProvider?_) => OwnedObject.Create(factory()); return(servicePublisher.PublishSingleton(CreateActivatedService)); }
/// <summary> /// Publishes an RPC service instance using an instance factory. /// </summary> /// <typeparam name="TService">The type of the published instance.</typeparam> /// <param name="factory">A factory function that should create the service instance specified by the <see cref="RpcObjectId"/>. If the created /// instance implements <see cref="IDisposable"/> the instance will be disposed when the RPC call has finished. /// </param> /// <returns>A scoped object including the <see cref="RpcObjectRef"/> identifying the published instance. The scoped object will unpublish /// the service instance when disposed.</returns> public static IOwned <RpcObjectRef <TService> > PublishInstance <TService>(this IRpcServicePublisher servicePublisher, Func <RpcObjectId, TService> factory) where TService : class { if (servicePublisher is null) { throw new ArgumentNullException(nameof(servicePublisher)); } IOwned <TService> CreateActivatedService(IServiceProvider?services, RpcObjectId objectId) { return(OwnedObject.Create(factory(objectId))); } return(servicePublisher.PublishInstance(CreateActivatedService)); }
internal static IOwned <TService> CreateActivatedService(IServiceProvider?services) { if (services == null) { throw new RpcFailureException(RpcFailure.RemoteDefinitionError, "An IServiceProvider must be supplied when services are published using IServiceProvider factories."); } TService service = services.GetService <TServiceImpl>(); if (service != null) { return(OwnedObject.CreateUnowned(service)); } service = (TService)Factory.Value(services, Array.Empty <object>()); return(OwnedObject.Create(service)); }
public IOwned <RpcObjectRef <TService> > PublishInstance <TService>(Func <IServiceProvider?, RpcObjectId, IOwned <TService> > factory) where TService : class { var allServices = RpcBuilderUtil.GetAllServices(typeof(TService), RpcServiceDefinitionSide.Server, true); this.TryRegisterServiceDefinitions(allServices, null); var connectionInfo = this.RetrieveConnectionInfo(); lock (this.syncRoot) { var objectId = RpcObjectId.NewId(); var publishedServices = this.PublishInstanceFactoryCore_Locked(allServices, objectId, factory); return(OwnedObject.Create(new RpcObjectRef <TService>( connectionInfo, objectId, publishedServices.ToArray()), () => this.UnpublishInstanceAsync(objectId))); } }
public IOwned <RpcSingletonRef <TService> > PublishSingleton <TService>(IOwned <TService> singletonService) where TService : class { if (singletonService == null) { throw new ArgumentNullException(nameof(singletonService)); } var allServices = RpcBuilderUtil.GetAllServices(typeof(TService), false); this.TryRegisterServiceDefinitions(allServices, null); var publishedServices = this.VerifyPublishedServices(allServices); var connectionInfo = this.RetrieveConnectionInfo(); lock (this.syncRoot) { var instanceKey = new InstanceKey(singletonService.Value); foreach (var serviceType in publishedServices.ServiceTypes) { if (this.singletonServiceTypeToServiceImpl.ContainsKey(serviceType) || this.singletonServiceTypeToFactory.ContainsKey(serviceType)) { throw new RpcDefinitionException($"A singleton for the type '{serviceType}' has already been published."); } } this.singletonTypeToPublishedServices.Add(typeof(TService), publishedServices); var publishedInstance = new PublishedInstance(singletonService); foreach (var serviceType in publishedServices.ServiceTypes) { this.singletonServiceTypeToServiceImpl.Add(serviceType, publishedInstance); } } return(OwnedObject.Create(new RpcSingletonRef <TService>( connectionInfo), () => this.UnpublishSingletonAsync <TService>())); }