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);
        }
Exemplo n.º 2
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);
        }