Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
WCF Feature Details
Clients
 How to: Use the ChannelFactory
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
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 Services, and Hosting 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

Visual Basic
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
C#
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());

        // Create another channel.
        IMath wcfClient2 = myChannelFactory.CreateChannel();
        s = wcfClient2.Add(15, 27);
        Console.WriteLine(s.ToString());
    }
}


© 2007 Microsoft Corporation. All rights reserved.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Worst Sample Ever      PaulWh   |   Edit   |  

MSFT, how do you expect people to follow you best practices, be productive on your platform, and create a great experience for customers if you publish horrible samples like this one. If a developer were to copy this code into a real application they would have just leaked two client connections, and it would take their user about ten minutes to hit the defaul connection limit. The code should look like this:

  

staticvoid Main(string[] args)

{

// Code not shown.

}

publicvoid Run()

{

// This code is written by an application developer.

// Create a channel factory.

BasicHttpBinding myBinding = newBasicHttpBinding();

EndpointAddress myEndpoint =

newEndpointAddress(http://localhost/MathService/Ep1);

// local variables that implement IDisposible should always be put in a using block

using (ChannelFactory<IMath> myChannelFactory = newChannelFactory<IMath>(myBinding, myEndpoint))

{

double s;

// Create a channel.

IMath wcfClient1 = myChannelFactory.CreateChannel();

try

{

s = wcfClient1.Add(3, 39);

Console.WriteLine(s.ToString());

}

finally

{

// Make sure we close the channel when we're done.

try

{

((

IClientChannel)wcfClient1).Close();

}

catch (CommunicationException)

{

// Ignore various exceptions regarding the Channel's current state

}

catch (TimeoutException)

{

// Ignore Timeouts

}

}

// Create another channel.

IMath wcfClient2 = myChannelFactory.CreateChannel();

try

{

s = wcfClient2.Add(15, 27);

Console.WriteLine(s.ToString());

}

finally

{

try

{

((

IClientChannel)wcfClient1).Close();

}

catch (CommunicationException)

{

}

catch (TimeoutException)

{

}

}

}

}

Re: Worst Sample Ever      Nicholas Allen   |   Edit   |  

Hi Paul,

Thanks for commenting on this sample. I have asked the owner to consider how best to fix the code. I think that for this demonstration it would be appropriate to make sure that the ChannelFactory was closed at the end of the sample- that would clean up the proxies in a graceful manner without much distraction. A more detailed consideration of error handling from the other samples could be added as a link.

- Nicholas

Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker