Example #1
0
 public ContractMetadata(Type contract)
 {
     Contract       = contract ?? throw new ArgumentNullException(nameof(contract));
     Session        = BoltFramework.SessionMetadata.Resolve(contract);
     NormalizedName = BoltFramework.GetNormalizedContractName(contract).ConvertToString();
     _actions       = BoltFramework.ValidateContract(contract).Select(a => BoltFramework.ActionMetadata.Resolve(a)).ToArray();
 }
        public void Resolve_Ok(string actionName)
        {
            var contract = BoltFramework.GetContract(typeof(IContract1));

            ActionResolver resolver = new ActionResolver();

            Assert.NotNull(resolver.Resolve(typeof(IContract1), actionName));
        }
Example #3
0
 protected virtual ServerActionContext CreateContext(MethodInfo action)
 {
     return(new ServerActionContext
     {
         HttpContext = new DefaultHttpContext(),
         Action = BoltFramework.GetContract(typeof(TContract)).GetAction(action),
         Contract = BoltFramework.GetContract(typeof(TContract))
     });
 }
        public void Resolve_Ok(string contractName)
        {
            IContractInvokerSelector resolver = new ContractInvokerSelector();

            ContractInvoker invoker = new ContractInvoker(new ServerRuntimeConfiguration());

            invoker.Contract = BoltFramework.GetContract(typeof(IContract));

            Assert.NotNull(resolver.Resolve(new[] { invoker }, contractName.AsReadOnlySpan()));
        }
Example #5
0
        protected virtual SessionContractMetadata Analyze(Type contract)
        {
            BoltFramework.ValidateContract(contract);

            Type[] allInterfaces = new[] { contract }.Concat(contract.GetTypeInfo().ImplementedInterfaces).ToArray();

            var initSession    = FindMethod(allInterfaces, nameof(InitSessionAttribute)) ?? InitSessionAction;
            var destroySession = FindMethod(allInterfaces, nameof(DestroySessionAttribute)) ?? DestroySessionAction;

            return(new SessionContractMetadata(contract, BoltFramework.ActionMetadata.Resolve(initSession), BoltFramework.ActionMetadata.Resolve(destroySession)));
        }
Example #6
0
 public ActionMetadata(MethodInfo action, ParameterMetadata[] parameters, Type resultType)
 {
     Action                    = action;
     Parameters                = new ReadOnlyCollection <ParameterMetadata>(parameters);
     ResultType                = resultType;
     Timeout                   = TimeSpan.Zero;
     IsAsynchronous            = typeof(Task).GetTypeInfo().IsAssignableFrom(action.ReturnType.GetTypeInfo());
     NormalizedName            = BoltFramework.NormalizeActionName(Name.AsReadOnlySpan()).ConvertToString();
     HasSerializableParameters = Parameters.Any(p => p.IsSerializable);
     CancellationTokenIndex    = GetCancellationTokenIndex();
 }
Example #7
0
        public static IContractInvoker Use(
            this IBoltRouteHandler bolt,
            Type contract,
            IInstanceProvider instanceProvider,
            Action <ConfigureContractContext> configure = null,
            ServerRuntimeConfiguration configuration    = null)
        {
            if (contract == null)
            {
                throw new ArgumentNullException(nameof(contract));
            }

            if (bolt == null)
            {
                throw new ArgumentNullException(nameof(bolt));
            }

            if (instanceProvider == null)
            {
                throw new ArgumentNullException(nameof(instanceProvider));
            }

            var factory = bolt.ApplicationServices.GetRequiredService <IContractInvokerFactory>();

            configuration = configuration ?? new ServerRuntimeConfiguration(bolt.Configuration);

            IContractInvoker invoker = factory.Create(BoltFramework.GetContract(contract), instanceProvider, configuration);
            IPipeline <ServerActionContext> pipeline = null;

            if (configure != null)
            {
                ConfigureContractContext ctxt = new ConfigureContractContext(invoker, bolt.ApplicationServices);
                configure.Invoke(ctxt);

                if (ctxt.Middlewares.Any())
                {
                    pipeline = bolt.ApplicationServices.GetRequiredService <IServerPipelineBuilder>().Build(ctxt.Middlewares);
                }
            }

            if (pipeline != null)
            {
                invoker.Pipeline = pipeline;
            }
            else if (invoker.Pipeline == null)
            {
                // build default pipeline
                invoker.Pipeline = bolt.ApplicationServices.GetRequiredService <IServerPipelineBuilder>().Build();
            }
            bolt.Add(invoker);

            return(invoker);
        }
Example #8
0
        public bool IsMatch(ReadOnlySpan <char> name)
        {
            if (NormalizedName.AsReadOnlySpan().AreEqualInvariant(name))
            {
                return(true);
            }

            if (NormalizedName.AsReadOnlySpan().AreEqualInvariant(BoltFramework.NormalizeActionName(name)))
            {
                return(true);
            }

            return(false);
        }
Example #9
0
        public virtual TContract Build <TContract>() where TContract : class
        {
            IClientPipeline pipeline = BuildPipeline();
            TContract       proxy    = _configuration.ProxyFactory.CreateProxy <TContract>(pipeline);

            if (proxy is IContractProvider)
            {
                pipeline.Validate((proxy as IContractProvider).Contract);
            }
            else
            {
                pipeline.Validate(BoltFramework.GetContract(typeof(TContract)));
            }

            return(proxy);
        }
Example #10
0
        public T CreateProxy <T>(IClientPipeline pipeline) where T : class
        {
            ContractMetadata contract = BoltFramework.GetContract(typeof(T));
            var interceptor           = new ChannelInterceptor();
            var options = new ProxyGenerationOptions
            {
                BaseTypeForInterfaceProxy = _baseProxy
            };

            ProxyMetadata metadata = _metadatas.GetOrAdd(typeof(T), v => new ProxyMetadata(_baseProxy));
            var           proxy    = _generator.CreateInterfaceProxyWithoutTarget <T>(
                options,
                interceptor);

            ProxyBase proxyBase = (ProxyBase)(object)proxy;

            proxyBase.Contract = contract;
            proxyBase.Pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline));

            interceptor.Proxy    = proxyBase;
            interceptor.Metadata = metadata;
            return(proxy);
        }
Example #11
0
 public static MethodInfo Resolve(this IActionResolver resolver, Type type, string name)
 {
     return(resolver.Resolve(BoltFramework.GetContract(type), name.AsReadOnlySpan())?.Action);
 }
Example #12
0
        public IContractInvoker Resolve(ReadOnlySpan <IContractInvoker> contracts, ReadOnlySpan <char> contractName)
        {
            foreach (IContractInvoker contract in contracts)
            {
                if (contract.Contract.NormalizedName.AsReadOnlySpan().AreEqualInvariant(contractName))
                {
                    return(contract);
                }
            }

            foreach (IContractInvoker contract in contracts)
            {
                if (contract.Contract.NormalizedName.AsReadOnlySpan().AreEqualInvariant(BoltFramework.NormalizeContractName(contractName)))
                {
                    return(contract);
                }
            }

            return(null);
        }
Example #13
0
 public ProxyBase(Type contract, IClientPipeline pipeline)
 {
     State     = ProxyState.Ready;
     _contract = BoltFramework.GetContract(contract);
     _pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline));
 }
Example #14
0
        public void StringContentResultType_ShouldThrow()
        {
            TestStreamingMiddleware middleware = new TestStreamingMiddleware();

            Assert.Throws <ContractViolationException>(() => middleware.Validate(BoltFramework.GetContract(typeof(IInvalid3))));
        }
Example #15
0
        public void MethodWithCustomArgument_ShouldThrow()
        {
            TestStreamingMiddleware middleware = new TestStreamingMiddleware();

            Assert.Throws <ContractViolationException>(() => middleware.Validate(BoltFramework.GetContract(typeof(IInvalid2))));
        }
Example #16
0
 public void Validate_Ok()
 {
     new TestStreamingMiddleware().Validate(BoltFramework.GetContract(typeof(IValid)));
 }
Example #17
0
 public void Validate_ContractWithSameActions_Throws()
 {
     Assert.Throws <InvalidOperationException>(() => BoltFramework.ValidateContract(typeof(IInvalidInterface)));
 }
Example #18
0
 public void NormalizeActionName(string input, string expected)
 {
     Assert.Equal(expected, BoltFramework.NormalizeActionName(input.AsReadOnlySpan()).ConvertToString());
 }
Example #19
0
 public void ValidateInvalidContract_Throws()
 {
     Assert.Throws <InvalidOperationException>(() => BoltFramework.ValidateContract(typeof(BoltFrameworkTest)));
 }