【问题标题】:silverlightFaultBehavior for WCF - but still getting code 500WCF 的 silverlightFaultBehavior - 但仍然获得代码 500
【发布时间】:2010-11-05 18:15:40
【问题描述】:

我想从我的 Silverlight 4 应用程序的 WCF 服务中获取有意义的错误消息。经过一番调查,我发现如果我想让 silverlight enable 读取有意义的错误消息,我需要将回复代码从 500 更改为 200。这里是文章:http://msdn.microsoft.com/de-de/library/ee844556(VS.95).aspx

我已经实现了它,因为它在那里编写,应用程序编译并且我可以使用该服务 - 但我仍然得到 500 返回码。我看到的主要区别是我通过 HTTPS 而不是 HTTP 调用服务。也许这就是原因,为什么它不起作用?任何想法,如何获得返回码 200?

这是我的 Web.Config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="ServiceConfiguratorDataSource.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="silverlightFaults" type="ServiceConfiguratorDataSource.SilverlightFaultBehavior, ServiceConfiguratorDataSource, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
        </behaviorExtensions>
    </extensions>       
    <services>
        <service name="ServiceConfiguratorDataSource.Service" behaviorConfiguration="ServiceConfiguratorDataSourceBehaviour">
            <endpoint address="" binding="customBinding" behaviorConfiguration="SLFaultBehavior" bindingConfiguration="ServiceConfiguratorCustomBinding" contract="ServiceConfiguratorDataSource.IService" />
        </service>
    </services>
    <bindings>
        <customBinding>
            <binding name="ServiceConfiguratorCustomBinding">
                <security authenticationMode="UserNameOverTransport"></security>
                <binaryMessageEncoding></binaryMessageEncoding>
                <httpsTransport/>
            </binding>
        </customBinding>
    </bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceConfiguratorDataSourceBehaviour">
      <serviceMetadata httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="True"/>
                <serviceCredentials>
                    <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="ServiceConfiguratorDataSource.UserCredentialsValidator,ServiceConfiguratorDataSource" />
                </serviceCredentials>
    </behavior>
  </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="SLFaultBehavior">
                <silverlightFaults/>
            </behavior>
        </endpointBehaviors>            
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

...这里是silverlightFaultBehavior.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.ServiceModel;

namespace ServiceConfiguratorDataSource
{
public class SilverlightFaultBehavior : BehaviorExtensionElement, IEndpointBehavior
{
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        SilverlightFaultMessageInspector inspector = new SilverlightFaultMessageInspector();
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
    }
    public class SilverlightFaultMessageInspector : IDispatchMessageInspector
    {
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (reply.IsFault)
            {
                HttpResponseMessageProperty property = new HttpResponseMessageProperty();

                // Here the response code is changed to 200.
                property.StatusCode = System.Net.HttpStatusCode.OK;

                reply.Properties[HttpResponseMessageProperty.Name] = property;
            }
        }

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            // Do nothing to the incoming message.
            return null;
        }
    }

    // The following methods are stubs and not relevant. 
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }

    public override System.Type BehaviorType
    {
        get { return typeof(SilverlightFaultBehavior); }
    }

    protected override object CreateBehavior()
    {
        return new SilverlightFaultBehavior();
    }

}
}

有人知道这是否是因为 https ......如果是,如何让它工作?

提前致谢,
弗兰克

EDITH 说:我刚刚添加了一些日志记录:ApplyDispatchBehavior - 方法被调用,但 BeforeSendReply - 方法没有......任何想法为什么?

【问题讨论】:

  • 当您看到 500 响应代码时,首先是什么导致了故障? (它是在 [OperationContract] 方法中抛出的异常吗?显式抛出 FaultException?还有别的吗?你能尝试显式抛出一个 FaultException 看看是否还会发生这种情况吗?)
  • 这是来自我在 UserNamePasswordValidator 派生类中抛出的显式 FaultException。我故意发送错误的用户凭据,因此验证器抛出异常。如果这就是我仍然得到 500 的原因......还有一个理由来踢这个该死的 UserNameOverTransport 身份验证,只需检查我的服务的每个方法调用的凭据。

标签: silverlight wcf silverlight-4.0 https


【解决方案1】:

如果我没记错的话,UserNamePasswordValidator 在管道中很早就被调用,在调度程序被调用之前,这就是为什么你的自定义调度行为不会影响任何东西。 (原因是安全性:WCF 希望尽早“丢弃”未经授权的请求,同时为它们运行尽可能少的代码)。 正如您自己在 cmets 中建议的那样,一种解决方案是稍后在管道中验证凭据 - 例如在每个操作中(甚至可能在消息检查器的 AfterReceiveRequest 中?)

【讨论】:

  • 我想我会在每个操作中验证凭据。无论如何,我都需要访问其中一些用户的信息,因为结果可能非常取决于授予当前用户的权限。非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-15
  • 2014-06-14
  • 2017-04-17
  • 1970-01-01
  • 2012-10-30
  • 2015-02-05
相关资源
最近更新 更多