Export (0) Print
Expand All

How to: Use the ChannelFactory

The generic ChannelFactory class is used in advanced scenarios that require the creation of a channel factory that can be used to create more than one channel.

To create and use the ChannelFactory class

  1. Build and run an Windows Communication Foundation (WCF) service. For more information, see Designing and Implementing Services, Configuring Windows Communication Foundation Services, and Hosting Windows Communication Foundation Services.

  2. Use the ServiceModel Metadata Utility Tool (Svcutil.exe) to generate the contract (interface) for the client.

  3. In the client code, use the ChannelFactory class to create multiple endpoint listeners.

Example

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())

        ' Create another channel
        Dim wcfClient2 As IMath = myChannelFactory.CreateChannel()
        s = wcfClient2.Add(15, 27)
        Console.WriteLine(s.ToString())
    End Sub
End Class


© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07

Community Additions

ADD
Show:
© 2014 Microsoft