title: Postsharp Methodboundry aspect not working with WCF Service class
body: <p>I have a custom aspect I am trying to add to my service to automatically log all error and send out an appropriate error message, but when I try to apply it on the class and set Attributetargetelements to Multicast.method or apply it to the individual method , my Service returns a 500 error with error message </p>
  
  <pre><code>HTTP/1.1 500 Internal Server Error
  Content-Length: 5829
  Content-Type: application/json; charset=utf-8
  Server: Microsoft-HTTPAPI/2.0
  jsonerror: true
  WWW-Authenticate: oRswGaADCgEAoxIEEAEAAACpU1QBCKB37wAAAAA=
  Date: Tue, 29 Mar 2016 10:03:03 GMT
  </code></pre>
  
  <blockquote>
    <p>{"ExceptionDetail":{"HelpLink":null,"InnerException":{"HelpLink":null,"InnerException":{"HelpLink":null,"InnerException":{"HelpLink":null,"InnerException":null,"Message":"Object cannot be stored in an array of this type.","StackTrace":"   at System.Array.InternalSetValue(Void* target, Object value)\u000d\u000a   at System.Array.SetValue(Object value, Int32[] indices)\u000d\u000a   at System.Runtime.Serialization.ObjectManager.CompleteObject(ObjectHolder holder, Boolean bObjectFullyComplete)\u000d\u000a   at System.Runtime.Serialization.ObjectManager.DoNewlyRegisteredObjectFixups(ObjectHolder holder)\u000d\u000a   at System.Runtime.Serialization.ObjectManager.RegisterObject(Object obj, Int64 objectID, SerializationInfo info, Int64 idOfContainingObj, MemberInfo member, Int32[] arrayIndex)\u000d\u000a   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.RegisterObject(Object obj, ParseRecord pr, ParseRecord objectPr, Boolean bIsString)\u000d\u000a   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObjectEnd(ParseRecord pr)\u000d\u000a   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)\u000d\u000a   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()\u000d\u000a   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)\u000d\u000a   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)\u000d\u000a   at PostSharp.Aspects.Serialization.BinaryAspectSerializer.Deserialize(Stream stream, IMetadataDispenser metadataDispenser)\u000d\u000a   at PostSharp.Aspects.Serialization.AspectSerializer.Deserialize(Assembly assembly, String resourceName, IMetadataDispenser metadataDispenser)\u000d\u000a   at PostSharp.ImplementationDetails_fd2023ff.&lt;>z__a_2..cctor() in :line 0","Type":"System.InvalidCastException"},"Message":"The type initializer for 'PostSharp.ImplementationDetails_fd2023ff.&lt;>z__a_2' threw an exception.","StackTrace":"   at PostSharp.ImplementationDetails_fd2023ff.&lt;>z__a_1..cctor() in :line 0","Type":"System.TypeInitializationException"},"Message":"The type initializer for 'PostSharp.ImplementationDetails_fd2023ff.&lt;>z__a_1' threw an exception.","StackTrace":"   at PostSharp.ImplementationDetails_fd2023ff.&lt;>z__a_1.Initialize()\u000d\u000a   at AOS.BrokerAPI.WCFService.BrokerAPIService..cctor() in :line 0","Type":"System.TypeInitializationException"},"Message":"The type initializer for 'AOS.BrokerAPI.WCFService.BrokerAPIService' threw an exception.","StackTrace":"   at AOS.BrokerAPI.WCFService.BrokerAPIService..ctor()\u000d\u000a   at CreateAOS.BrokerAPI.WCFService.BrokerAPIService()\u000d\u000a   at System.ServiceModel.Dispatcher.InstanceProvider.GetInstance(InstanceContext instanceContext, Message message)\u000d\u000a   at System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance(InstanceContext instanceContext, Message request)\u000d\u000a   at System.ServiceModel.InstanceContext.GetServiceInstance(Message message)\u000d\u000a   at System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)","Type":"System.TypeInitializationException"},"ExceptionType":"System.TypeInitializationException","Message":"The type initializer for 'AOS.BrokerAPI.WCFService.BrokerAPIService' threw an exception.","StackTrace":"   at AOS.BrokerAPI.WCFService.BrokerAPIService..ctor()\u000d\u000a   at CreateAOS.BrokerAPI.WCFService.BrokerAPIService()\u000d\u000a   at System.ServiceModel.Dispatcher.InstanceProvider.GetInstance(InstanceContext instanceContext, Message message)\u000d\u000a   at System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance(InstanceContext instanceContext, Message request)\u000d\u000a   at System.ServiceModel.InstanceContext.GetServiceInstance(Message message)\u000d\u000a   at System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)\u000d\u000a   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)"}</p>
  </blockquote>
  
  <p><strong><em>Is there a way I can get around this?</em></strong> </p>
  
  <p><strong>My Code is as follows</strong></p>
  
  <blockquote>
    <p><em>Service Aspect</em></p>
  </blockquote>
  
  <pre><code>[Serializable]
  public class ServiceAspect : OnMethodBoundaryAspect
  {
      //create logger
  
      private static readonly log4net.ILog log = LogManager.GetLogger(typeof(BrokerServiceAspect).Name);//
  
      public override void OnException(MethodExecutionArgs args)
      {
          log.Error(args.Exception.Message, args.Exception);//log specific type of error
          args.FlowBehavior = FlowBehavior.Return;//return excution
  
          if (args.Exception is NullReferenceException)
          {
              args.ReturnValue = new FaultException(BrokerFaultCodes.NullExceptionCode);
          }
          else if (args.Exception is Exception)
          {
              args.ReturnValue = new FaultException(BrokerFaultCodes.GenericException);
          }
      }
  }
  </code></pre>
  
  <p>And applying it on the class</p>
  
  <pre><code>[ServiceAspect(AttributeTargetElements = MulticastTargets.Method)]
  [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, InstanceContextMode = InstanceContextMode.PerCall,IncludeExceptionDetailInFaults = true)]
  public class APIService : ServiceBase,IAPIService
  {
  </code></pre>
  
  <p>as soon as I remove the service aspect from my code, it works as expected.</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>Setting <code>[OnMethodBoundaryAspectConfiguration(SerializerType = typeof(MsilAspectSerializer))]</code> to replace the [Serializable attribute now returns </p>
  
  <blockquote>
    <p>I have thought about that, but the issue is that the service does not start because of the [Serializable] attribute on the ServiceAspect. Changing the serializable attribute to  <code>[OnMethodBoundaryAspectConfiguration(SerializerType = typeof(MsilAspectSerializer))]</code> returns
    <code>Method not found: 'Void API.Model.Infrastructure.Aspect.ServiceAspect.OnException(PostSharp.Aspects.MethodExecutionArgs</code></p>
  </blockquote>