Tôi xin lỗi trước vì trả lời câu hỏi của riêng tôi nhưng tôi thấy rất nhiều câu trả lời nói rằng AddressFilterMode.Any cần phải được thêm vào như một thuộc tính mã khi bạn có thể tạo một hành vi mở rộng cho WCF làm điều tương tự. Với cùng một câu hỏi được hỏi ở rất nhiều nơi, tôi nghĩ sẽ có ích hơn nếu trả lời câu hỏi ở một nơi.WCF: Làm thế nào để chỉ định AddressFilterMode.Any tuyên bố
6
A
Trả lời
4
Tạo một hành vi tùy chỉnh
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
Tạo một yếu tố cấu hình tùy chỉnh
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
Sử dụng các yếu tố mở rộng trong cấu hình WCF bạn Chúng ta sẽ giả định rằng lắp ráp có tên là 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>
Các vấn đề liên quan
- 1. Làm thế nào để bao gồm Tuyên bố với XElement.ToString()
- 2. Làm thế nào để gỡ lỗi một Tuyên bố LINQ
- 3. Làm thế nào để giảm một tuyên bố hợp lý?
- 4. làm thế nào để in Tuyên bố (CallableStatement) trong Java?
- 5. Tuyên bố, tuyên bố, tuyên bố
- 6. Làm thế nào để xác định hiệu trưởng cho một tuyên bố chính sách AWS?
- 7. Bison/Flex tuyên bố lộn xộn ... làm thế nào những gì tôi nên bao gồm/tuyên bố ở đâu?
- 8. IF tuyên bố chỉ là không làm việc
- 9. WPF DialogResult tuyên bố?
- 10. Tuyên bố hoặc Định nghĩa trong C
- 11. Làm thế nào để sử dụng nếu tuyên bố trong trường hợp khoản trong SQL?
- 12. Làm thế nào để đặt thời gian chờ cho NHibernate LINQ tuyên bố
- 13. Làm cách nào để chỉ định tệp cấu hình thay thế cho ứng dụng khách WCF?
- 14. Làm thế nào để vô hiệu hóa một tuyên bố loại bỏ từ local.xml trong Magento
- 15. C# Tuyên bố rỗng
- 16. Tuyên bố "CALLBACK" trong C làm gì?
- 17. Làm thế nào để thêm tiêu đề cột bảng để SQL SELECT tuyên bố
- 18. Làm thế nào để sử dụng Laravel Eloquent ORM để thực hiện một hoặc tuyên bố
- 19. IF Tuyên bố nhiều điều kiện, cùng một tuyên bố
- 20. Perl: Làm thế nào để tuyên bố refs mảng trống trong một hash mới?
- 21. Làm thế nào để bạn in bên trong một tuyên bố trường hợp trong SML?
- 22. Làm thế nào để sử dụng một hoặc trong một tuyên bố chuyển đổi tcl?
- 23. Làm thế nào để tìm kiếm trong nhiều lĩnh vực với OR tuyên bố trong ElasticSearch
- 24. Làm thế nào để cải thiện hiệu suất sqlite như tuyên bố
- 25. WF, WCF và dịch vụ khai báo (hoặc: Microsoft có ý nghĩa gì bởi "tuyên bố"?)
- 26. Tuyên bố SQLAlchemy: xác định trình kích hoạt và chỉ mục (Postgres 9)
- 27. Tuyên bố printf được giải thích như thế nào?
- 28. chỉ hủy bỏ các tuyên bố đơn đặt hàng
- 29. Biểu/Tuyên Bố
- 30. switch tuyên bố trong LINQ
Trong trường hợp của tôi, tôi đã hy vọng chỉ đi vào cấu hình và thêm hành vi chuẩn như vậy wsa: To Address không được chọn hoặc được yêu cầu làm tiêu đề; Nhưng tôi thấy rằng giải pháp của bạn, giống như nhiều người khác, yêu cầu bạn viết mã cho phần mở rộng chỉ giống nhau, đó là nhiều dòng dài; vậy tại sao lại phiền phức, đơn giản hơn là chỉ thêm thuộc tính trên dịch vụ? – joedotnot