我有一个自定义方面我试图添加到我的服务中以自动记录所有错误并发出相应的错误消息,但是当我尝试将其应用于类并将Attributetargetelements设置为Multicast.method或将其应用于个人方法,我的服务返回500错误,错误消息
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":空,"的InnerException" {" HELPLINK":空,"的InnerException" {" HELPLINK":空,"的InnerException" {" HELPLINK":空,"的InnerException":空,& #34;消息":"对象不能存储在这种类型的数组中。"," StackTrace":"在System.Rrtime.Serialization.ObjectManager.CompleteObject(ObjectHolder holder)的System.Array.SetValue(对象值,Int32 []索引)\ u000d \ u000a处的System.Array.InternalSetValue(Void * target,Object value)\ u000d \ u000a ,System.Runtime.Serialization.ObjectManager.DoNewlyRegisteredObjectFixups(ObjectHolder holder)\ u000d \ u000a,System.Runtime.Serialization.ObjectManager.RegisterObject(Object obj,Int64 objectID,SerializationInfo info,Int64 idOfContainingObj,MemberInfo)中的,布尔bObjectFullyComplete)\ u000d \ u000a System.Runtime.Serialization.Formatters的System.Runtime.Serialization.Formatters.Binary.ObjectReader.RegisterObject(Object obj,ParseRecord pr,ParseRecord objectPr,Boolean bIsString)\ u000d \ u000a中的成员,Int32 [] arrayIndex)\ u000d \ u000a System.Runtime.Serialization.Formatters.Binary .__ BinaryP的System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)\ u000d \ u000a .Binary.ObjectReader.ParseObjectEnd(ParseRecord pr)\ u000d \ u000a arser.Run()\ u000d \ u000a在System.Runtime.Serialization的System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler,__BinaryParser serParser,Boolean fCheck,Boolean isCrossAppDomain,IMethodCallMessage methodCallMessage)\ u000d \ u000a。 PostSharp的PostSharp.Aspects.Serialization.BinaryAspectSerializer.Deserialize(Stream stream,IMetadataDispenser metadataDispenser)\ u000d \ u000a的Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream,HeaderHandler handler,Boolean fCheck,Boolean isCrossAppDomain,IMethodCallMessage methodCallMessage)\ u000d \ u000a .Aspects.Serialization.AspectSerializer.Deserialize(Assembly assembly,String resourceName,IMetadataDispenser metadataDispenser)\ u000d \ u000a at PostSharp.ImplementationDetails_fd2023ff。<> z__a_2..cctor()in:line 0"," Type& #34;:" System.InvalidCastException"}," Message":"' PostSharp.ImplementationDetails_fd2023ff的类型初始值设定项。<> z__a_2'抛出异常。"," StackTrace":"在PostSharp.ImplementationDetails_fd2023ff。<> z__a_1..cctor()in:第0行","输入":" System.TypeInitializationException"}," Message&# 34;:"' PostSharp.ImplementationDetails_fd2023ff的类型初始值设定项。<> z__a_1'抛出异常。"," StackTrace":"在PostSharp.ImplementationDetails_fd2023ff。<> z__a_1.Initialize()\ u000d \ u000a在AOS.BrokerAPI.WCFService.BrokerAPIService..cctor()中:第0行","输入":&# 34; System.TypeInitializationException"}," Message":"' AOS.BrokerAPI.WCFService.BrokerAPIService'的类型初始值设定项。抛出异常。"," StackTrace":"在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位于System.ServiceModel的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)\ u000d \ u000a,位于System.ServiceModel的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)\ u000d \ u000a System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageR)中的.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)\ u000d \ u000a PC和放大器; rpc)\ u000d \ u000a在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)\ u000d \ u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)\ u000d \ u000a at System.ServiceModel.Dispatcher System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)"," Type":" System.TypeInitializationException&#34 ;}," ExceptionType":" System.TypeInitializationException"," Message":"' AOS.BrokerAPI.WCFService的类型初始值设定项。 BrokerAPIService'抛出异常。"," StackTrace":"在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位于System.ServiceModel的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)\ u000d \ u000a,位于System.ServiceModel的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)\ u000d \ u000a System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageR)中的.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)\ u000d \ u000a PC和放大器; rpc)\ u000d \ u000a在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)\ u000d \ u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)\ u000d \ u000a at System.ServiceModel.Dispatcher System.ServiceModel.Dispatcher.MessageRpc.Process中的.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)\ u000d \ u000a(Boolean isOperationContextSet)"}
我有办法解决这个问题吗?
我的代码如下
服务方面
[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);
}
}
}
并将其应用于班级
[ServiceAspect(AttributeTargetElements = MulticastTargets.Method)]
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, InstanceContextMode = InstanceContextMode.PerCall,IncludeExceptionDetailInFaults = true)]
public class APIService : ServiceBase,IAPIService
{
一旦从代码中删除服务方面,它就会按预期工作。
编辑
设置[OnMethodBoundaryAspectConfiguration(SerializerType = typeof(MsilAspectSerializer))]
以替换[Serializable属性现在返回
我已经考虑过了,但问题是由于ServiceAspect上的[Serializable]属性,服务无法启动。将serializable属性更改为
[OnMethodBoundaryAspectConfiguration(SerializerType = typeof(MsilAspectSerializer))]
会返回Method not found: 'Void API.Model.Infrastructure.Aspect.ServiceAspect.OnException(PostSharp.Aspects.MethodExecutionArgs