public object GetInstance(ServiceChannel channel, InvokeMethodDelegate invokeMethod, ServiceUser user)
        {
            if (channel.Provider.Mode == ServiceMode.Single)
            {
                return(GetInstance(channel, invokeMethod));
            }
            if (UserInstances[user].ContainsKey(channel.Provider))
            {
                return(UserInstances[user][channel.Provider]);
            }
            Type type;

            try
            {
                type = Builder.GetInstanceType(channel.Provider.Instance, channel.Provider.Contract);
            }
            catch
            {
                return(null);
            }
            object instance = CreateInstance(channel, invokeMethod, type);

            if (channel.Provider.Mode == ServiceMode.PreClient)
            {
                UserInstances[user].Add(channel.Provider, instance);
            }
            return(instance);
        }
        public object GetInstance(ServiceChannel channel, InvokeMethodDelegate invokeMethod)
        {
            if (GlobaInstances.ContainsKey(channel.Provider))
            {
                return(GlobaInstances[channel.Provider]);
            }
            Type type;

            try
            {
                type = Builder.GetInstanceType(channel.Provider.Instance, channel.Provider.Contract);
            }
            catch
            {
                return(null);
            }
            object instance = CreateInstance(channel, invokeMethod, type);

            if (instance == null)
            {
                return(null);
            }
            GlobaInstances.Add(channel.Provider, instance);
            return(instance);
        }
Пример #3
0
 public ServiceContext GetContext(ServiceChannel channel)
 {
     if (!Contexts.ContainsKey(channel))
     {
         Contexts.Add(channel, new ServiceContext(this, channel));
     }
     return(Contexts[channel]);
 }
 public bool RegisterChannel(ServiceChannel channel)
 {
     if (Channels.ContainsKey(channel.Name.ToLower()))
     {
         return(false);
     }
     Channels.Add(channel.Name.ToLower(), channel);
     return(true);
 }
Пример #5
0
        internal object CreateInstance(ServiceChannel channel)
        {
            Type type;

            //try
            //{
            type = Builder.GetInstanceType(channel.Provider.Instance, channel.Provider.Contract);
            //}
            //catch
            //{
            //    return null;
            //}

            InstanceProxy proxy = new InstanceProxy(channel, channel.Provider.ServerOperations.ToList(), InvokeMethod);

            if (Unity.IsRegistered(type))
            {
                return(Unity.GetInstance(type));
            }
            foreach (var constructor in type.GetConstructors())
            {
                var      parameters       = constructor.GetParameters();
                object[] parameterArray   = new object[parameters.Length];
                bool     parameterSuccess = true;
                parameterArray[0] = proxy;
                for (int i = 1; i < parameters.Length; i++)
                {
                    object obj = Unity.GetInstance(parameters[i].ParameterType);
                    if (obj == null)
                    {
                        parameterSuccess = false;
                        break;
                    }
                    parameterArray[i] = obj;
                }
                if (!parameterSuccess)
                {
                    continue;
                }
                try
                {
                    return(Activator.CreateInstance(type, parameterArray));
                }
                catch
                {
                }
            }
            return(null);
        }
Пример #6
0
 public void RegisterChannel(ServiceChannel channel)
 {
     if (Opened)
     {
         throw new InvalidOperationException("服务打开时不能注册服务频道。");
     }
     if (channel.Provider.Mode == ServiceMode.Client)
     {
         throw new NotSupportedException("服务器类型为客户端类型。");
     }
     if (!ChannelManager.RegisterChannel(channel))
     {
         throw new InvalidOperationException("服务频道名称重复。");
     }
 }
Пример #7
0
 public ChannelFactory <T> GetChannelFactory <T>(ServiceChannel channel)
 {
     if (channel == null)
     {
         throw new ArgumentNullException("channel");
     }
     if (channel.Provider.Mode != ServiceMode.Client)
     {
         throw new NotSupportedException("服务类型不是客户端类型。");
     }
     if (typeof(T) != channel.Provider.Contract)
     {
         throw new ArgumentException("T类型与服务契约类型不一样。");
     }
     if (!ChannelBuffer.ContainsKey(channel))
     {
         ChannelBuffer.Add(channel, new ChannelFactory <T>(this, channel));
     }
     return((ChannelFactory <T>)ChannelBuffer[channel]);
 }
Пример #8
0
 public ChannelFactory(ServiceClient client, ServiceChannel channel)
 {
     if (client == null)
     {
         throw new ArgumentNullException("client");
     }
     if (channel == null)
     {
         throw new ArgumentNullException("channel");
     }
     if (channel.Provider.Mode != ServiceMode.Client)
     {
         throw new NotSupportedException("服务类型不是客户端类型。");
     }
     Contract = typeof(T);
     if (Contract != channel.Provider.Contract)
     {
         throw new ArgumentException("T类型与服务契约类型不一样。");
     }
     Client  = client;
     Channel = channel;
 }
        private object CreateInstance(ServiceChannel channel, InvokeMethodDelegate invokeMethod, Type type)
        {
            InstanceProxy proxy = new InstanceProxy(channel, channel.Provider.ClientOperations.ToList(), invokeMethod);

            if (Unity.IsRegistered(type))
            {
                return(Unity.GetInstance(type));
            }
            foreach (var constructor in type.GetConstructors())
            {
                var      parameters       = constructor.GetParameters();
                object[] parameterArray   = new object[parameters.Length];
                bool     parameterSuccess = true;
                parameterArray[0] = proxy;
                for (int i = 1; i < parameters.Length; i++)
                {
                    object obj = Unity.GetInstance(parameters[i].ParameterType);
                    if (obj == null)
                    {
                        parameterSuccess = false;
                        break;
                    }
                    parameterArray[i] = obj;
                }
                if (!parameterSuccess)
                {
                    continue;
                }
                try
                {
                    return(Activator.CreateInstance(type, parameterArray));
                }
                catch
                {
                }
            }
            return(null);
        }
Пример #10
0
 internal InstanceProxy(ServiceChannel channel, List <MethodInfo> methods, InvokeMethodDelegate invokeMethod)
 {
     Channel      = channel;
     Methods      = methods;
     InvokeMethod = invokeMethod;
 }
Пример #11
0
 public ServiceContext(ServiceUser user, ServiceChannel channel)
 {
     User    = user;
     Channel = channel;
     Session = new ServiceSessionState();
 }
        public void ToChannelMethodInvoke(byte[] data, ServiceChannelManager channelManager, out ServiceChannel channel, out int methodIndex, out object[] args)
        {
            BinaryDataReader reader = new BinaryDataReader(data);

            channel = channelManager[reader.ReadString()];
            if (channel == null)
            {
                methodIndex = -1;
                args        = null;
                return;
            }
            methodIndex = reader.ReadInt32();
            if (methodIndex < 0 || methodIndex >= channel.Provider.ServerOperations.Count)
            {
                methodIndex = -1;
                args        = null;
                return;
            }
            var method    = channel.Provider.ServerOperations[methodIndex];
            var parameter = method.GetParameters();

            args = new object[reader.ReadByte()];
            for (int i = 0; i < args.Length; i++)
            {
                args[i] = Formatter.Deserialize(parameter[i].ParameterType, reader.ReadBytes());
            }
        }
Пример #13
0
 internal object InvokeMethod(ServiceChannel channel, MethodInfo method, object[] args)
 {
     return(InvokeMethod(channel.Name, method.Name, channel.Provider.ClientOperations.IndexOf(method), args, method.ReturnType));
 }