방법: 포트 공유를 사용하도록 Windows Communication Foundation 서비스 구성
WCF(Windows Communication Foundation) 응용 프로그램에서 net.tcp:// 포트 공유를 사용하는 가장 쉬운 방법은 NetTcpBinding을 사용하여 서비스를 노출하는 것입니다.
이 바인딩에서는 이 바인딩을 사용하여 구성하는 서비스에 대해 net.tcp:// 포트 공유를 사용할지 여부를 제어하는 PortSharingEnabled 속성을 제공합니다.
다음 절차에서는 NetTcpBinding 클래스를 사용하여 URI(Uniform Resource Identifier) net.tcp://localhost/MyService에서 끝점을 여는 방법을 보여 줍니다. 코드와 구성 요소를 사용한 방법을 차례로 보여 줍니다.
코드로 NetTcpBinding에서 net.tcp:// 포트 공유를 사용하도록 설정하려면
IMyService라는 계약을 구현할 서비스를 만든 후 MyService라는 이름을 지정합니다.
<ServiceContract()> _ Friend Interface IMyService 'Define the contract operations. End Interface Friend Class MyService Implements IMyService 'Implement the IMyService operations. End Class
[ServiceContract] interface IMyService { //Define the contract operations. } class MyService : IMyService { //Implement the IMyService operations. }
NetTcpBinding 클래스의 인스턴스를 만들고 PortSharingEnabled 속성을 true로 설정합니다.
Dim portsharingBinding As New NetTcpBinding() portsharingBinding.PortSharingEnabled = True
NetTcpBinding portsharingBinding = new NetTcpBinding(); portsharingBinding.PortSharingEnabled = true;
ServiceHost를 만든 다음 포트 공유 사용 NetTcpBinding을 사용하고 끝점 주소 URI "net.tcp://localhost/MyService"에서 수신 대기하는 MyService에 대해 서비스 끝점을 추가합니다.
Dim host As New ServiceHost(GetType(MyService)) host.AddServiceEndpoint(GetType(IMyService), portsharingBinding, "net.tcp://localhost/MyService")
ServiceHost host = new ServiceHost( typeof( MyService ) ); host.AddServiceEndpoint( typeof( IMyService ), portsharingBinding,"net.tcp://localhost/MyService" );
참고:
끝점 주소 URI에서 다른 포트 번호를 지정하지 않기 때문에 이 예제에서는 기본 TCP 포트 808을 사용합니다. 전송 바인딩에서 포트 공유가 명시적으로 사용되기 때문에 이 서비스는 다른 프로세스의 다른 서비스와 포트 808을 공유할 수 있습니다. 포트 공유가 허용되지 않고 다른 응용 프로그램에서 포트 808을 이미 사용하고 있는 경우 포트를 열면 이 서비스에서 AddressAlreadyInUseException을 throw합니다.
구성을 통해 NetTcpBinding에서 net.tcp:// 포트 공유를 사용하도록 설정하려면
- 다음 예제에서는 구성 요소를 사용하여 포트 공유를 사용하도록 설정하고 서비스 끝점을 추가하는 방법을 보여 줍니다.
<system.serviceModel>
<bindings>
<netTcpBinding name="portSharingBinding"
portSharingEnabled="true" />
<services>
<service name="MyService">
<endpoint address="net.tcp://localhost/MyService"
binding="netTcpBinding"
contract="IMyService"
bindingConfiguration="portSharingBinding" />
</service>
</services>
</system.serviceModel>
참고 항목
작업
방법: Net.TCP Port Sharing Service 사용