서비스 계약(서비스 계약 디자인)을 설계하고 서비스 계약(서비스 계약 구현)을 구현한 후에는 서비스 런타임의 작업 동작을 구성할 수 있습니다. 이 항목에서는 시스템에서 제공하는 서비스 및 작업 동작에 대해 설명하고 새 동작을 만들기 위해 자세한 정보를 찾을 수 있는 위치를 설명합니다. 일부 동작은 특성으로 적용되지만 많은 동작은 애플리케이션 구성 파일을 사용하거나 프로그래밍 방식으로 적용됩니다. 서비스 애플리케이션을 구성하는 방법에 대한 자세한 내용은 서비스 구성을 참조하세요.
개요
계약은 해당 형식의 서비스의 입력, 출력, 데이터 형식 및 기능을 정의합니다. 서비스 계약을 구현하면, 클래스가 특정 주소에서 바인딩을 통해 구성되어 구현된 계약을 이행할 수 있게 됩니다. 계약, 바인딩 및 주소 정보는 모두 클라이언트에서 알 수 있습니다. 없으면 클라이언트가 서비스를 사용할 수 없습니다.
그러나 스레딩 문제 또는 인스턴스 관리와 같은 작업 세부 사항은 클라이언트에 불투명합니다. 서비스 계약을 구현한 후에는 동작을 사용하여 많은 수의 작업 특성을 구성할 수 있습니다. 동작은 런타임 속성을 설정하거나 사용자 지정 형식을 런타임에 삽입하여 WCF(Windows Communication Foundation) 런타임을 수정하는 개체입니다. 사용자 정의 동작을 만들어 런타임을 수정하는 방법에 대한 자세한 내용은 ServiceHost 및 서비스 모델 계층 확장을 참조하세요.
System.ServiceModel.ServiceBehaviorAttribute 및 System.ServiceModel.OperationBehaviorAttribute 특성은 가장 널리 유용한 동작을 제공하며, 가장 일반적으로 요청되는 작업 기능을 노출합니다. 특성이므로 서비스 또는 작업 구현에 적용합니다. 다른 동작, 예를 들어 System.ServiceModel.Description.ServiceMetadataBehavior나 System.ServiceModel.Description.ServiceDebugBehavior, 은 일반적으로 애플리케이션 구성 파일을 사용하여 적용되지만, 프로그래밍 방식으로 사용할 수도 있습니다.
이 주제는 ServiceBehaviorAttribute 및 OperationBehaviorAttribute 특성에 대한 개요를 제공하고, 동작이 작동할 수 있는 다양한 범위에 대해 설명하며, 다양한 범위에서 WCF 개발자가 관심을 가질 만한 시스템 제공 동작에 대해 간략하게 설명합니다.
ServiceBehaviorAttribute 및 OperationBehaviorAttribute
가장 중요한 동작은 제어하는 데 사용할 수 있는 ServiceBehaviorAttribute 속성과 OperationBehaviorAttribute 속성입니다.
인스턴스 라이프타임
동시성 및 동기화 지원
설정 동작 방식
트랜잭션 동작
Serialization 작동 방식
메타데이터 변환
세션 수명
주소 필터링 및 헤더 처리
사칭
이러한 특성을 사용하려면 해당 범위에 적합한 특성으로 서비스 또는 작업 구현을 표시하고 속성을 설정합니다. 예를 들어, 다음 코드 예제는 호출자가 이 작업을 수행하기 위해 대리 실행을 지원하도록 요구하는 OperationBehaviorAttribute.Impersonation 속성을 사용하는 작업 구현을 보여줍니다.
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;
namespace Microsoft.WCF.Documentation
{
[ServiceContract(
Name="SampleHello",
Namespace="http://microsoft.wcf.documentation"
)]
public interface IHello
{
[OperationContract]
string Hello(string greeting);
}
public class HelloService : IHello
{
public HelloService()
{
Console.WriteLine("Service object created: " + this.GetHashCode().ToString());
}
~HelloService()
{
Console.WriteLine("Service object destroyed: " + this.GetHashCode().ToString());
}
[OperationBehavior(Impersonation=ImpersonationOption.Required)]
public string Hello(string greeting)
{
Console.WriteLine("Called by: " + Thread.CurrentPrincipal.Identity.Name);
Console.WriteLine("IsAuthenticated: " + Thread.CurrentPrincipal.Identity.IsAuthenticated.ToString());
Console.WriteLine("AuthenticationType: " + Thread.CurrentPrincipal.Identity.AuthenticationType.ToString());
Console.WriteLine("Caller sent: " + greeting);
Console.WriteLine("Sending back: Hi, " + Thread.CurrentPrincipal.Identity.Name);
return "Hi, " + Thread.CurrentPrincipal.Identity.Name;
}
}
}
Imports System.ServiceModel
Imports System.Threading
Namespace Microsoft.WCF.Documentation
<ServiceContract(Name:="SampleHello", Namespace:="http://microsoft.wcf.documentation")> _
Public Interface IHello
<OperationContract> _
Function Hello(ByVal greeting As String) As String
End Interface
Public Class HelloService
Implements IHello
Public Sub New()
Console.WriteLine("Service object created: " & Me.GetHashCode().ToString())
End Sub
Protected Overrides Sub Finalize()
Console.WriteLine("Service object destroyed: " & Me.GetHashCode().ToString())
End Sub
<OperationBehavior(Impersonation:=ImpersonationOption.Required)> _
Public Function Hello(ByVal greeting As String) As String Implements IHello.Hello
Console.WriteLine("Called by: " & Thread.CurrentPrincipal.Identity.Name)
Console.WriteLine("IsAuthenticated: " & Thread.CurrentPrincipal.Identity.IsAuthenticated.ToString())
Console.WriteLine("AuthenticationType: " & Thread.CurrentPrincipal.Identity.AuthenticationType.ToString())
Console.WriteLine("Caller sent: " & greeting)
Console.WriteLine("Sending back: Hi, " & Thread.CurrentPrincipal.Identity.Name)
Return "Hi, " & Thread.CurrentPrincipal.Identity.Name
End Function
End Class
End Namespace
대부분의 속성에는 바인딩에서 추가 지원이 필요합니다. 예를 들어 클라이언트에서 트랜잭션이 필요한 작업은 흐름된 트랜잭션을 지원하는 바인딩을 사용하도록 구성해야 합니다.
Well-Known Singleton Services
ServiceBehaviorAttribute 및 OperationBehaviorAttribute 특성을 사용하여 InstanceContext와 작업을 구현하는 서비스 개체들의 특정 수명을 제어할 수 있습니다.
예를 들어, ServiceBehaviorAttribute.InstanceContextMode 속성은 InstanceContext의 릴리스 빈도를 제어하고, OperationBehaviorAttribute.ReleaseInstanceMode 및 ServiceBehaviorAttribute.ReleaseServiceInstanceOnTransactionComplete 속성은 서비스 개체가 해제될 때를 제어합니다.
그러나 서비스 개체를 직접 만들고 해당 개체를 사용하여 서비스 호스트를 만들 수도 있습니다. 이렇게 하려면 ServiceBehaviorAttribute.InstanceContextMode 속성을 ServiceBehaviorAttribute.InstanceContextMode 또는 로 설정해야 합니다. 그렇지 않으면 서비스 호스트가 열릴 때 예외가 발생합니다.
ServiceHost(Object, Uri[]) 생성자를 사용하여 이러한 서비스를 만듭니다. 단일 서비스에서 사용할 특정 개체 인스턴스를 제공하려는 경우 사용자 지정 System.ServiceModel.Dispatcher.IInstanceContextInitializer 을 구현하는 대신 사용할 수 있습니다. 서비스 구현 형식을 생성하기 어려운 경우(예: 매개 변수가 없는 공용 생성자를 구현하지 않는 경우) 이 오버로드를 사용할 수 있습니다.
이 생성자에 개체가 제공되면 WCF(Windows Communication Foundation) 인스턴스화 동작과 관련된 일부 기능이 다르게 작동합니다. 예를 들어 잘 알려진 개체 인스턴스가 제공되면 호출 InstanceContext.ReleaseServiceInstance 에 영향을 주지 않습니다. 마찬가지로 다른 인스턴스 릴리스 메커니즘도 무시됩니다. ServiceHost 클래스는 항상 OperationBehaviorAttribute.ReleaseInstanceMode 속성이 모든 작업에 대해 ReleaseInstanceMode.None으로 설정된 것처럼 동작합니다.
기타 서비스, 엔드포인트, 계약 및 작업 동작
서비스 행동, 예를 들어 ServiceBehaviorAttribute 속성, 은 전체 서비스에서 작동합니다. 예를 들어 ServiceBehaviorAttribute.ConcurrencyMode 속성을 ConcurrencyMode.Multiple로 설정하면 해당 서비스의 각 작업 내에서 스레드 동기화 문제를 직접 관리해야 합니다. 엔드포인트 동작은 엔드포인트에서 작동합니다. 많은 시스템 제공 엔드포인트 동작은 클라이언트 기능을 위한 것입니다. 계약 동작은 계약의 단계에서 작동하고 작업 동작은 작업 수행을 수정합니다.
이러한 동작의 대부분은 특성에 구현되며, 이를 ServiceBehaviorAttribute 및 OperationBehaviorAttribute 특성을 사용하는 것처럼 적절한 서비스 클래스나 작업 구현에 적용합니다. 다른 동작(예: ServiceMetadataBehavior 및 ServiceDebugBehavior 개체)은 프로그래밍 방식으로도 사용할 수 있지만, 일반적으로 애플리케이션 구성 파일을 사용하여 적용됩니다.
예를 들어, 메타데이터 게시 구성은 ServiceMetadataBehavior 개체로 구성됩니다. 다음 애플리케이션 구성 파일은 가장 일반적인 사용량을 보여 줍니다.
<configuration>
<system.serviceModel>
<services>
<service
name="Microsoft.WCF.Documentation.SampleService"
behaviorConfiguration="metadataSupport"
>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/SampleService" />
</baseAddresses>
</host>
<endpoint
address=""
binding="wsHttpBinding"
contract="Microsoft.WCF.Documentation.ISampleService"
/>
<!-- Adds a WS-MetadataExchange endpoint at -->
<!-- "http://localhost:8080/SampleService/mex" -->
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataSupport">
<!-- Enables the IMetadataExchange endpoint in services that -->
<!-- use "metadataSupport" in their behaviorConfiguration attribute. -->
<!-- In addition, the httpGetEnabled and httpGetUrl attributes publish -->
<!-- Service metadata for retrieval by HTTP/GET at the address -->
<!-- "http://localhost:8080/SampleService?wsdl" -->
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
다음 섹션에서는 서비스 또는 클라이언트의 런타임 배달을 수정하는 데 사용할 수 있는 가장 유용한 시스템 제공 동작에 대해 설명합니다. 각 항목을 사용하는 방법을 확인하려면 참조 항목을 참조하세요.
서비스 동작
다음 동작은 서비스에서 작동합니다.
AspNetCompatibilityRequirementsAttribute; ASP.NET 호환 모드에서 해당 서비스를 실행할 수 있는지 여부를 나타내기 위해 WCF 서비스에 적용됩니다.
ServiceAuthorizationBehavior; 서비스에서 클라이언트 클레임에 권한을 부여하는 방법을 제어합니다.
ServiceCredentials; 서비스 자격 증명을 구성합니다. 이 클래스를 사용하여 X.509 인증서와 같은 서비스에 대한 자격 증명을 지정합니다.
ServiceDebugBehavior; WCF 서비스에 대한 디버깅 및 도움말 정보 기능을 사용하도록 설정합니다.
ServiceMetadataBehavior; 서비스 메타데이터 및 관련 정보의 게시를 제어합니다.
ServiceSecurityAuditBehavior; 보안 이벤트의 감사 동작을 지정합니다.
ServiceThrottlingBehavior; 서비스 성능을 조정할 수 있는 런타임 처리량 설정을 구성합니다.
엔드포인트 동작
다음 동작은 엔드포인트에서 작동합니다. 이러한 동작의 대부분은 클라이언트 애플리케이션에서 사용됩니다.
CallbackBehaviorAttribute; 이중 클라이언트 애플리케이션에서 콜백 서비스 구현을 구성합니다.
CallbackDebugBehavior; WCF 콜백 개체에 대한 서비스 디버깅을 사용하도록 설정합니다.
ClientCredentials; 사용자가 클라이언트 및 서비스 자격 증명뿐만 아니라 클라이언트에서 사용할 서비스 자격 증명 인증 설정을 구성할 수 있습니다.
ClientViaBehavior; 클라이언트에서 전송 채널을 만들어야 하는 URI(Uniform Resource Identifier)를 지정하는 데 사용됩니다.
MustUnderstandBehavior; WCF에
MustUnderstand
처리 비활성화를 지시합니다.SynchronousReceiveBehavior; 채널에 대해 동기 수신 프로세스를 사용하도록 런타임에 지시합니다.
TransactedBatchingBehavior; 트랜잭션 수신을 지원하는 전송에 대한 수신 작업을 최적화합니다.
계약 행동
DeliveryRequirementsAttribute; 바인딩이 서비스 또는 클라이언트 구현에 제공해야 하는 기능 요구 사항을 지정합니다.
작업 동작
다음 작업 동작은 작업에 대한 serialization 및 트랜잭션 컨트롤을 지정합니다.
DataContractSerializerOperationBehavior; System.Runtime.Serialization.DataContractSerializer의 런타임 동작을 나타냅니다.
XmlSerializerOperationBehavior;
XmlSerializer
의 런타임 동작을 제어하고 작업에 연관시킵니다.TransactionFlowAttribute; 서비스 작업에서 트랜잭션 헤더를 허용하는 수준을 지정합니다.