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
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
{"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.<>z__a_2..cctor() in :line 0","Type":"System.InvalidCastException"},"Message":"The type initializer for 'PostSharp.ImplementationDetails_fd2023ff.<>z__a_2' threw an exception.","StackTrace":" at PostSharp.ImplementationDetails_fd2023ff.<>z__a_1..cctor() in :line 0","Type":"System.TypeInitializationException"},"Message":"The type initializer for 'PostSharp.ImplementationDetails_fd2023ff.<>z__a_1' threw an exception.","StackTrace":" at PostSharp.ImplementationDetails_fd2023ff.<>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& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& 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& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)"}
Is there a way I can get around this?
My Code is as follows
Service Aspect
[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);
}
}
}
And applying it on the class
[ServiceAspect(AttributeTargetElements = MulticastTargets.Method)]
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, InstanceContextMode = InstanceContextMode.PerCall,IncludeExceptionDetailInFaults = true)]
public class APIService : ServiceBase,IAPIService
{
as soon as I remove the service aspect from my code, it works as expected.
EDIT
Setting [OnMethodBoundaryAspectConfiguration(SerializerType = typeof(MsilAspectSerializer))]
to replace the [Serializable attribute now returns
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
[OnMethodBoundaryAspectConfiguration(SerializerType = typeof(MsilAspectSerializer))]
returnsMethod not found: 'Void API.Model.Infrastructure.Aspect.ServiceAspect.OnException(PostSharp.Aspects.MethodExecutionArgs