方法 : ChannelFactory を使用するHow to: Use the ChannelFactory

ChannelFactory<TChannel> ジェネリック クラスは、複数チャネルの作成に使用できるチャネル ファクトリの作成を必要とする高度なシナリオで使用します。The generic ChannelFactory<TChannel> class is used in advanced scenarios that require the creation of a channel factory that can be used to create more than one channel.

ChannelFactory クラスの作成方法と使用方法To create and use the ChannelFactory class

  1. ビルドして、Windows Communication Foundation (WCF) サービスを実行します。Build and run an Windows Communication Foundation (WCF) service. 詳細については、次を参照してください。 Services の実装を設計およびサービスを構成する、およびホスティング サービスです。For more information, see Designing and Implementing Services, Configuring Services, and Hosting Services.

  2. 使用して、 ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe)をクライアントのコントラクト (インターフェイス) を生成します。Use the ServiceModel Metadata Utility Tool (Svcutil.exe) to generate the contract (interface) for the client.

  3. クライアント コード内で、ChannelFactory<TChannel> クラスを使用して複数のエンドポイント リスナーを作成します。In the client code, use the ChannelFactory<TChannel> class to create multiple endpoint listeners.

Example

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("http://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();
    }
}
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("http://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