ServiceContractAttribute 클래스

정의

인터페이스 또는 클래스에서 WCF(Windows Communication Foundation) 애플리케이션의 서비스 계약을 정의함을 나타냅니다.

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 계약의 서비스 작업을 정의합니다. 서비스 계약을 구현하고 Bindings 및 개체와 EndpointAddress 결합하면 클라이언트에서 사용할 서비스 계약이 노출됩니다. 간단한 예제를 사용하는 프로세스에 대한 개요는 시작 자습서를 참조하세요. 서비스 계약을 만드는 방법에 대한 자세한 내용은 서비스 디자인 및 구현을 참조하세요.

A ServiceContractAttribute 및 해당 인터페이스로 표현되는 정보는 WSDL(Web Services Description Language) <portType> 요소와 느슨하게 관련됩니다. 서비스 계약은 서비스 쪽에서 서비스의 엔드포인트가 호출자에게 노출하는 내용을 지정하는 데 사용됩니다. 또한 클라이언트 쪽에서 클라이언트가 통신하는 엔드포인트의 계약을 지정하고 이중 계약의 경우 이중 대화에 참여하기 위해 클라이언트가 구현해야 하는 콜백 계약(속성 사용 CallbackContract )을 지정하는 데 사용됩니다.

참고

또한 데코레이팅된 ServiceContractAttribute 인터페이스 또는 클래스에는 기능을 노출하기 위해 특성으로 OperationContractAttribute 표시된 메서드가 하나 이상 있어야 합니다. 서비스를 정의하고 구현하기 위해 두 특성을 가장 간단하게 사용하는 코드 예제는 예제 섹션을 참조하세요.

ServiceContractAttribute 속성을 사용하여 서비스 계약을 수정합니다.

  • 이 속성은 ConfigurationName 사용할 구성 파일의 서비스 요소 이름을 지정합니다.

  • Namespace 속성은 Name WSDL <portType> 요소에서 계약의 이름과 네임스페이스를 제어합니다.

  • 이 속성은 SessionMode 계약에 세션을 지원하는 바인딩이 필요한지 여부를 지정합니다.

  • 이 속성은 CallbackContract 양방향(이중) 대화에서 반환 계약을 지정합니다.

  • ProtectionLevel 속성은 HasProtectionLevel 계약을 지원하는 모든 메시지에 명시적 값이 있는지 여부와 명시적 ProtectionLevel 값이 있는 경우 해당 수준이 무엇인지를 나타냅니다.

서비스는 서비스 유형이 지원하는 데이터 교환을 나타내는 서비스 계약을 구현합니다. 서비스 클래스는 메서드가 표시된 ServiceContractAttribute 인터페이스를 구현하여 서비스 계약을 구현하거나 해당 메서드를 OperationContractAttributeServiceContractAttribute 사용하여 표시하고 특성을 자체 메서드에 적용 OperationContractAttribute 할 수 있습니다. (클래스가 표시된 ServiceContractAttribute인터페이스를 구현하는 경우 자체에서 .로 ServiceContractAttribute표시될 수 없습니다.) 표시된 서비스 형식의 메서드는 OperationContractAttribute 서비스 유형 자체에서 지정한 기본 서비스 계약의 일부로 처리됩니다. 서비스 작업에 대한 자세한 내용은 다음을 참조하세요 OperationContractAttribute.

기본적으로 및 Name Namespace 속성은 계약 형식의 이름이며 http://tempuri.org, 각각입니다 ProtectionLevel ProtectionLevel.None. 서비스 계약은 이러한 속성을 사용하여 이름, 네임스페이스 및 보호 수준을 명시적으로 설정하는 것이 좋습니다. 이렇게 하면 두 가지 목표가 달성됩니다. 먼저 관리되는 형식 정보에 직접 연결되지 않은 계약을 빌드하여 WSDL로 표현된 대로 계약을 위반하지 않고 관리 코드 및 네임스페이스를 리팩터링할 수 있습니다. 둘째, 계약 자체에 대해 특정 수준의 보호를 명시적으로 요구하면 런타임에서 바인딩 구성이 해당 수준의 보안을 지원하는지 유효성을 검사하여 잘못된 구성으로 인해 중요한 정보가 노출되지 않도록 할 수 있습니다. 보호 수준에 대한 자세한 내용은 보호 수준 이해를 참조하세요.

클라이언트 애플리케이션에서 사용할 수 있도록 서비스를 노출 하려면 Windows Communication Foundation (WCF)를 사용 하 여 서비스 엔드포인트을 등록 하는 호스트 애플리케이션을 만듭니다. 콘솔 애플리케이션, Windows 서비스 애플리케이션, ASP.NET 애플리케이션, Windows Forms 애플리케이션 또는 다른 유형의 애플리케이션 도메인에서 Windows 정품 인증 서비스 (WAS)를 사용 하 여 WCF 서비스를 호스트할 수 있습니다.

WAS에서 호스팅하는 것은 ASP.NET 애플리케이션을 만드는 것과 매우 비슷합니다. 자세한 내용은 방법: IIS에서 WCF 서비스 호스트를 참조하세요.

클라이언트는 서비스 계약 인터페이스(표시된 ServiceContractAttribute인터페이스)를 사용하여 서비스에 대한 채널을 만들거나 클라이언트 개체(서비스 계약 인터페이스의 형식 정보를 클래스와 ClientBase<TChannel> 결합)를 사용하여 서비스와 통신합니다. 서비스에 대한 클라이언트 채널에 대한 자세한 내용은 클래스 및 WCF 클라이언트 개요를 참조 ChannelFactory<TChannel> 하세요.

클래스 또는 인터페이스를 ServiceContractAttribute 사용하여 다른 ServiceContractAttribute 클래스 또는 인터페이스에서 상속하면 부모 계약이 확장됩니다. 예를 들어 인터페이스가 IChildContract 다른 서비스 계약 인터페이스로 ServiceContractAttribute 표시되고 다른 서비스 계약 인터페이스 IParentContract``IChildContract 에서 상속되는 경우 서비스 계약에는 둘 다 IParentContract 의 메서드가 IChildContract포함됩니다. 계약 확장(클래스 또는 인터페이스에 관계없이)은 관리되는 클래스 및 인터페이스를 확장하는 것과 매우 유사합니다.

서비스를 만드는 가장 유연한 방법은 먼저 서비스 계약 인터페이스를 정의한 다음 서비스 클래스가 해당 인터페이스를 구현하도록 하는 것입니다. (다른 사용자가 정의한 서비스 계약을 구현해야 하는 경우 서비스를 빌드하는 가장 간단한 방법이기도 합니다.) 서비스가 하나의 계약만 노출하는 경우 클래스 및 ServiceContractAttribute 해당 메서드를 OperationContractAttribute 사용하여 직접 서비스를 빌드합니다(하지만 해당 계약은 둘 이상의 엔드포인트에서 노출될 수 있습니다).

CallbackContract 속성을 사용하여 원래 서비스 계약과 함께 바인딩된 경우 두 가지 방법으로 독립적으로 흐를 수 있는 메시지 교환을 정의하는 다른 서비스 계약을 나타냅니다. 자세한 내용은 CallbackContract를 참조하십시오.

생성자

ServiceContractAttribute()

ServiceContractAttribute 클래스의 새 인스턴스를 초기화합니다.

속성

CallbackContract

계약이 이중 계약인 경우 콜백 계약의 형식을 가져오거나 설정합니다.

ConfigurationName

애플리케이션 구성 파일에서 서비스를 찾는 데 사용되는 이름을 가져오거나 설정합니다.

HasProtectionLevel

멤버에 보호 수준이 할당되어 있는지 여부를 나타내는 값을 가져옵니다.

Name

WSDL(웹 서비스 기술 언어)에서 <portType> 요소의 이름을 가져오거나 설정합니다.

Namespace

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)

적용 대상

추가 정보