public ServiceChannel(string name, ServiceProvider provider, DataFormatter formatter)
     : this(name, provider)
 {
     Formatter = formatter;
 }
 public ChannelMessageFormatter(DataFormatter dataFormatter)
 {
     Formatter = new DataFormatter();
 }
        internal void Receive_3_InvokeMethod(CommunicationReceiveEventArgs e)
        {
            byte[]         data;
            ServiceChannel channel;
            int            methodIndex;

            object[]         args;
            BinaryDataReader reader = new BinaryDataReader(e.Data);
            var channelName         = reader.ReadString().ToLower().Trim();

            channel = ChannelBuffer.Keys.SingleOrDefault(t => t.Name.ToLower().Trim() == channelName);
            if (channel == null)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "ChannelNotExist");
                Client.Send(data, e.Head);
                return;
            }
            methodIndex = reader.ReadInt32();
            if (methodIndex >= channel.Provider.ClientOperations.Count)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "MethodNotExist");
                Client.Send(data, e.Head);
                return;
            }
            var method    = channel.Provider.ClientOperations[methodIndex];
            var parameter = method.GetParameters();

            args = new object[reader.ReadByte()];
            for (int i = 0; i < args.Length; i++)
            {
                if (channel.Formatter != null)
                {
                    args[i] = channel.Formatter.Deserialize(parameter[i].ParameterType, reader.ReadBytes());
                }
                else
                {
                    args[i] = DataFormatter.Deserialize(parameter[i].ParameterType, reader.ReadBytes());
                }
            }

            var instance = ChannelBuffer[channel].GetType().GetMethod("GetChannel").Invoke(ChannelBuffer[channel], null);

            if (instance == null)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "InstanceCreateFailed");
                Client.Send(data, e.Head);
                return;
            }
            object result;

            try
            {
                result = method.Invoke(instance, args);
            }
            catch (ArgumentException)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "MethodParametersError");
                Client.Send(data, e.Head);
                return;
            }
            catch (Exception)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "MethodRuntimeError");
                Client.Send(data, e.Head);
                return;
            }
            data = MessageFormatter.FromChannelMethodResult(result, null);
            Client.Send(data, e.Head);
        }