用于WCF的silverlightFaultBehavior - 但仍然获得代码500

时间:2010-11-05 18:15:40

标签: silverlight wcf silverlight-4.0 https

我希望从我的WCF服务获取Silverlight 4应用程序的有意义的错误消息。经过一番调查,我发现如果我想让silverlight启用读取有意义的错误信息,我需要将回复代码从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 - 方法没有......任何想法为什么?

1 个答案:

答案 0 :(得分:1)

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