WCF:如何指定AddressFilterMode.Any声明 [英] WCF: How to specify AddressFilterMode.Any declaratively

查看:647
本文介绍了WCF:如何指定AddressFilterMode.Any声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提前回答我的问题道歉,但我看到这么多的答案,指出AddressFilterMode.Any需要添加的时候,你可以创建WCF,做同样的事情的扩展行为code属性。随着问这么多的地方同样的问题,我认为这将是更为有利回答这个问题在一个地方。

I apologize in advance for answering my own question but I see so many answers stating that AddressFilterMode.Any needs to be added as a code attribute when you can create an extension behavior for WCF that does the same thing. With the same question asked in so many places, I thought it would be more beneficial to answer the question in one place.

推荐答案

创建自定义行为

Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.ServiceModel.Description
Imports System.ServiceModel.Dispatcher
Imports System.ServiceModel.Channels

'We're assuming your project declares some default namespace like Org.ServiceModel
Namespace Description
Public Class AddressFilterModeAnyBehavior
    Implements IEndpointBehavior

    Private Const jsPostfix As String = "js"
    Private Const jsdebugPostFix As String = "jsdebug"

    Private Const MaxMetadataEndpointBufferSize As Integer = 2048

    Public Sub AddBindingParameters(ByVal endpoint As System.ServiceModel.Description.ServiceEndpoint, ByVal bindingParameters As System.ServiceModel.Channels.BindingParameterCollection) Implements System.ServiceModel.Description.IEndpointBehavior.AddBindingParameters

    End Sub

    Public Sub ApplyClientBehavior(ByVal endpoint As System.ServiceModel.Description.ServiceEndpoint, ByVal clientRuntime As System.ServiceModel.Dispatcher.ClientRuntime) Implements System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior

    End Sub

    Public Sub ApplyDispatchBehavior(ByVal endpoint As System.ServiceModel.Description.ServiceEndpoint, ByVal endpointDispatcher As System.ServiceModel.Dispatcher.EndpointDispatcher) Implements System.ServiceModel.Description.IEndpointBehavior.ApplyDispatchBehavior
        If endpointDispatcher Is Nothing Then Return
        endpointDispatcher.AddressFilter = New MatchAllMessageFilter
        If (HasWebScriptBehavior(endpoint)) Then
            HandleWebScriptBehavior(endpoint, endpointDispatcher)
        End If
    End Sub

    Public Sub Validate(ByVal endpoint As System.ServiceModel.Description.ServiceEndpoint) Implements System.ServiceModel.Description.IEndpointBehavior.Validate

    End Sub

    Protected Sub HandleWebScriptBehavior(ByVal endpoint As ServiceEndpoint, ByVal endpointDispatcher As EndpointDispatcher)
        If endpoint Is Nothing OrElse endpointDispatcher Is Nothing Then Exit Sub
        If Not HasListenUri(endpoint) Then Exit Sub
        Dim baseAddress As Uri = endpoint.Address.Uri
        Dim jsUri As Uri = CreateWebScriptUri(baseAddress, False)
        Dim jsdebugUri As Uri = CreateWebScriptUri(baseAddress, True)
        Dim host As ServiceHostBase = endpointDispatcher.ChannelDispatcher.Host
        Dim channelDispatchers As ChannelDispatcherCollection = host.ChannelDispatchers
        For Each channelDispatcher As ChannelDispatcher In channelDispatchers
            For Each dispatcher As EndpointDispatcher In channelDispatcher.Endpoints
                With dispatcher
                    Dim endpointUri As Uri = .EndpointAddress.Uri
                    If (endpointUri.Equals(jsdebugUri) OrElse endpointUri.Equals(jsUri)) Then
                        .AddressFilter = New MatchAllMessageFilter
                    End If
                End With
            Next
        Next
    End Sub

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")>
    Protected Function HasWebScriptBehavior(ByVal endpoint As ServiceEndpoint) As Boolean
        If endpoint Is Nothing Then Return False
        Return (From behavior In endpoint.Behaviors Where TypeOf behavior Is WebScriptEnablingBehavior).Any
    End Function

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")>
    Protected Function HasListenUri(ByVal endpoint As ServiceEndpoint) As Boolean
        If endpoint Is Nothing Then Return False
        Return Not endpoint.Address.Uri.Equals(endpoint.ListenUri)
    End Function

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")>
    Protected Function CreateWebScriptUri(ByVal baseUri As Uri, ByVal debug As Boolean) As Uri
        Dim builder As New UriBuilder(baseUri)
        If (debug) Then
            builder.Path += If(builder.Path.EndsWith("/", StringComparison.OrdinalIgnoreCase), (jsdebugPostFix), ("/" + jsdebugPostFix))
        Else
            builder.Path += If(builder.Path.EndsWith("/", StringComparison.OrdinalIgnoreCase), (jsPostfix), ("/" + jsPostfix))
        End If
        Return builder.Uri
    End Function


End Class
End Namespace

创建自定义的配置元素

Imports System.ServiceModel
Imports System.ServiceModel.Configuration
Imports Hsb.ServiceModel.Description

'We're assuming your project declares some default namespace like Org.ServiceModel
Namespace Configuration

Public Class AddressFilterModeAnyElement
    Inherits BehaviorExtensionElement


#Region "BehaviorExtensionElement Implementation"
    'The BehaviorExtensionElement base class allows the behavior to be added through configuration
    'using the system.servicemodel/extensions .config element.
    <System.Diagnostics.CodeAnalysis.SuppressMessage("Configuration", "Configuration102:ConfigurationPropertyAttributeRule", MessageId:="System.ServiceModel.Configuration.WebScriptEnablingElement.BehaviorType", Justification:="Not a configurable property; a property that had to be overridden from abstract parent class")> _
    Public Overrides ReadOnly Property BehaviorType() As System.Type
        Get
            Return GetType(AddressFilterModeAnyBehavior)
        End Get
    End Property

    Protected Overrides Function CreateBehavior() As Object
        Return New AddressFilterModeAnyBehavior()
    End Function
#End Region

End Class
End Namespace

使用扩展元素在你的WCF配置 我们将假定组件名为Org.ServiceModel

Use the extension element in your WCF Configuration We're going to assume that the assembly is named Org.ServiceModel

<system.serviceModel>
 <behaviors>
  <endpointBehaviors>
    <behavior name="JSON">          
      <enableWebScript />
      <addressFilterModeAny />
    </behavior>        
  </endpointBehaviors>
</behaviors>
<extensions>
  <behaviorExtensions>
    <!-- Microsoft Connect Issue ID 216431: The full assembly qualified typename including version, culture and key must be specified.-->
    <!-- The following endpoint behavior extension element sets the endpoint's address filter mode to any.  This allows the service
    to operate behind an SSL load balancer where externally https is used and internally http is used.-->
    <add name="addressFilterModeAny" type="Org.ServiceModel.Configuration.AddressFilterModeAnyElement, Org.ServiceModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </behaviorExtensions>
</extensions>
</system.serviceModel>

这篇关于WCF:如何指定AddressFilterMode.Any声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆