방법: ChannelFactory 사용
제네릭 ChannelFactory 클래스는 둘 이상의 채널을 만드는 데 사용할 수 있는 채널 팩터리를 만들어야 하는 고급 시나리오에서 사용됩니다.
ChannelFactory 클래스를 만들고 사용하려면
WCF(Windows Communication Foundation) 서비스를 빌드하고 실행합니다. 자세한 내용은 다음 항목을 참조하십시오. 서비스 디자인 및 구현, 서비스 구성 및 서비스 호스팅을 참조하십시오.
ServiceModel Metadata 유틸리티 도구(Svcutil.exe)를 사용하여 클라이언트에 대한 계약(인터페이스)을 생성합니다.
클라이언트 코드에서 ChannelFactory 클래스를 사용하여 여러 개의 끝점 수신기를 만듭니다.
예제
Imports System
Imports System.ServiceModel
' This code generated by svcutil.exe.
<ServiceContract()> _
Interface IMath
<OperationContract()> _
Function Add(ByVal A As Double, ByVal B As Double) As Double
End Interface
public class Math
Implements IMath
Function Add(ByVal A As Double, ByVal B As Double) As Double Implements IMath.Add
Return A + B
End Function
End Class
Public Class Test
Public Shared Sub Main()
End Sub
Public Sub Run()
' This code is written by an application developer.
' Create a channel factory.
Dim myBinding As New BasicHttpBinding
Dim myEndpoint As New EndpointAddress("https://localhost/MathService/Ep1")
Dim myChannelFactory As ChannelFactory(Of IMath) = _
New ChannelFactory(Of IMath) (myBinding, myEndpoint)
'Create a channel.
Dim wcfClient1 As IMath = myChannelFactory.CreateChannel()
Dim s As Integer = wcfClient1.Add(3, 39)
Console.WriteLine(s.ToString())
Dim clientChannel As IClientChannel = CType(wcfClient1, IClientChannel)
clientChannel.Close()
' Create another channel
Dim wcfClient2 As IMath = myChannelFactory.CreateChannel()
s = wcfClient2.Add(15, 27)
Console.WriteLine(s.ToString())
clientChannel = CType(wcfClient2, IClientChannel)
clientChannel.Close()
myChannelFactory.Close()
End Sub
End Class
using System;
using System.ServiceModel;
// This code generated by svcutil.exe.
[ServiceContract()]
interface IMath
{
[OperationContract()]
double Add(double A, double B);
}
public class Math : IMath
{
public double Add(double A, double B)
{
return A + B;
}
}
public sealed class Test
{
static void Main()
{
// Code not shown.
}
public void Run()
{
// This code is written by an application developer.
// Create a channel factory.
BasicHttpBinding myBinding = new BasicHttpBinding();
EndpointAddress myEndpoint = new EndpointAddress("https://localhost/MathService/Ep1");
ChannelFactory<IMath> myChannelFactory = new ChannelFactory<IMath>(myBinding, myEndpoint);
// Create a channel.
IMath wcfClient1 = myChannelFactory.CreateChannel();
double s = wcfClient1.Add(3, 39);
Console.WriteLine(s.ToString());
((IClientChannel)wcfClient1).Close();
// Create another channel.
IMath wcfClient2 = myChannelFactory.CreateChannel();
s = wcfClient2.Add(15, 27);
Console.WriteLine(s.ToString());
((IClientChannel)wcfClient2).Close();
myChannelFactory.Close();
}
}