0

この質問は複数回尋ねられたようですが、うまくいきません。

まず、Windows 7 と Windows 8 の両方で VS 2012 を使用してこれを試しましたが、まったく同じ問題が発生しました。

SOAP ヘッダー メッセージを受信して​​処理する WCF サービスを作成しようとしていますが、多くの検索の結果、メッセージ インターセプターを使用して SOAP ヘッダーを読み取って処理する必要があることがわかりました。

そこで、ローカル マシンに新しい Web サイトを作成し、DemoMessageInspector という名前を付けました。このソリューション内に、次のクラスを追加しました。

HmacVerificationBehavior

Imports System.ServiceModel.Description

Public Class HmacVerificationBehavior
    Implements IEndpointBehavior
  #Region "IEndpointBehavior Members"

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

End Sub

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

End Sub

Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, endpointDispatcher As System.ServiceModel.Dispatcher.EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
    Dim inspector As New HmacVerificationInspector()

    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector)
End Sub

Public Sub Validate(endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate

End Sub

#End Region

End Class

HmacVerificationConfigurationセクション

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

Namespace NamespaceHere

Public Class HmacVerificationConfigurationSection
    Inherits BehaviorExtensionElement
    Implements IServiceBehavior

#Region "IServiceBehavior Members"

    Public Sub AddBindingParameters(serviceDescription As ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase, endpoints As System.Collections.ObjectModel.Collection(Of ServiceEndpoint), bindingParameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IServiceBehavior.AddBindingParameters

    End Sub

    Public Sub ApplyDispatchBehavior(serviceDescription As ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase) Implements IServiceBehavior.ApplyDispatchBehavior

    End Sub

    Public Sub Validate(serviceDescription As ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase) Implements IServiceBehavior.Validate

    End Sub

#End Region

    Public Overrides ReadOnly Property BehaviorType() As Type
        Get
            Return GetType(HmacVerificationBehavior)
        End Get
    End Property

    Protected Overrides Function CreateBehavior() As Object
        Return New HmacVerificationBehavior()
    End Function

End Class

End Namespace

HmacVerificationInspector

Imports System.ServiceModel.Dispatcher
Imports System.ServiceModel.Channels

Public Class HmacVerificationInspector
Implements IDispatchMessageInspector


#Region "IDispatchMessageInspector Members"

Public Function AfterReceiveRequest(ByRef request As System.ServiceModel.Channels.Message, channel As System.ServiceModel.IClientChannel, instanceContext As System.ServiceModel.InstanceContext) As Object
    Dim buffer As MessageBuffer = request.CreateBufferedCopy(Int32.MaxValue)
    request = buffer.CreateMessage()
    Dim dupeRequest As Message = buffer.CreateMessage()

    ValidateHmac(dupeRequest)

    buffer.Close()

    Return Nothing
End Function

Public Sub BeforeSendReply(ByRef reply As System.ServiceModel.Channels.Message, correlationState As Object)


End Sub

#End Region

Public Function AfterReceiveRequest1(ByRef request As Message, channel As ServiceModel.IClientChannel, instanceContext As ServiceModel.InstanceContext) As Object Implements IDispatchMessageInspector.AfterReceiveRequest

End Function

Public Sub BeforeSendReply1(ByRef reply As Message, correlationState As Object) Implements IDispatchMessageInspector.BeforeSendReply

End Sub
End Class

Iサービス

Imports System.ServiceModel

' NOTE: You can use the "Rename" command on the context menu to change the interface name "IService" in both code and config file together.'
<ServiceContract()>
Public Interface IService

<OperationContract()>
Sub DoWork()

<OperationContract()>
Function DoSomething(ByVal _str As String) As String


End Interface

そしてサービス

Public Class Service
Implements IService

Public Sub DoWork() Implements IService.DoWork
End Sub

Public Function DoSomething(ByVal _str As String) As String Implements IService.DoSomething

    Return "Message [" & _str & "]"

End Function

End 

Web.Config ファイルは次のとおりです。

<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
   <extensions>
  <behaviorExtensions>
    <add name="hmacVerification" 
         type="NamespaceHere.HmacVerificationConfigurationSection, NamespaceHere, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
     </behaviorExtensions>
   </extensions>
  <services>
    <service name="MySecureService">
    <endpoint address="" binding="webHttpBinding" contract="IMySecureService" behaviorConfiguration="web"/>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp automaticFormatSelectionEnabled="true"/>
      <hmacVerification />      *********COMMENT OUT**************
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>
</configuration>

したがって、Web.Config で行がコメントアウトされている場合に完全に機能するサンプル コードがあります。コメントが解除されるとすぐに、次のことが起こります。

Server Error in '/DemoMessageInspector' Application.
--------------------------------------------------------------------------------


Configuration Error 
  Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

 Parser Error Message: The type 'NamespaceHere.HmacVerificationConfigurationSection, NamespaceHere, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' registered for extension 'hmacVerification' could not be loaded.

Source Error: 



Line 25:         <behavior name="web">
Line 26:           <webHttp automaticFormatSelectionEnabled="true"/>
Line 27:           <hmacVerification />
Line 28:         </behavior>
Line 29:       </endpointBehaviors>


 Source File:  C:\inetpub\wwwroot\DemoMessageInspector\web.config    Line:  27 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044 

それで、問題は基本的にこれをどのように構成する必要があるかです。

または、問題は、WCF エンドポイントから SOAP ヘッダーを読み取るという単純なタスクを完了するために使用する正しいアプローチですか?

4

1 に答える 1

2

「WCF エンドポイントから SOAP ヘッダーを読み取るという単純なタスクを完了する」だけでよい場合は、次を使用できるはずです。

OperationContext.Current.IncomingMessageHeaders

次のリンクには、IncomingMessageHeaders コレクションの実装の詳細が含まれています。

http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.incomingmessageheaders.aspx

于 2013-09-30T16:11:29.793 に答える