方法: カスタム ポリシー アサーションをエクスポートする

ポリシー アサーションはサービス エンドポイントの機能と要件を説明します。 サービス アプリケーションは、サービス メタデータに含まれるカスタム ポリシー アサーションを使用して、エンドポイントのバインディングまたはコントラクトのカスタマイズ情報をクライアント アプリケーションに伝達します。 Windows Communication Foundation (WCF) を使用すると、伝達している機能または要件に応じて、エンドポイント、操作、またはメッセージ サブジェクトで WSDL バインディングに結び付けられているポリシー表現のアサーションをエクスポートできます。

カスタム ポリシー アサーションは、System.ServiceModel.Description.IPolicyExportExtension インターフェイスを System.ServiceModel.Channels.BindingElement に実装して、サービス エンドポイントのバインディングにバインド要素を直接挿入するか、またはアプリケーション構成ファイルにバインド要素を登録することによってエクスポートされます。 ポリシーのエクスポートの実装では、System.Xml.XmlElement メソッドに渡された System.ServiceModel.Description.PolicyAssertionCollection の適切な System.ServiceModel.Description.PolicyConversionContext に、ExportPolicy インスタンスとしてカスタム ポリシー アサーションを追加する必要があります。

また、PolicyVersion クラスの WsdlExporter プロパティをチェックし、入れ子になったポリシー表現およびポリシー フレームワーク属性を、指定されたポリシー バージョンに基づいた正しい名前空間にエクスポートする必要もあります。

カスタム ポリシー アサーションをインポートするには、「System.ServiceModel.Description.IPolicyImportExtension」 および「方法: カスタム ポリシー アサーションをインポートする」を参照してください。

カスタム ポリシー アサーションをエクスポートするには

  1. System.ServiceModel.Description.IPolicyExportExtensionSystem.ServiceModel.Channels.BindingElement インターフェイスを実装します。 バインディング レベルでのカスタム ポリシー アサーションの実装を次のコード例に示します。

    #region IPolicyExporter Members
    public void ExportPolicy(MetadataExporter exporter, PolicyConversionContext policyContext)
    {
      if (exporter == null)
        throw new NullReferenceException("The MetadataExporter object passed to the ExporterBindingElement is null.");
      if (policyContext == null)
        throw new NullReferenceException("The PolicyConversionContext object passed to the ExporterBindingElement is null.");
    
      XmlElement elem = doc.CreateElement(name1, ns1);
      elem.InnerText = "My custom text.";
      XmlAttribute att = doc.CreateAttribute("MyCustomAttribute", ns1);
      att.Value = "ExampleValue";
      elem.Attributes.Append(att);
      XmlElement subElement = doc.CreateElement("MyCustomSubElement", ns1);
      subElement.InnerText = "Custom Subelement Text.";
      elem.AppendChild(subElement);
      policyContext.GetBindingAssertions().Add(elem);
      Console.WriteLine("The custom policy exporter was called.");
    }
    #endregion
    
    #Region "IPolicyExporter Members"
            Public Sub ExportPolicy(ByVal exporter As MetadataExporter, ByVal policyContext As PolicyConversionContext) Implements IPolicyExportExtension.ExportPolicy
                If exporter Is Nothing Then
                    Throw New NullReferenceException("The MetadataExporter object passed to the ExporterBindingElement is null.")
                End If
                If policyContext Is Nothing Then
                    Throw New NullReferenceException("The PolicyConversionContext object passed to the ExporterBindingElement is null.")
                End If
    
                Dim elem As XmlElement = doc.CreateElement(name1, ns1)
                elem.InnerText = "My custom text."
                Dim att As XmlAttribute = doc.CreateAttribute("MyCustomAttribute", ns1)
                att.Value = "ExampleValue"
                elem.Attributes.Append(att)
                Dim subElement As XmlElement = doc.CreateElement("MyCustomSubElement", ns1)
                subElement.InnerText = "Custom Subelement Text."
                elem.AppendChild(subElement)
                policyContext.GetBindingAssertions().Add(elem)
                Console.WriteLine("The custom policy exporter was called.")
            End Sub
    #End Region
    
  2. プログラムまたはアプリケーション構成ファイルを使用して、エンドポイントのバインディングにバインド要素を挿入します。 次の手順を参照してください。

アプリケーション構成ファイルを使用してバインディングを挿入するには

  1. カスタム ポリシー アサーション バインド要素の System.ServiceModel.Configuration.BindingElementExtensionElement を実装します。

  2. <bindingElementExtensions> 要素を使用して、そのバインド要素拡張を構成ファイルに追加します。

  3. System.ServiceModel.Channels.CustomBinding を使用してカスタム バインドを作成します。

プログラムでバインド要素を挿入するには

  1. 新しい System.ServiceModel.Channels.BindingElement を作成して System.ServiceModel.Channels.CustomBinding に追加します。

  2. 手順 1. のカスタム バインドを 新しいエンドポイントに追加し、System.ServiceModel.ServiceHost メソッドを呼び出してその新しいサービス エンドポイントを AddServiceEndpoint に追加します。

  3. ServiceHost を開きます。 カスタム バインディングの作成と、プログラムによるバインド要素の挿入を次のコード例に示します。

    Uri baseAddress = new Uri("http://localhost:8000/servicemodelsamples/service");
    
    // Create a ServiceHost for the CalculatorService type and provide the base address.
    using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress))
    {
        // Create a custom binding that contains two binding elements.
        ReliableSessionBindingElement reliableSession = new ReliableSessionBindingElement();
        reliableSession.Ordered = true;
    
        HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
        httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
        httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    
        CustomBinding binding = new CustomBinding(reliableSession, httpTransport);
    
        // Add an endpoint using that binding.
        serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, "");
    
        // Add a MEX endpoint.
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        smb.HttpGetUrl = new Uri("http://localhost:8001/servicemodelsamples");
        serviceHost.Description.Behaviors.Add(smb);
    
        // Open the ServiceHostBase to create listeners and start listening for messages.
        serviceHost.Open();
    
        // The service can now be accessed.
        Console.WriteLine("The service is ready.");
        Console.WriteLine("Press <ENTER> to terminate service.");
        Console.WriteLine();
        Console.ReadLine();
    
        // Close the ServiceHostBase to shutdown the service.
        serviceHost.Close();
    }
    
    Dim baseAddress As New Uri("http://localhost:8000/servicemodelsamples/service")
    
    ' Create a ServiceHost for the CalculatorService type and provide the base address.
    Using serviceHost As New ServiceHost(GetType(CalculatorService), baseAddress)
        ' Create a custom binding that contains two binding elements.
        Dim reliableSession As New ReliableSessionBindingElement()
        reliableSession.Ordered = True
    
        Dim httpTransport As New HttpTransportBindingElement()
        httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous
        httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard
    
        Dim binding As New CustomBinding(reliableSession, httpTransport)
    
        ' Add an endpoint using that binding.
        serviceHost.AddServiceEndpoint(GetType(ICalculator), binding, "")
    
        ' Add a MEX endpoint.
        Dim smb As New ServiceMetadataBehavior()
        smb.HttpGetEnabled = True
        smb.HttpGetUrl = New Uri("http://localhost:8001/servicemodelsamples")
        serviceHost.Description.Behaviors.Add(smb)
    
        ' Open the ServiceHostBase to create listeners and start listening for messages.
        serviceHost.Open()
    
        ' The service can now be accessed.
        Console.WriteLine("The service is ready.")
        Console.WriteLine("Press <ENTER> to terminate service.")
        Console.WriteLine()
        Console.ReadLine()
    
        ' Close the ServiceHostBase to shutdown the service.
        serviceHost.Close()
    End Using
    

関連項目