ServiceContractAttribute クラス

定義

インターフェイスまたはクラスが、Windows Communication Foundation (WCF) アプリケーション内にサービス コントラクトを定義することを示します。

public ref class ServiceContractAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple=false, Inherited=false)]
public sealed class ServiceContractAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, Inherited=false)]
public sealed class ServiceContractAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple=false, Inherited=false)>]
type ServiceContractAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, Inherited=false)>]
type ServiceContractAttribute = class
    inherit Attribute
Public NotInheritable Class ServiceContractAttribute
Inherits Attribute
継承
ServiceContractAttribute
属性

次のコード例は、ServiceContractAttribute をインターフェイスに適用し、OperationContractAttribute によって示される 1 つのサービス メソッドを持つサービス コントラクトを定義する方法を示しています。 この場合、すべてのメッセージのバインドで要求される保護レベルは ProtectionLevel.EncryptAndSign です。

その後、このコード例では、定義したコントラクトを SampleService クラスで実装します。

using System;
using System.Collections.Generic;
using System.Net.Security;
using System.ServiceModel;
using System.Text;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(
    Namespace="http://microsoft.wcf.documentation",
    Name="SampleService",
    ProtectionLevel=ProtectionLevel.EncryptAndSign
  )]
  public interface ISampleService{
    [OperationContract]
    string SampleMethod(string msg);
  }

  class SampleService : ISampleService
  {
  #region ISampleService Members

  public string  SampleMethod(string msg)
  {
      return "The service greets you: " + msg;
  }

  #endregion
  }
}


Imports System.Collections.Generic
Imports System.Net.Security
Imports System.ServiceModel
Imports System.Text

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace:="http://microsoft.wcf.documentation", Name:="SampleService", ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
  Public Interface ISampleService
    <OperationContract> _
    Function SampleMethod(ByVal msg As String) As String
  End Interface

  Friend Class SampleService
      Implements ISampleService
  #Region "ISampleService Members"

  Public Function SampleMethod(ByVal msg As String) As String Implements ISampleService.SampleMethod
       Return "The service greets you: " & msg
  End Function

  #End Region
  End Class
End Namespace

次のコード例は、上のサービス用の 1 つのエンドポイントを作成する単純な構成ファイルを示しています。

<configuration>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="mex"
      >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService"/>
          </baseAddresses>
        </host>
        <endpoint
          address=""
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
         />
        <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange"
        />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mex">
          <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

次のコード例は、上の SampleService を呼び出す単純なクライアントを示しています。

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

public class Client
{
  public static void Main()
  {
    // Picks up configuration from the config file.
    SampleServiceClient wcfClient = new SampleServiceClient();
    try
    {
        // Making calls.
        Console.WriteLine("Enter the greeting to send: ");
        string greeting = Console.ReadLine();
        Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting));

        Console.WriteLine("Press ENTER to exit:");
        Console.ReadLine();

        // Done with service.
        wcfClient.Close();
        Console.WriteLine("Done!");
    }
    catch (TimeoutException timeProblem)
    {
      Console.WriteLine("The service operation timed out. " + timeProblem.Message);
      wcfClient.Abort();
      Console.Read();
    }
    catch(CommunicationException commProblem)
    {
      Console.WriteLine("There was a communication problem. " + commProblem.Message);
      wcfClient.Abort();
      Console.Read();
    }
  }
}


Imports System.ServiceModel
Imports System.ServiceModel.Channels

Public Class Client
  Public Shared Sub Main()
    ' Picks up configuration from the config file.
    Dim wcfClient As New SampleServiceClient()
    Try
        ' Making calls.
        Console.WriteLine("Enter the greeting to send: ")
            Dim greeting = Console.ReadLine()
        Console.WriteLine("The service responded: " & wcfClient.SampleMethod(greeting))

        Console.WriteLine("Press ENTER to exit:")
        Console.ReadLine()

        ' Done with service. 
        wcfClient.Close()
        Console.WriteLine("Done!")
    Catch timeProblem As TimeoutException
      Console.WriteLine("The service operation timed out. " & timeProblem.Message)
      wcfClient.Abort()
      Console.Read()
    Catch commProblem As CommunicationException
      Console.WriteLine("There was a communication problem. " & commProblem.Message)
      wcfClient.Abort()
      Console.Read()
    End Try
  End Sub
End Class

注釈

インターフェイス (またはクラス) に対して ServiceContractAttribute 属性を使用して、サービス コントラクトを定義します。 その後、1 つ以上のクラス (またはインターフェイス) のメソッドに対して OperationContractAttribute 属性を使用して、コントラクトのサービス操作を定義します。 サービス コントラクトが実装され、 Bindings とオブジェクトと EndpointAddress 組み合わされると、サービス コントラクトはクライアントによって使用するために公開されます。 簡単な例を使用したプロセスの概要については、「はじめに チュートリアル」を参照してください。 サービス コントラクトの作成の詳細については、「サービスの 設計と実装」を参照してください。

ServiceContractAttribute とそのインターフェイスで表現される情報は、Web サービス記述言語 (WSDL) の <portType> 要素に大まかに関連しています。 サービス コントラクトは、サービスのエンドポイントが呼び出し元に公開する内容を指定するために、サービス側で使用されます。 サービス コントラクトはクライアント側でも使用され、クライアントが通信を行うエンドポイントのコントラクトを指定します。双方向コントラクトの場合は、クライアントが双方向のメッセージ交換を行うために実装する必要があるコールバック コントラクトを (CallbackContract プロパティを使用して) 指定します。

注意

さらに、ServiceContractAttribute で修飾したインターフェイスまたはクラスがなんらかの機能を公開するには、OperationContractAttribute 属性でマークされた、少なくとも 1 つのメソッドを持つ必要があります。 この 2 つの属性を使用してサービスの定義と実装を行う最も単純な方法のコード例については、「使用例」のセクションを参照してください。

サービス コントラクトを変更するには、ServiceContractAttribute プロパティを使用します。

  • ConfigurationName プロパティは、使用する構成ファイル内のサービス要素の名前を指定します。

  • Name プロパティと Namespace プロパティは、WSDL の<portType> 要素内のコントラクトの名前と名前空間を制御します。

  • SessionMode プロパティは、コントラクトがセッションをサポートするバインディングを必要とするかどうかを指定します。

  • CallbackContract プロパティは、双方向 (二重) 通信で使用されるリターン コントラクトを指定します。

  • HasProtectionLevel プロパティと ProtectionLevel プロパティは、このコントラクトをサポートするすべてのメッセージに明示的な ProtectionLevel 値が設定されているかどうかを示し、設定されている場合はそのレベルを示します。

サービスはサービス コントラクトを実装します。サービス コントラクトは、サービスの種類がサポートするデータ交換を表します。 サービス クラスは、ServiceContractAttribute でマークされたメソッドを持つ OperationContractAttribute でマークされたインターフェイスを実装することで、サービス コントラクトを実装できます。または、ServiceContractAttribute でマークし、OperationContractAttribute 属性を独自のメソッドに適用できます。 (クラスでマークされたインターフェイスが実装されている ServiceContractAttribute場合は、それ自体を .で ServiceContractAttributeマークすることはできません)。でマークされているサービスの種類の OperationContractAttribute メソッドは、サービスの種類自体で指定された既定のサービス コントラクトの一部として扱われます。 サービス操作の詳細については、OperationContractAttribute を参照してください。

既定では、Name プロパティと Namespace プロパティは、それぞれコントラクトの種類とhttp://tempuri.org であり、ProtectionLevelProtectionLevel.None です。 サービス コントラクトでは、名前、名前空間、および保護レベルを、これらのプロパティを使用して明示的に設定することをお勧めします。 この作業により、次の 2 つの目標が達成されます。 第 1 に、マネージド型情報に直接接続されないコントラクトを構築し、コントラクトを WSDL で表現するときに、コントラクトを壊さずにマネージド コードと名前空間をリファクタリングできます。 第 2 に、コントラクト自体の保護レベルを明示的に要求することによって、ランタイムがバインド構成でそのレベルのセキュリティがサポートされているかどうかを検証でき、それによって、機密情報を漏洩する不良な構成を回避できます。 保護レベルの詳細については、「 保護レベルについて」を参照してください。

クライアント アプリケーションで使用するサービスを公開するには、サービス エンドポイントを Windows Communication Foundation (WCF) に登録するホスト アプリケーションを作成します。 Windows Activation Services (WAS) を使用して、コンソール アプリケーション、Windows サービス アプリケーション、ASP.NET アプリケーション、Windows フォーム アプリケーション、またはその他の種類のアプリケーション ドメインで WCF サービスをホストできます。

WAS でのホスティングは、ASP.NET アプリケーションの作成とほとんど同じです。 詳細については、「 方法: IIS で WCF サービスをホストする」を参照してください。

クライアントは、サービス コントラクト インターフェイス (ServiceContractAttribute でマークされたインターフェイス) を使用してサービスへのチャネルを作成するか、またはクライアント オブジェクト (サービス コントラクト インターフェイスの型情報と ClientBase<TChannel> クラスの組み合わせ) を使用してサービスと通信します。 サービスへのクライアント チャネルの詳細については、クラスと WCF クライアントの概要ChannelFactory<TChannel>参照してください。

ServiceContractAttribute クラスまたはインターフェイスを使用して別の ServiceContractAttribute クラスまたはインターフェイスから継承すると、親コントラクトが拡張されます。 たとえば、IChildContract インターフェイスが ServiceContractAttribute でマークされ、別のサービス コントラクト インターフェイスである IParentContract から継承された場合、IChildContract サービス コントラクトには、IParentContractIChildContract の両方のメソッドが含まれます。 コントラクト (クラスまたはインターフェイス) の拡張は、マネージド クラスとマネージド インターフェイスの拡張とほとんど同じです。

サービスを作成する最も柔軟な方法は、最初にサービス コントラクト インターフェイスを定義し、その後、サービス クラスでそのインターフェイスを実装することです。 (これは、他のユーザーによって定義されているサービス コントラクトを実装する必要がある場合に、サービスを構築する最も簡単な方法でもあります)。サービスが 1 つのコントラクトのみを公開する場合に、クラス ServiceContractAttribute とそのメソッドにマークを付 OperationContractAttribute けることでサービスを直接構築できます (ただし、そのコントラクトは複数のエンドポイントによって公開できます)。

このプロパティを使用して、元の CallbackContract サービス コントラクトと結び付けられたときに、2 つの方法で個別にフローできるメッセージ交換を定義する別のサービス コントラクトを示します。 詳細については、「CallbackContract」を参照してください。

コンストラクター

ServiceContractAttribute()

ServiceContractAttribute クラスの新しいインスタンスを初期化します。

プロパティ

CallbackContract

コントラクトが双方向コントラクトの場合のコールバック コントラクトの型を取得または設定します。

ConfigurationName

アプリケーション構成ファイル内でサービスを検索するために使用される名前を取得または設定します。

HasProtectionLevel

メンバーに保護レベルが割り当てられているかどうかを示す値を取得します。

Name

Web サービス記述言語 (WSDL) での <portType> 要素の名前を取得または設定します。

Namespace

Web サービス記述言語 (WSDL) での <portType> 要素の名前空間を取得または設定します。

ProtectionLevel

コントラクトのバインドで、ProtectionLevel プロパティの値をサポートする必要があるかどうかを指定します。

SessionMode

セッションが許可されるか、許可されないか、または必要であるかを示す値を取得または設定します。

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

(継承元 Attribute)

メソッド

Equals(Object)

このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。

(継承元 Attribute)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 Attribute)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象

こちらもご覧ください