示例#1
0
            public ValueTask <T?> GetProxyAsync <T>(ServiceRpcDescriptor descriptor, ServiceActivationOptions options, CancellationToken cancellationToken) where T : class
            {
                var pipePair = FullDuplexStream.CreatePipePair();

                var clientConnection = descriptor
                                       .WithTraceSource(_services.ServiceProvider.TraceSource)
                                       .ConstructRpcConnection(pipePair.Item2);

                Contract.ThrowIfFalse(options.ClientRpcTarget is null == descriptor.ClientInterface is null);

                if (descriptor.ClientInterface != null)
                {
                    Contract.ThrowIfNull(options.ClientRpcTarget);
                    clientConnection.AddLocalRpcTarget(options.ClientRpcTarget);
                }

                // Clear RPC target so that the server connection is forced to create a new proxy for the callback
                // instead of just invoking the callback object directly (this emulates the product that does
                // not serialize the callback object over).
                options.ClientRpcTarget = null;

                // Creates service instance and connects it to the pipe.
                // We don't need to store the instance anywhere.
                _ = _services.CreateBrokeredService(descriptor, pipePair.Item1, options);

                clientConnection.StartListening();

                return(ValueTaskFactory.FromResult((T?)clientConnection.ConstructRpcClient <T>()));
            }
        public async Task ProfferServicesAsync_WithAuthorizingBrokeredServiceFactoryService_ProffersService(
            ServiceRpcDescriptor serviceDescriptor,
            Type serviceType)
        {
            using (await NuGetBrokeredServiceFactory.ProfferServicesAsync(this))
            {
                Assert.True(
                    _authorizingServiceFactories.TryGetValue(
                        serviceDescriptor,
                        out AuthorizingBrokeredServiceFactory factory));

                using (var authorizationServiceClient = new AuthorizationServiceClient(new MockAuthorizationService()))
                {
                    object service = await factory(
                        serviceDescriptor.Moniker,
                        default(ServiceActivationOptions),
                        new MockServiceBroker(),
                        authorizationServiceClient,
                        CancellationToken.None);

                    using (service as IDisposable)
                    {
                        Assert.IsType(serviceType, service);
                    }
                }
            }
        }
            public async ValueTask <T?> GetProxyAsync <T>(ServiceRpcDescriptor serviceDescriptor, ServiceActivationOptions options = default, CancellationToken cancellationToken = default)
                where T : class
            {
                if (this.container.profferedServices.TryGetValue(serviceDescriptor.Moniker, out object?factory))
                {
                    if (factory is BrokeredServiceFactory fac1)
                    {
                        T?service = (T?) await fac1(serviceDescriptor.Moniker, options, this, cancellationToken).ConfigureAwait(false);

                        if (service is object)
                        {
                            return(serviceDescriptor.ConstructLocalProxy <T>(service));
                        }
                    }
                    else if (factory is AuthorizingBrokeredServiceFactory fac2)
                    {
#pragma warning disable CA2000 // Dispose objects before losing scope
                        T?service = (T?) await fac2(serviceDescriptor.Moniker, options, this, new AuthorizationServiceClient(new MockAuthorizationService()), cancellationToken).ConfigureAwait(false);

#pragma warning restore CA2000 // Dispose objects before losing scope
                        if (service is object)
                        {
                            return(serviceDescriptor.ConstructLocalProxy <T>(service));
                        }
                    }
                    else
                    {
                        throw Assumes.NotReachable();
                    }
                }

                return(default);
示例#4
0
 public ValueTask <T> GetProxyAsync <T>(
     ServiceRpcDescriptor serviceDescriptor,
     ServiceActivationOptions options    = default,
     CancellationToken cancellationToken = default)
     where T : class
 {
     throw new NotImplementedException();
 }
示例#5
0
        /// <summary>
        /// Use to perform a callback from ServiceHub process to an arbitrary brokered service hosted in the original process (usually devenv).
        /// </summary>
        public static async ValueTask <TResult> InvokeServiceAsync <TResult>(
            ServiceBrokerClient client,
            ServiceRpcDescriptor serviceDescriptor,
            Func <RemoteCallback <T>, CancellationToken, ValueTask <TResult> > invocation,
            CancellationToken cancellationToken)
        {
            ServiceBrokerClient.Rental <T> rental;
            try
            {
                rental = await client.GetProxyAsync <T>(serviceDescriptor, cancellationToken).ConfigureAwait(false);
            }
            catch (ObjectDisposedException e)
            {
                // When a connection is dropped ServiceHub's ServiceManager disposes the brokered service, which in turn disposes the ServiceBrokerClient.
                cancellationToken.ThrowIfCancellationRequested();
                throw new OperationCanceledIgnoringCallerTokenException(e);
            }

            Contract.ThrowIfNull(rental.Proxy);
            var callback = new RemoteCallback <T>(rental.Proxy);

            return(await invocation(callback, cancellationToken).ConfigureAwait(false));
        }
        public IDisposable Proffer(ServiceRpcDescriptor serviceDescriptor, AuthorizingBrokeredServiceFactory factory)
        {
            _authorizingServiceFactories.Add(serviceDescriptor, factory);

            return(null);
        }
 /// <inheritdoc/>
 public IDisposable Proffer(ServiceRpcDescriptor serviceDescriptor, AuthorizingBrokeredServiceFactory factory)
 {
     this.profferedServices.Add(serviceDescriptor.Moniker, factory);
     return(new DisposableAction(this, serviceDescriptor.Moniker));
 }