/// <summary> /// Invokes the message specified by the request parameter. /// </summary> /// <param name="request">Request to invoke</param> /// <returns>IMessage instance representing the return message</returns> public override IMessage Invoke(IMessage request) { IMethodReturnMessage response; var call = (IMethodCallMessage)request; var ctor = call as IConstructionCallMessage; if (ctor != null) { // ReSharper disable PossibleNullReferenceException // --- This is the call to the constructor, initialize the object var defaultProxy = RemotingServices.GetRealProxy(Target); response = defaultProxy.InitializeServerObject(ctor); if (response == null) { var returnException = new AspectInfrastructureException( "InitializeServerObject failed in AspectInjectionProxy.Invoke"); WindowsEventLogger.Log<AspectInfrastructureErrorEvent>( "The {0} constructor raised a non-business exception:{1}", ctor.MethodBase.DeclaringType.FullName, returnException.ToString()); return new ReturnMessage(returnException, call); } if (response.Exception != null) { WindowsEventLogger.Log<AspectInfrastructureErrorEvent>( "The {0} constructor raised a non-business exception:{1}", ctor.MethodBase.DeclaringType.FullName, response.Exception.ToString()); return new ReturnMessage(response.Exception, call); } // --- At this point the server object is initialized, and so can be used // --- Call the contructor behind the transparent proxy. var tp = (ContextBoundObject)GetTransparentProxy(); response = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, tp); // --- Allow configuring the proxy try { InitializeProxy(); } catch (Exception ex) { var returnException = new AspectInfrastructureException( "Calling context setup failed in AspectInjectionProxy.Invoke", ex); return new ReturnMessage(returnException, call); } // ReSharper restore PossibleNullReferenceException } else { // --- This is a call to a method // --- Check, if the method is an aspected operation or not response = IsAspectedMessage(call) ? OperationMethodCall(call) : RemotingServices.ExecuteMessage(Target, call); } return response; }
/// <summary> /// Executes the operation method. /// </summary> /// <param name="call">Method call with parameters, to execute</param> /// <returns>IMethodReturnMessage representing the result of the call</returns> private IMethodReturnMessage OperationMethodCall(IMethodCallMessage call) { try { // --- Manage the "before" aspects var response = BeforeOperationAspect(call); // --- Call the target method, assuming there is no result yet response = response ?? RemotingServices.ExecuteMessage(Target, call); // --- Manage the "after" aspects including exceptions AfterOperationAspect(call, response); if (response.Exception != null) { response = new ReturnMessage(HandleExceptionAspect(call, response.Exception), call); } if (response.Exception == null) AfterSuccessfulOperationAspect(call, response); return response; } catch (Exception ex) { try { var handledEx = HandleExceptionAspect(call, ex); return new ReturnMessage(handledEx, call); } catch (Exception ex2) { var innerException = new AspectInfrastructureException("HandleExceptionAspect failed.", ex2); innerException.Data.Add("ExceptionBeinghandled", ex); var returnException = new AspectInfrastructureException( "Exception handling failed in AspectInjectionProxy.OperationMethodCall", innerException); return new ReturnMessage(returnException, call); } } }