Example #1
0
        /// <summary>
        /// 生成代理代码
        /// </summary>
        /// <param name="spaceName"></param>
        internal static void GenerateProxy(string spaceName)
        {
            StringBuilder csStr = new StringBuilder();

            csStr.AppendLine(Header("using System.Collections.Generic;", "using SAEA.Common;", "using SAEA.RPC.Consumer;", $"using {spaceName}.Consumer.Model;", $"using {spaceName}.Consumer.Service;"));
            csStr.AppendLine($"namespace {spaceName}.Consumer");
            csStr.AppendLine("{");
            csStr.AppendLine($"{GetSpace(1)}public class RPCServiceProxy");
            csStr.AppendLine(GetSpace(1) + "{");
            csStr.AppendLine(GetSpace(2) + "public event ExceptionCollector.OnErrHander OnErr;");
            csStr.AppendLine(GetSpace(2) + "ServiceConsumer _serviceConsumer;");
            csStr.AppendLine(GetSpace(2) + "public RPCServiceProxy(string uri = \"rpc://127.0.0.1:39654\") : this(uri, 4, 5, 10 * 1000) { }");
            csStr.AppendLine(GetSpace(2) + "public RPCServiceProxy(string uri, int links = 4, int retry = 5, int timeOut = 10 * 1000)");
            csStr.AppendLine(GetSpace(2) + "{");
            csStr.AppendLine(GetSpace(3) + "ExceptionCollector.OnErr += ExceptionCollector_OnErr;");
            csStr.AppendLine(GetSpace(3) + "_serviceConsumer = new ServiceConsumer(new Uri(uri), links, retry, timeOut);");

            var names = RPCMapping.GetServiceNames();

            if (names != null)
            {
                foreach (var name in names)
                {
                    csStr.AppendLine(GetSpace(3) + GetSuffixStr(name) + $" = new {name}(_serviceConsumer);");
                }
            }
            csStr.AppendLine(GetSpace(2) + "}");

            csStr.AppendLine(GetSpace(2) + "private void ExceptionCollector_OnErr(string name, Exception ex)");
            csStr.AppendLine(GetSpace(2) + "{");
            csStr.AppendLine(GetSpace(3) + "OnErr(name, ex);");
            csStr.AppendLine(GetSpace(2) + "}");

            if (names != null)
            {
                foreach (var name in names)
                {
                    var suffixStr = GetSuffixStr(name);

                    csStr.AppendLine(GetSpace(2) + $"{name} {suffixStr};");
                    csStr.AppendLine(GetSpace(2) + $"public {name} {name}");
                    csStr.AppendLine(GetSpace(2) + "{");
                    csStr.AppendLine($"{GetSpace(3)} get{{ return {suffixStr}; }}");
                    csStr.AppendLine(GetSpace(2) + "}");

                    var list = RPCMapping.GetAll(name);
                    if (list != null)
                    {
                        GenerateService(spaceName, name, list);
                    }
                }
            }

            csStr.AppendLine(GetSpace(1) + "}");
            csStr.AppendLine("}");
            _proxyStr = csStr.ToString();
        }
Example #2
0
        /// <summary>
        /// 生成代理代码
        /// </summary>
        /// <param name="spaceName"></param>
        internal static void GenerateProxy(string spaceName)
        {
            StringBuilder csStr = new StringBuilder();

            csStr.AppendLine(Header("using System.Collections.Generic;", "using SAEA.RPC.Consumer;", $"using {spaceName}.Consumer.Model;", $"using {spaceName}.Consumer.Service;"));
            csStr.AppendLine($"namespace {spaceName}.Consumer");
            csStr.AppendLine("{");
            csStr.AppendLine($"{GetSpace(1)}public class RPCServiceProxy");
            csStr.AppendLine(GetSpace(1) + "{");

            csStr.AppendLine(GetSpace(2) + "ServiceConsumer _serviceConsumer;");
            csStr.AppendLine(GetSpace(2) + "public RPCServiceProxy(string uri = \"rpc://127.0.0.1:39654\") : this(new Uri(uri)){}");
            csStr.AppendLine(GetSpace(2) + "public RPCServiceProxy(Uri uri)");
            csStr.AppendLine(GetSpace(2) + "{");

            csStr.AppendLine(GetSpace(3) + "_serviceConsumer = new ServiceConsumer(uri);");

            var names = RPCMapping.GetServiceNames();

            if (names != null)
            {
                foreach (var name in names)
                {
                    csStr.AppendLine(GetSpace(3) + GetSuffixStr(name) + $" = new {name}(_serviceConsumer);");
                }
            }
            csStr.AppendLine(GetSpace(2) + "}");

            if (names != null)
            {
                foreach (var name in names)
                {
                    var suffixStr = GetSuffixStr(name);

                    csStr.AppendLine(GetSpace(2) + $"{name} {suffixStr};");
                    csStr.AppendLine(GetSpace(2) + $"public {name} {name}");
                    csStr.AppendLine(GetSpace(2) + "{");
                    csStr.AppendLine($"{GetSpace(3)} get{{ return {suffixStr}; }}");
                    csStr.AppendLine(GetSpace(2) + "}");

                    var list = RPCMapping.GetAll(name);
                    if (list != null)
                    {
                        GenerateService(spaceName, name, list);
                    }
                }
            }

            csStr.AppendLine(GetSpace(1) + "}");
            csStr.AppendLine("}");
            _proxyStr = csStr.ToString();
        }