Esempio n. 1
0
        protected virtual object ExecuteOnRemoteSideInternal(RemoteSideIDType remoteSideID, RemoteOperationDescriptor rso)
        {
            object ret = null;

            if (remoteSideID == null)
            {
                throw new ArgumentNullException("remoteSideID");
            }

            if (rso == null)
            {
                throw new ArgumentNullException("rso");
            }

            // find appropriate client
            IRemoteSideCommunicationContract contract = null;
            bool lockTaken = false;

            try
            {
                syncRoot.Enter(ref lockTaken);
                communicationContractsByRemoteSideIDDict.TryGetValue(remoteSideID, out contract);
            }
            finally
            {
                if (lockTaken)
                {
                    syncRoot.Exit();
                }
            }

            if (contract != null)
            {
                RemoteRequest req = remoteOperationHandler.CreateRequest(rso);
                req.RemoteID = remoteSideID;

                RemoteResponse resp = null;

                try
                {
                    RequestToRemoteSideStarted(remoteSideID, rso);

                    resp = contract.ExecuteRequest(req);
                }
                finally
                {
                    RequestToRemoteSideFinished(remoteSideID, rso);
                }
                ret = remoteOperationHandler.HandleResponse(resp);
            }
            else
            {
                throw new InvalidOperationException(String.Format("Unknown remote side id: {0}", remoteSideID));
            }

            return(ret);
        }
        public virtual object HandleResponse(RemoteResponse resp)
        {
            if (resp == null)
            {
                throw new ArgumentNullException("resp");
            }

            object ret = null;

            Exception ex = resp.ReturnValue as Exception;

            if (ex != null)
            {
                throw ex;
            }

            ret = resp.ReturnValue;

            return(ret);
        }
Esempio n. 3
0
        public virtual RemoteResponse ExecuteRequest(RemoteRequest request)
        {
            ThrowIfNotInitialized();

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            RemoteResponse ret = null;

            RemoteSideIDType remoteID = request.RemoteID;

            if (remoteID == null)
            {
                throw new InvalidOperationException("Remote call without remote side id!"); //LOCSTR
            }

            var wrapper = remoteSide;
            IRemoteSideCommunicationContract currentContract = wrapper.GetCurrentRemoteSideCommunicationContract();

            try
            {
                bool isNewRemoteSide = false;
                bool lockTaken       = false;
                try
                {
                    syncRoot.Enter(ref lockTaken);

                    RemoteRequestStarted(remoteID, request);

                    IRemoteSideCommunicationContract lastKnownContract = null;
                    if (communicationContractsByRemoteSideIDDict.TryGetValue(remoteID, out lastKnownContract))
                    {
                        // known client comes in again
                        if (lastKnownContract != currentContract)
                        {
                            communicationContractsByRemoteSideIDDict[remoteID] = currentContract;
                            remoteSideIDsByCommunicationContractDict.Remove(lastKnownContract);
                            remoteSideIDsByCommunicationContractDict.Add(currentContract, remoteID);

                            wrapper.Closed  += new EventHandler(Channel_Closed);
                            wrapper.Faulted += new EventHandler(Channel_Faulted);

                            RemoteSideReconnected(remoteID);
                            //Log.Debug("Client {0} reconnected.", remoteID.ToString()); //LOCSTR
                            System.Diagnostics.Debug.WriteLine("Client {0} reconnected.", remoteID.ToString()); //LOCSTR
                        }

                        KnownRemoteSideRequest(remoteID);
                    }
                    else
                    {
                        wrapper.Closed  += new EventHandler(Channel_Closed);
                        wrapper.Faulted += new EventHandler(Channel_Faulted);

                        remoteSideIDsByCommunicationContractDict.Add(currentContract, remoteID);
                        communicationContractsByRemoteSideIDDict.Add(remoteID, currentContract);

                        NewRemoteSideConnected(remoteID);

                        isNewRemoteSide = true;
                    }
                }
                finally
                {
                    if (lockTaken)
                    {
                        syncRoot.Exit();
                    }
                }

                if (isNewRemoteSide)
                {
                    OnRemoteSideConnected(remoteID);
                }

                // process request
                ret = remoteOperationHandler.ExecuteRequest(request, typeof(RemoteCallableTypeAttribute), typeof(RemoteCallableFuncAttribute));
            }
            finally
            {
                RemoteRequestFinished(remoteID, request);
            }

            return(ret);
        }
        public virtual RemoteResponse ExecuteRequest(RemoteRequest request, Type callableTypeAttribute, Type callableFuncAttribute)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (callableTypeAttribute == null)
            {
                throw new ArgumentNullException("callableTypeAttribute");
            }
            if (callableFuncAttribute == null)
            {
                throw new ArgumentNullException("callableFuncAttribute");
            }

            RemoteResponse ret = new RemoteResponse();

            try
            {
                // get type
                Type interfaceType;
                interfaceType = TypeHelper.GetType(request.ExecuteOnRemoteSideOperation.InterfaceType, VersionMismatchCheckerAssemblyResolver, null);
                if (interfaceType != null)
                {
                    MethodInfo methodInfo = TypeHelper.GetMethodInfo(interfaceType, request.ExecuteOnRemoteSideOperation.MethodName, request.ExecuteOnRemoteSideOperation.Parameters);
                    if (methodInfo != null)
                    {
                        string calledMethodName = String.Concat(interfaceType.Name, "+", methodInfo.Name);
                        System.Diagnostics.Debug.WriteLine("{0}Call to: {1}", (request.RemoteID != null) ? String.Concat("[", request.RemoteID, "] ") : String.Empty, calledMethodName);

                        // sanity checks
                        if (!TypeHelper.HasCustomAttribute(interfaceType, callableTypeAttribute))
                        {
                            throw new InvalidOperationException(String.Format("Method {0} in type {1} called, but type not marked with {2}!", methodInfo.Name, interfaceType.Name, callableTypeAttribute)); //LOCSTR
                        }
                        var attrs = TypeHelper.GetCustomAttributes(methodInfo, callableFuncAttribute);
                        if (attrs.Length != 1)
                        {
                            throw new InvalidOperationException(String.Format("Method {0} in type {1} called, but method not marked with {2}!", methodInfo.Name, interfaceType.Name, callableFuncAttribute)); //LOCSTR
                        }

                        // get instance
                        var lbInstance = diContainer.GetLazyBoundInstance(interfaceType);
                        if (lbInstance.Value != null)
                        {
                            object instance = lbInstance.Value;
                            // call method
                            ret.ReturnValue = methodInfo.Invoke(instance, request.ExecuteOnRemoteSideOperation.Parameters);
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format("Interface {0} not available in DIContainer!", interfaceType.Name)); //LOCSTR
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format("Method {0} not found with parameters: {1}!", request.ExecuteOnRemoteSideOperation.MethodName, string.Join(",", request.ExecuteOnRemoteSideOperation.Parameters))); //LOCSTR
                    }
                }
                else
                {
                    throw new InvalidOperationException(string.Format("Interface {0} not found!", request.ExecuteOnRemoteSideOperation.InterfaceType)); //LOCSTR
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("While processing request: {1}{0}{1}{2}", request, Environment.NewLine, ex); //LOCSTR
                TargetInvocationException tex = ex as TargetInvocationException;
                if ((tex != null) && (tex.InnerException != null))
                {
                    ex = tex.InnerException;
                }
                ret.ReturnValue = ex;
            }

            return(ret);
        }