Пример #1
0
        private async ValueTask <AnyValue> InvokeInternal(string servicePath, InvokeArgs args, bool byClient, int msgId)
        {
            if (string.IsNullOrEmpty(servicePath))
            {
                throw new ArgumentNullException(nameof(servicePath));
            }

            var span     = servicePath.AsMemory();
            var firstDot = span.Span.IndexOf('.');
            var lastDot  = span.Span.LastIndexOf('.');

            if (firstDot == lastDot)
            {
                throw new ArgumentException(nameof(servicePath));
            }
            var app     = span.Slice(0, firstDot);
            var service = servicePath.AsMemory(firstDot + 1, lastDot - firstDot - 1);
            var method  = servicePath.AsMemory(lastDot + 1);

            if (app.Span.SequenceEqual(appbox.Consts.SYS.AsSpan()))
            {
                if (Runtime.SysServiceContainer.TryGet(service, out IService serviceInstance))
                {
                    try
                    {
                        return(await InvokeSysAsync(serviceInstance, servicePath, method, args));
                    }
                    finally
                    {
                        args.ReturnBuffer(); //注意归还缓存块
                    }
                }
            }

            //非系统服务则包装为InvokeRequire转发至子进程处理
            var tcs     = invokeTasksPool.Allocate();
            var require = new InvokeRequire(byClient ? InvokeSource.Client : InvokeSource.Host,
                                            byClient ? InvokeProtocol.Json : InvokeProtocol.Bin,
                                            tcs.GCHandlePtr, servicePath, args, msgId, RuntimeContext.Current.CurrentSession); //注意传播会话信息

            ChildProcess.AppContainer.Channel.SendMessage(ref require);
            args.ReturnBuffer(); //注意归还缓存块
            var res = await tcs.WaitAsync();

            invokeTasksPool.Free(tcs);
            return(res);
        }