Example #1
0
        public TService Create <TService>(ushort spacialMessageId = 0) where TService : class
        {
            var serviceType = typeof(TService);
            var cacheKey    = $"{serviceType.FullName}${spacialMessageId}";

            if (TYPE_CACHE.TryGetValue(cacheKey, out var cacheService))
            {
                return((TService)cacheService);
            }

            var service = serviceType.GetCustomAttribute(typeof(RpcServiceAttribute), false);

            if (service == null)
            {
                throw new InvalidOperationException($"Miss RpcServiceAttribute at {serviceType}");
            }
            var sAttr = service as RpcServiceAttribute;

            var serviceIdentity = InternalHelper.FormatServiceIdentity(sAttr.GroupName, sAttr.ServiceId, spacialMessageId);
            // $"{sAttr.ServiceId}${spacialMessageId};{sAttr.GroupName}";
            var servicePath = InternalHelper.FormatServicePath(sAttr.ServiceId, spacialMessageId);

            var isLocal = IsLocalCall(serviceIdentity);

            TService proxy;

            if (isLocal)
            {
                var actor = ActorLocator.LocateServiceActor(servicePath);

                if (!(actor is TService realService))
                {
                    throw new InvalidOperationException($"{serviceType.FullName} has no implementation class");
                }
                proxy = this._generator.CreateInterfaceProxyWithTarget(realService, LocalInvokeInterceptor);
                TYPE_CACHE.TryAdd(cacheKey, proxy);
            }
            else
            {
                proxy = this._generator.CreateInterfaceProxyWithoutTarget <TService>(RemoteCallInterceptor);
                TYPE_CACHE.TryAdd(cacheKey, proxy);
            }

            return(proxy);
        }