本文介绍了WCF Silverlight服务返回自定义错误,但作为HTTP 500响应而不是200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我一直在用Silverlight WCF服务中的自定义错误来敲打头,所以我会很高兴尽我所能买啤酒,任何可以帮助我解决这个问题的人! !

It seems like I've been banging my head with custom faults in my Silverlight WCF service forever so I will happily DO MY BEST TO BUY A BEER for anyone who can help me solve this!!!

经过一番痛苦之后,我终于有了WCF服务,它抛出了自定义错误(ParameterValidationFault),并使用Fiddler,我知道该服务的响应包含我的序列化故障对象,但是HTTP响应代码是500,而不是200,因此客户端启动了抛出异常而不是读取响应.

After much pain I finally have my WCF service throwing custom errors (ParameterValidationFault) and using Fiddler I know that the service's response contains my serialized fault object, but the HTTP response code is 500, not 200, so the client starts throwing exceptions rather than reading the response.

我知道我的SilverlightFaultBehavior类应该负责更改响应状态代码,但是我设置的断点永远不会被碰到,因此我希望这是一个简单的web.config问题(web.config在帖子结尾).

I know my SilverlightFaultBehavior class is supposed to take care of changing the response status code, but the breakpoints I set there are never being hit, so I'm hoping this is a simple web.config issue (web.config at end of post).

如果与此相关,则我的web.config在部分中显示元素'行为'具有无效的子元素'silverlightFaults'..."

If this is relevant my web.config shows "the element 'behavior' has invalid child element 'silverlightFaults'...", in the section

  <endpointBehaviors>
    <behavior name="SilverlightFaultBehavior">
      <silverlightFaults/>
    </behavior>
  </endpointBehaviors>

但是我认为这不是问题,因为我可以无错误地查看服务的元数据.但是,现在我想这是缺少的链接,它阻止了我的状态代码在出路时被更改.我已经阅读到此错误表明behaviorExtension元素中的type属性存在问题,该问题与.NET认为的不完全匹配,但是我已对此进行了一百万次检查,并且名称空间和程序集名称绝对正确.我对版本,文化或公钥内容一无所知.

but I thought this wasn't a problem as I can view the service's metadata without error. However now I'm thinking this is the missing link that's preventing my status code from being changed on the way out. I have read that this error indicates a problem with the type attribute within my behaviorExtension element not exactly matching what .NET thinks it should be, but I have checked this a million times and the namespace and assembly name are definitely correct. I haven't messed with the version, culture, or public key stuff.

.NET是否有一种简单的方法来确切告诉我该类型字符串应为什么(空格,逗号和所有字符)?我已经在资源管理器中查看了dll的属性,但仍然没有找到.

Is there a simple way for .NET to tell me exactly what this type string should be (spaces, commas, and all)? I have viewed the dll's properties in explorer but I'm still no closer.

任何其他关于此内容可能来自何处的建议,将不胜感激.

Any other suggestions on where this might be coming from would be hugely appreciated.

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="silverlightFaults" type="my.namespace.SilverlightFaultBehavior, AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </behaviorExtensions>
    </extensions>
    <behaviors>
      <endpointBehaviors>
        <behavior name="SilverlightFaultBehavior">
          <silverlightFaults/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <customBinding>
        <binding name="my.namespace.IService.customBinding0">
          <binaryMessageEncoding />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="my.namespace.IService">
        <endpoint address="" binding="customBinding" bindingConfiguration="my.namespace.IService.customBinding0" contract="my.namespace.IService" behaviorConfiguration="SilverlightFaultBehavior" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

我的SilverlightFaultBehavior类是这样开始的,并且是此MSDN帖子更改了名称空间

My SilverlightFaultBehavior class starts like this and is a copy-paste of this MSDN post with a namespace change

namespace my.namespace
{
    public class SilverlightFaultBehavior : BehaviorExtensionElement, IEndpointBehavior
    {
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {

推荐答案

好,因此此问题尚未解决,但可以解决.我终于在这条非常有用的帖子中看到了,我可以拥有匿名endpointBehavior.现在,将应用该行为,并且正确(以非标准的方式)将自定义错误作为HTTP 200s返回.将行为设为匿名意味着将其应用于所有端点,但是由于我的服务当前仅需要单个端点,因此对我有效.

OK so this problem has not been resolved but worked-around. I finally saw in this very useful post that I can have anonymous endpointBehaviors. The behaviour is now applied and custom faults are correctly (in a non-standard kind of way) returned as HTTP 200s. Making the behavior anonymous means it is applied to all endpoints, but as my service currently only needs a single endpoint this works for me.

在阅读了这份严谨的手册并将其逐字逐句地实施之后,我真的为所有这些悲伤而感到难过.最后,我的配置解析错误元素'behavior'具有无效的子元素'silverlightFaults'..."无关紧要,但是一路很臭的红鲱鱼很可能是原因(并且仍然可能是).

It truly sucks that I had all this grief after reading the friggin manual and implementing it word for word. In the end my config parse error "the element 'behavior' has invalid child element 'silverlightFaults'..." was irrelevant, but a very smelly red-herring along the way as it very well could have been the cause (and still may be).

万一有人想知道的话,我就喝啤酒.

In case anyone is wondering, I just drank the beer.

这篇关于WCF Silverlight服务返回自定义错误,但作为HTTP 500响应而不是200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:50