【问题标题】:Castle Windsor throws a CommunicationObjectFaultedException when disposing a WCF client that has thrown a MessageSecurityException处置已引发 MessageSecurityException 的 WCF 客户端时,Castle Windsor 引发 CommunicationObjectFaultedException
【发布时间】:2015-04-28 11:21:25
【问题描述】:

我有一个带有使用 WCF 服务的控制器的 ASP.NET MVC 应用程序。 Web 应用程序由 WCF 服务使用 WIF 和安全令牌服务 (STS) 进行身份验证。这要求 WCF 服务在其 web.config () 中具有 STS 证书指纹。当调用 WCF 服务方法并且使用错误的指纹配置 WCF 服务时,服务方法会引发异常:

System.AggregateException: One or more errors occurred. ---> System.ServiceModel.Security.MessageSecurityException: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail. ---> System.ServiceModel.FaultException: An error occurred when processing the security tokens in the message.     --- End of inner exception stack trace ---     at 
System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)     at 
System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)     at 
System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)     at 
System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass5`1.<CreateGenericTask>b__4(IAsyncResult asyncResult)     at 
System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)     --- End of inner exception stack trace ---     at 
System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)     at 
Helper.GetMessages(String func) in xxx  ---> (Inner Exception #0) System.ServiceModel.Security.MessageSecurityException: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail. ---> System.ServiceModel.FaultException: An error occurred when processing the security tokens in the message.     --- End of inner exception stack trace ---     at 
System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)     at 
System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)     at 
System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)     at 
System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass5`1.<CreateGenericTask>b__4(IAsyncResult asyncResult)     at 
System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)<--- 

在 MVC 控制器中,我们捕获服务抛出的任何异常,因为即使服务出现问题,我们也希望返回响应:

try {
    service.Hello();
}
catch (Exception e) {
    log.Error("Service error", e);
}

控制器方法返回 ViewResult,Castle Windsor 开始清理控制器及其依赖项。但是,当 Windsor 处理抛出 MessageSecurityException 的 WCF 客户端时,我得到了这个异常:

[CommunicationObjectFaultedException: The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +14579646
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +622
System.IDisposable.Dispose() +0
Castle.MicroKernel.LifecycleConcerns.DisposalConcern.Apply(ComponentModel model, Object component) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\LifecycleConcerns\DisposalConcern.cs:47
Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.ApplyConcerns(IEnumerable`1 steps, Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\ComponentActivator\AbstractComponentActivator.cs:120
Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.ApplyDecommissionConcerns(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\ComponentActivator\AbstractComponentActivator.cs:132
Castle.Facilities.WcfIntegration.WcfClientActivator.ApplyDecommissionConcerns(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Facilities.WcfIntegration\Client\WcfClientActivator.cs:64
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalDestroy(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs:74
Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Destroy(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\ComponentActivator\AbstractComponentActivator.cs:87
Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Release(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\AbstractLifestyleManager.cs:64
Castle.MicroKernel.Handlers.DefaultHandler.ReleaseCore(Burden burden) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Handlers\DefaultHandler.cs:65
Castle.MicroKernel.Handlers.AbstractHandler.Release(Burden burden) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Handlers\AbstractHandler.cs:170
Castle.MicroKernel.Burden.Release() in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Burden.cs:119
Castle.MicroKernel.Lifestyle.Scoped.ScopeCache.<Dispose>b__0(Burden b) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\Scoped\ScopeCache.cs:65
Castle.Core.Internal.CollectionExtensions.ForEach(IEnumerable`1 items, Action`1 action) in d:\work\16de7b8c88ab14af\src\Castle.Core\Core\Internal\CollectionExtensions.cs:44
Castle.MicroKernel.Lifestyle.Scoped.ScopeCache.Dispose() in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\Scoped\ScopeCache.cs:65
Castle.MicroKernel.Lifestyle.Scoped.DefaultLifetimeScope.Dispose() in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\Scoped\DefaultLifetimeScope.cs:38
Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule.Application_EndRequest(Object sender, EventArgs e) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\PerWebRequestLifestyleModule.cs:47
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165

抛出异常的代码是:

AbstractComponentActivator.ApplyConcerns(IEnumerable<IDecommissionConcern> steps, object instance)
DisposalConcern.Apply(ComponentModel model, object component) 

在调试时我注意到了以下区别:

当 service.Hello();被调用,frameowrk 抛出 MessageSecurityException 然后object component = {System.ServiceModel.Channels.ServiceChannelProxy}DisposalConcern.Apply(ComponentModel model, object component) 这会引发 CommunicationObjectFaultedException

当 service.Hello();被调用,我抛出新异常(“测试”) 然后object component = {System.Runtime.Remoting.Proxies.__TransparentProxy}DisposalConcern.Apply(ComponentModel model, object component) 这不会引发异常

该服务在 Web 应用程序中注册如下:

public class MeldingssystemServiceInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component.For<IMeldingssystemService>()
            .AsWcfClient(new DefaultClientModel(WcfEndpoint.FromConfiguration("MeldingssystemService")))
        .LifeStyle.PerWebRequest);
    }
}

我正在尝试确定这是 Castle Windsor 中的错误/限制还是配置错误。

【问题讨论】:

    标签: c# wcf castle-windsor


    【解决方案1】:

    您可以在 OnDestroy 事件中添加 abort 语句:

    public class MeldingssystemServiceInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Component.For<IMeldingssystemService>()
                .AsWcfClient(new DefaultClientModel(WcfEndpoint.FromConfiguration("MeldingssystemService")))
                .LifeStyle.PerWebRequest.OnDestroy(service =>
                {
                    var channel = (IClientChannel) service;
    
                    if (channel.State == CommunicationState.Faulted)
                        channel.Abort();
                }));
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试添加一个中止语句:

      try {
          service.Hello();
      }
      catch (Exception e) {
          service.abort();    
          log.Error("Service error", e);   
      }
      

      请参阅以下内容:http://www.codeproject.com/Articles/74129/The-Proper-Use-and-Disposal-of-WCF-Channels-or-Com

      祝你好运,

      马尔维恩。

      【讨论】:

      • 嗨,我知道 Indisposable WCF 客户端问题。 Castle Windsor 已经处理了故障客户端 (Castle.Facilities.WcfIntegration.Internal.WcfUtils)。如果服务抛出异常,它将处于故障状态,Windsor 能够正确处理客户端。问题是服务是否抛出了 MessageSecurityException Windsor 无法正确处理客户端。我可以在 catch 块中添加((IClientChannel)service).Abort();,但这似乎有点笨拙。我希望城堡温莎处理对象的生活方式。
      【解决方案3】:

      错误的根本原因是温莎城堡 wcf 设施中的回归错误。该错误已在 nuget 包 Castle.WcfIntegrationFacility 的 4.1.0 版中修复。

      清理语句的顺序已更改,详情见https://github.com/castleproject/Windsor/issues/104

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-22
        • 2012-04-22
        相关资源
        最近更新 更多