簡體   English   中英

后銳方法邊界方面不適用於WCF服務類

[英]Postsharp Methodboundry aspect not working with WCF Service class

我有一個自定義方面,我試圖添加到服務中以自動記錄所有錯誤並發送適當的錯誤消息,但是當我嘗試將其應用於類並將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”:空,“ Message”:“對象不能存儲在這種類型的數組中。”,“ StackTrace”:“在System.Array.InternalSetValue(Void *目標,對象值)\\ u000d \\ u000a在System.Array.SetValue(Object值,在System.Runtime.Serialization.ObjectManager.CompleteObject(ObjectHolder持有人,布爾值bObjectFullyComplete)在System.Runtime.Serialization.ObjectManager.DoNewlyRegisteredObjectFixups(ObjectHolder持有人)\\ u000d \\ u000a中的Int32 []索引) System.Runtime.Serialization.ObjectManager.RegisterObject(Object obj,Int64 objectID,SerializationInfo info,Int64 idOfContainingObj,MemberInfo成員,Int32 [] arrayIndex)\\ u000d \\ u000a at System.Runtime.Serialization.Formatters.Binary.ObjectReader.RegisterObject( System.Runtime.Serialization.Formatter上的對象obj,ParseRecord pr,ParseRecord objectPr,布爾bIsString)\\ u000d \\ u000a s.Binary.ObjectReader.ParseObjectEnd(ParseRecord pr)\\ u000d \\ u000a在System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)\\ u000d \\ u000a在System.Runtime.Serialization.Formatters.Binary .__ BinaryParser。 System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler處理程序,__BinaryParser serParser,布爾fCheck,布爾值isCrossAppDomain,IMethodCallMessage methodCallMessage)的Run()\\ u000d \\ u000a,System.Runtime.Serialization。 Binary.BinaryFormatter.Deserialize(流serializationStream,HeaderHandler處理程序,布爾fCheck,布爾isCrossAppDomain,IMethodCallMessage methodCallMessage)位於PostSharp.Aspects.Serialization.BinaryAspectSerializer.Deserialize(uStream) .Serialization.AspectSerializer.Deserialize(組裝程序集,字符串resourceName,IMetadataDispenser metadataDispenser)\\ u000d \\ u000a在PostSharp.ImplementationDe 位於System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance處的00d \\ u000a(InstanceContext instanceContext,消息請求)\\ System.ServiceModel.InstanceContext.GetServiceInstance(消息消息)\\ u000d \\ u000a處於System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance (MessageRpc&rpc)\\ u000d \\ u000a位於System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&rpc)\\ u000d \\ u000a位於System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&rpc)\\ u000d \\ u000a .System.ServiceModel.Dispatcher上的.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&rpc)\\ u000d \\ u000a.System.ServiceModel.Dispatcher.ImmutableDispatchRunu.000上的.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&rpc)\\ u000d \\ u000a在System.ServiceModel.Dispatcher.ImmutableDispatchRunu.000 .ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&rpc)\\ u000d \\ u000a位於System.ServiceModel.Dispatcher.ImmutableDispat chRuntime.ProcessMessage1(MessageRpc&rpc)\\ u000d \\ u000a位於System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)“,” Type“:” System.TypeInitializationException“},” ExceptionType“:” System.TypeInitializationException“,”消息“:”“ AOS.BrokerAPI.WCFService.BrokerAPIService的類型初始化程序引發了異常。”,“ StackTrace”:“在AOS.BrokerAPI.WCFService.BrokerAPIService..ctor()\\ u000d \\ u000a在CreateAOS.BrokerAPI.WCFService System.ServiceModel.Dispatcher.InstanceProvider.GetInstance(InstanceContext instanceContext,消息消息)處的.BrokerAPIService()\\ u000d \\ u000a,System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance(InstanceContext instanceContext,消息請求)處的.BrokerAPIService()\\ u000d \\ u000a在System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance(MessageRpc&rpc)在System.ServiceModel.InstanceContext.GetServiceInstance(Message message)\\ u000d \\ u000a在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(M System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&rpc)\\ u000d \\ u000a在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&rpc)在System.ServiceModel.Dispatcher.u000d \\ u000a。 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&rpc)\\ u000d \\ u000a位於System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.d上的ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&rpc)\\ u000d \\ u000a。 ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&rpc)\\ u000d \\ u000a at System.ServiceModel.Dispatcher.MessageRpc.Process(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]屬性,服務無法啟動。 將可序列化的屬性更改為[OnMethodBoundaryAspectConfiguration(SerializerType = typeof(MsilAspectSerializer))]返回Method not found: 'Void API.Model.Infrastructure.Aspect.ServiceAspect.OnException(PostSharp.Aspects.MethodExecutionArgs

請看一下這個討論 看來您的項目中可能有兩個不同版本的PostSharp.dll。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM