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 表示。 在此情況下,所有訊息之繫結所要求的保護層級為 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

下列程式碼範例是前述服務的一個簡單組態檔,此服務會建立一個端點。

<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 屬性來定義服務合約。 接著使用一個或多個類別 (或介面) 方法上的 OperationContractAttribute 屬性,定義合約的服務作業。 當服務合約實作並結合 BindingsEndpointAddress 物件時,服務合約會公開供用戶端使用。 如需使用簡單範例的程式概觀,請參閱消費者入門教學課程。 如需建立服務合約的詳細資訊,請參閱 設計和實作服務

ServiceContractAttribute 表示的資訊及其介面與 Web 服務描述語言 (WSDL) 的 <portType> 項目鬆散相關。 服務合約會用於服務端,以指定服務端點向呼叫端公開的內容。 用戶端亦會使用服務合約,來指定該用戶端所通訊之端點的合約,而在雙工合約中,也會指定用戶端必須實作的回呼合約 (使用 CallbackContract 屬性),以參與雙工對談。

注意

ServiceContractAttribute 裝飾的介面或類別至少要有一個以 OperationContractAttribute 屬性標記的方法,才能公開其功能。 請參閱<範例>一節中,使用這兩個屬性來定義並實作服務的最簡單程式碼範例。

使用 ServiceContractAttribute 屬性修改服務合約。

服務會實作服務合約,代表服務類型支援的資料交換。 服務類別可實作服務合約 (藉由實作以 ServiceContractAttribute 標記的介面,此介面有以 OperationContractAttribute 標記的方法),或可用 ServiceContractAttribute 標記此類別,並套用 OperationContractAttribute 屬性至此類別的方法 (如果類別實作標示 ServiceContractAttribute 為 的介面,它本身不能以 .) 方法標記 ServiceContractAttribute 在以 標示的服務類型上,會被視為服務類型 OperationContractAttribute 本身所指定之預設服務合約的一部分。 如需服務作業的詳細資訊,請參閱 OperationContractAttribute

根據預設,NameNamespace 屬性分別為合約類型名稱與 http://tempuri.org 的名稱,而 ProtectionLevel 則為 ProtectionLevel.None。 建議您使用這些屬性,明確設定服務合約的名稱、命名空間與保護層級。 如此可達成兩個目標, 首先,它會建置一份不會與 Managed 型別資訊直接連接的合約,可讓您在不破壞 WSDL 所表示的合約下,重構 Managed 程式碼與命名空間。 還有,在合約內明確要求某種保護層級,可讓執行階段驗證該繫結組態是否支援該安全性層級,避免因組態設定不當而洩漏敏感資訊。 如需保護層級的詳細資訊,請參閱 瞭解保護層級

若要公開用戶端應用程式使用的服務,請建立主應用程式,以向 Windows Communication Foundation (WCF) 註冊服務端點。 您可以在主控台應用程式中、Windows服務應用程式、ASP.NET 應用程式、Windows Forms應用程式或任何其他類型的應用程式域中,使用 Windows Activation Services (WAS) 裝載 WCF 服務。

在 WAS 裝載的方式與建立 ASP.NET 應用程式非常相似。 如需詳細資訊,請參閱 如何:在 IIS 中裝載 WCF 服務

用戶端使用服務合約介面 (以 ServiceContractAttribute 標示的介面) 建立通往服務的通道,或使用用戶端物件 (結合了服務合約介面的型別資訊與 ClientBase<TChannel> 類別) 與您的服務通訊。 如需服務用戶端通道的詳細資訊,請參閱 ChannelFactory<TChannel> 類別和 WCF 用戶端概觀

使用 ServiceContractAttribute 類別或介面來繼承另一個延伸了父合約的 ServiceContractAttribute 類別或介面。 例如,如果 IChildContract 介面以 ServiceContractAttribute 標記,並繼承了另一個服務合約介面 IParentContract,則 IChildContract 服務合約就包含了 IParentContractIChildContract 的方法。 延伸合約 (在類別或介面上) 與延伸 Managed 類別與介面非常相似。

建立服務最有彈性的方式,是先定義服務合約介面,再讓您的服務類別實作該介面 (如果您必須實作其他服務所定義的服務合約,則這是建置服務最簡單的方式。) 透過標記類別 ServiceContractAttribute 及其方法 OperationContractAttribute 直接建置服務,當服務只公開一個合約 (,但該合約可由多個端點公開) 時運作。

CallbackContract使用 屬性來表示另一個服務合約,當與原始服務合約系結時,請定義可以獨立以兩種方式流動的訊息交換。 如需詳細資訊,請參閱 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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱