이 항목에서는 바인딩 및 바인딩 요소에 대한 구성 및 메타데이터 지원을 사용하도록 설정하는 방법을 설명합니다.
구성 및 메타데이터 개요
이 항목에서는 개발 채널 작업 목록의 선택적 항목 1, 2 및 4인 다음 작업에 대해 설명합니다 .
바인딩 요소에 대한 구성 파일 지원을 사용하도록 설정합니다.
바인딩에 대한 구성 파일 지원을 사용하도록 설정합니다.
바인딩 요소에 대한 WSDL 및 정책 어설션 내보내기
바인딩 또는 바인딩 요소를 삽입하고 구성하기 위한 WSDL 및 정책 어설션을 식별하기
사용자 정의 바인딩 및 바인딩 요소를 만드는 방법에 대한 자세한 내용은 User-Defined 바인딩 만들기 및 BindingElement 만들기를 각각 참조하세요.
구성 지원 추가
채널에 대한 구성 파일 지원을 활성화하려면, 바인딩 요소에 대한 구성 지원을 가능하게 하는 System.ServiceModel.Configuration.BindingElementExtensionElement와 바인딩에 대한 구성 지원을 가능하게 하는 System.ServiceModel.Configuration.StandardBindingElement 및 System.ServiceModel.Configuration.StandardBindingCollectionElement<TStandardBinding,TBindingConfiguration>의 두 구성 섹션을 구현해야 합니다.
이 작업을 수행하는 더 쉬운 방법은 ConfigurationCodeGenerator 샘플 도구를 사용하여 바인딩 및 바인딩 요소에 대한 구성 코드를 생성하는 것입니다.
BindingElementExtensionElement 네임스페이스 확장하기
다음 예제 코드는 전송: UDP 샘플에서 가져옵니다.
UdpTransportElement
는 BindingElementExtensionElement로, UdpTransportBindingElement
를 구성 시스템에 노출합니다. 몇 가지 기본 재정의를 사용하여 샘플은 구성 섹션 이름, 바인딩 요소의 형식 및 바인딩 요소를 만드는 방법을 정의합니다. 그러면 사용자는 다음과 같이 구성 파일에 확장 섹션을 등록할 수 있습니다.
<configuration>
<system.serviceModel>
<extensions>
<bindingElementExtensions>
<add name="udpTransport" type="Microsoft.ServiceModel.Samples.UdpTransportElement, UdpTransport" />
</bindingElementExtensions>
</extensions>
</system.serviceModel>
</configuration>
UDP를 전송으로 사용하기 위해 사용자 지정 바인딩에서 확장을 참조할 수 있습니다.
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding configurationName="UdpCustomBinding">
<udpTransport/>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
</configuration>
바인딩에 대한 구성 추가
섹션 SampleProfileUdpBindingCollectionElement
은 StandardBindingCollectionElement<TStandardBinding,TBindingConfiguration>으로써 SampleProfileUdpBinding
를 구성 시스템에 노출합니다. 구현의 대부분은 SampleProfileUdpBindingConfigurationElement
에서 파생된 StandardBindingElement에게 위임됩니다.
SampleProfileUdpBindingConfigurationElement
는 SampleProfileUdpBinding
의 속성과 일치하는 속성이 있으며, ConfigurationElement
바인딩에서 매핑하는 기능이 있습니다. 마지막으로, 다음 샘플 코드에 나타난 것처럼 OnApplyConfiguration
에서 메서드가 SampleProfileUdpBinding
재정의됩니다.
protected override void OnApplyConfiguration(string configurationName)
{
if (binding == null)
throw new ArgumentNullException("binding");
if (binding.GetType() != typeof(SampleProfileUdpBinding))
{
var expectedType = typeof(SampleProfileUdpBinding).AssemblyQualifiedName;
var typePassedIn = binding.GetType().AssemblyQualifiedName;
throw new ArgumentException($"Invalid type for binding. Expected type: {expectedType}. Type passed in: {typePassedIn}.");
}
SampleProfileUdpBinding udpBinding = (SampleProfileUdpBinding)binding;
udpBinding.OrderedSession = this.OrderedSession;
udpBinding.ReliableSessionEnabled = this.ReliableSessionEnabled;
udpBinding.SessionInactivityTimeout = this.SessionInactivityTimeout;
if (this.ClientBaseAddress != null)
udpBinding.ClientBaseAddress = ClientBaseAddress;
}
이 처리기를 구성 시스템에 등록하려면 다음 섹션을 관련 구성 파일에 추가합니다.
<configuration>
<configSections>
<sectionGroup name="system.serviceModel">
<sectionGroup name="bindings">
<section name="sampleProfileUdpBinding" type="Microsoft.ServiceModel.Samples.SampleProfileUdpBindingCollectionElement, UdpTransport" />
</sectionGroup>
</sectionGroup>
</configSections>
</configuration>
그런 다음 system.serviceModel< 구성 섹션에서 참조>할 수 있습니다.
<configuration>
<system.serviceModel>
<client>
<endpoint configurationName="calculator"
address="soap.udp://localhost:8001/"
bindingConfiguration="CalculatorServer"
binding="sampleProfileUdpBinding"
contract= "Microsoft.ServiceModel.Samples.ICalculatorContract">
</endpoint>
</client>
</system.serviceModel>
</configuration>
Binding 요소에 대한 메타데이터 지원 추가
채널을 메타데이터 시스템에 통합하려면 정책 가져오기 및 내보내기를 모두 지원해야 합니다. 이렇게 하면 ServiceModel 메타데이터 유틸리티 도구(Svcutil.exe) 와 같은 도구가 바인딩 요소의 클라이언트를 생성할 수 있습니다.
WSDL 지원 추가
바인딩의 전송 바인딩 요소는 메타데이터에서 주소 지정 정보를 내보내고 가져오는 역할을 합니다. SOAP 바인딩을 사용하는 경우 전송 바인딩 요소도 메타데이터에서 올바른 전송 URI를 내보내야 합니다. 다음 예제 코드는 전송: UDP 샘플에서 가져옵니다.
WSDL 내보내기
UdpTransportBindingElement
는 주소 지정 정보를 내보내기 위해 System.ServiceModel.Description.IWsdlExportExtension 인터페이스를 구현합니다. 이 메서드는 IWsdlExportExtension.ExportEndpoint WSDL 포트에 올바른 주소 지정 정보를 추가합니다.
if (context.WsdlPort != null)
{
AddAddressToWsdlPort(context.WsdlPort, context.Endpoint.Address, encodingBindingElement.MessageVersion.Addressing);
}
또한 이 메서드의 UdpTransportBindingElement
구현은 ExportEndpoint 엔드포인트에서 SOAP 바인딩을 사용하는 경우 전송 URI를 내보냅니다.
WsdlNS.SoapBinding soapBinding = GetSoapBinding(context, exporter);
if (soapBinding != null)
{
soapBinding.Transport = UdpPolicyStrings.UdpNamespace;
}
WSDL 가져오기
WSDL 가져오기 시스템을 확장하여 주소 가져오기를 처리하려면 Svcutil.exe.config 파일에 표시된 대로 Svcutil.exe 구성 파일에 다음 구성을 추가합니다.
<configuration>
<system.serviceModel>
<client>
<metadata>
<wsdlImporters>
<extension type=" Microsoft.ServiceModel.Samples.UdpBindingElementImporter, UdpTransport" />
</wsdlImporters>
</metadata>
</client>
</system.serviceModel>
</configuration>
Svcutil.exe을(를) 실행할 때 Svcutil.exe이 WSDL 가져오기 확장을 로드하도록 설정할 수 있는 두 가지 옵션이 있습니다.
/SvcutilConfig:<>file을 사용하여 구성 파일에 Svcutil.exe 가리킵니다.
Svcutil.exe동일한 디렉터리에 Svcutil.exe.config 구성 섹션을 추가합니다.
UdpBindingElementImporter
형식은 System.ServiceModel.Description.IWsdlImportExtension 인터페이스를 구현합니다. 이 메서드는 ImportEndpoint
WSDL 포트에서 주소를 가져옵니다.
BindingElementCollection bindingElements = context.Endpoint.Binding.CreateBindingElements();
TransportBindingElement transportBindingElement = bindingElements.Find<TransportBindingElement>();
if (transportBindingElement is UdpTransportBindingElement)
{
ImportAddress(context);
}
정책 지원 추가
사용자 지정 바인딩 요소는 서비스 엔드포인트에 대한 WSDL 바인딩에서 정책 어설션을 내보내 해당 바인딩 요소의 기능을 표현할 수 있습니다. 다음 예제 코드는 전송: UDP 샘플에서 가져옵니다.
정책 내보내기
UdpTransportBindingElement
유형은 정책 내보내기 지원을 추가하기 위해 System.ServiceModel.Description.IPolicyExportExtension을 구현합니다. 결과적으로 System.ServiceModel.Description.MetadataExporter는 이를 포함하는 모든 바인딩의 정책 생성에 UdpTransportBindingElement
을 포함합니다.
채널 IPolicyExportExtension.ExportPolicy이 멀티캐스트 모드인 경우 UDP에 대한 어설션과 다른 어설션을 추가합니다. 멀티캐스트 모드는 통신 스택이 생성되는 방식에 영향을 주므로 양쪽 간에 조정되어야 하기 때문입니다.
ICollection<XmlElement> bindingAssertions = context.GetBindingAssertions();
XmlDocument xmlDocument = new XmlDocument();
bindingAssertions.Add(xmlDocument.CreateElement(
UdpPolicyStrings.Prefix, UdpPolicyStrings.TransportAssertion, UdpPolicyStrings.UdpNamespace));
if (Multicast)
{
bindingAssertions.Add(xmlDocument.CreateElement(
UdpPolicyStrings.Prefix, UdpPolicyStrings.MulticastAssertion, UdpPolicyStrings.UdpNamespace));
}
주소 지정 처리를 담당하는 사용자 지정 전송 바인딩 요소는, System.ServiceModel.Description.IPolicyExportExtension 상의 구현 UdpTransportBindingElement
또한 사용 중인 WS-Addressing의 버전을 나타내기 위해 적절한 WS-Addressing 정책 어설션을 내보내기를 처리해야 합니다.
AddWSAddressingAssertion(context, encodingBindingElement.MessageVersion.Addressing);
정책 가져오기
정책 가져오기 시스템을 확장하려면 Svcutil.exe.config 파일에 표시된 대로 Svcutil.exe 구성 파일에 다음 구성을 추가합니다.
<configuration>
<system.serviceModel>
<client>
<metadata>
<policyImporters>
<extension type=" Microsoft.ServiceModel.Samples.UdpBindingElementImporter, UdpTransport" />
</policyImporters>
</metadata>
</client>
</system.serviceModel>
</configuration>
그런 다음 등록된 클래스(System.ServiceModel.Description.IPolicyImportExtension)에서 UdpBindingElementImporter
을(를) 구현합니다.
IPolicyImportExtension.ImportPolicy에서 적절한 네임스페이스의 어설션을 검사한 후, 전송을 생성하고 멀티캐스트인지 확인하기 위한 어설션을 처리합니다. 또한 가져오기가 바인딩 어설션 목록에서 처리하는 어설션을 제거합니다. 다시 Svcutil.exe실행할 때는 두 가지 통합 옵션이 있습니다.
/SvcutilConfig:<>file을 사용하여 구성 파일에 Svcutil.exe 가리킵니다.
Svcutil.exe동일한 디렉터리에 Svcutil.exe.config 구성 섹션을 추가합니다.
사용자 지정 표준 바인딩 가져오기 추가
기본적으로 Svcutil.exe 형식과 System.ServiceModel.Description.WsdlImporter 형식은 시스템에서 제공하는 바인딩을 인식하고 가져옵니다. 그렇지 않으면 바인딩이 System.ServiceModel.Channels.CustomBinding 인스턴스로 가져옵니다. Svcutil.exe 및 WsdlImporter를 사용하도록 설정하고 SampleProfileUdpBinding
을 가져오려면, UdpBindingElementImporter
도 사용자 지정 표준 바인딩 가져오기로 작동해야 합니다.
사용자 지정 표준 바인딩 가져오기는 ImportEndpoint
인터페이스에서 System.ServiceModel.Description.IWsdlImportExtension 메서드를 구현하여 메타데이터에서 가져온 System.ServiceModel.Channels.CustomBinding 인스턴스를 검사하여 특정 표준 바인딩에 의해 생성되었을 수 있는지 확인합니다.
if (context.Endpoint.Binding is CustomBinding)
{
Binding binding;
if (transportBindingElement is UdpTransportBindingElement)
{
//if TryCreate is true, the CustomBinding will be replace by a SampleProfileUdpBinding in the
//generated config file for better typed generation.
if (SampleProfileUdpBinding.TryCreate(bindingElements, out binding))
{
binding.Name = context.Endpoint.Binding.Name;
binding.Namespace = context.Endpoint.Binding.Namespace;
context.Endpoint.Binding = binding;
}
}
}
일반적으로 사용자 지정 표준 바인딩 가져오기를 구현하려면 가져온 바인딩 요소의 속성을 확인하여 표준 바인딩에서 설정할 수 있는 속성만 변경되었고 다른 모든 속성이 기본값인지 확인해야 합니다. 표준 바인딩 가져오기를 구현하기 위한 기본 전략은 표준 바인딩의 인스턴스를 만들고, 바인딩 요소의 속성을 표준 바인딩이 지원하는 표준 바인딩 인스턴스로 전파하고, 표준 바인딩의 바인딩 요소를 가져온 바인딩 요소와 비교하는 것입니다.