Exemple #1
0
        public ActionResult ResolveService()
        {
            string      serviceAddress = Request["serviceAddress"];
            ServiceInfo serviceInfo    = new ServiceInfo
            {
                ServiceAddress = serviceAddress,
                ContractInfos  = new List <ServiceContractInfo>()
            };

            DynamicProxyFactory factory = new DynamicProxyFactory(serviceAddress);
            //get contracts
            var contracts = factory.Contracts;

            foreach (ContractDescription contract in contracts)
            {
                var contractInfo = new ServiceContractInfo
                {
                    ContractName   = contract.Name,
                    OperationInfos = new List <ContractOperationInfo>()
                };
                serviceInfo.ContractInfos.Add(contractInfo);
                //get methods in each contract
                var operations = contract.Operations;
                foreach (OperationDescription operation in operations)
                {
                    try
                    {
                        var operationInfo = new ContractOperationInfo
                        {
                            OperationName  = operation.Name,
                            ParameterInfos = new List <OperationParameterInfo>()
                        };
                        contractInfo.OperationInfos.Add(operationInfo);
                        //get parameters in each method
                        Type contractType = factory.ProxyAssembly.GetType(contract.Name);

                        var method = contractType.GetMethod(operation.Name);
                        if (method != null)
                        {
                            var parameters = method.GetParameters();

                            foreach (ParameterInfo parameter in parameters)
                            {
                                operationInfo.ParameterInfos.Add(new OperationParameterInfo
                                {
                                    ParameterName = parameter.Name,
                                    ParameterType = parameter.ParameterType.ToString()
                                });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            return(Json(serviceInfo));
        }
Exemple #2
0
        public List <ServiceContractInfo> GetServicesInfo()
        {
            List <Data.ServiceContractInfo> infos = new List <ServiceContractInfo>();

            foreach (BaseService service in _services.Values)
            {
                ServiceContractInfo info = new ServiceContractInfo()
                {
                    ServiceName = service.GetServiceName()
                };
                info.OperationsList.AddRange(service.GetOperationsList().OrderBy(s => s));
                infos.Add(info);
            }

            return(infos);
        }
        /// <summary>
        /// Extension method that Wraps an existing JsonRpcClient object with an explicit
        ///  interface definition (Service Contract)
        /// </summary>
        /// <typeparam name="T">The type of the interface (Service Contract).</typeparam>
        /// <param name="client">A JSON-RPC 2.0 service client</param>
        /// <returns></returns>
        public static T AsServiceContract <T>(this JsonRpcClient client)
            where T : class
        {
            _ = client ?? throw new ArgumentNullException(nameof(client));

            var svcCntr = _svcCtrInfoDict.Value.GetOrAdd(typeof(T), svcCntrType => new Lazy <ServiceContractInfo>(() =>
            {
                if (!svcCntrType.IsInterface)
                {
                    throw new InvalidOperationException($"{svcCntrType.Name}: is not an interface.");
                }

                var res = new ServiceContractInfo
                {
                    OperContracts = svcCntrType.GetMethodsFromInterface().Select(mi => new OperationContractInfo
                    {
                        MethodInfo = mi,
                        MethodName = mi.GetCustomAttribute <AliasAttribute>()?.Name ?? mi.Name,
                        RpcName    = mi.GetCustomAttribute <JsonRpcNameAttribute>()?.Value
                    }).Where(x => x.RpcName != null).ToArray(),
                    Disposable = typeof(IDisposable).IsAssignableFrom(svcCntrType)
                };

                if (res.OperContracts.Count == 0)
                {
                    throw new InvalidOperationException($"{svcCntrType.Name}: no operation contracts found.");
                }

                return(res);
            })).Value;

            IDictionary <string, object> expando = new ExpandoObject();

            foreach (var oc in svcCntr.OperContracts)
            {
                expando.Add(oc.MethodName, GetOrAddMapFunc(oc.MethodInfo, oc.RpcName, client));
            }

            if (svcCntr.Disposable)
            {
                expando.Add("Dispose", new Action(() => client.Dispose()));
            }

            return(expando.ActLike <T>());
        }