locked
Caching the channel factory on a web site? RRS feed

  • Question

  • Hello everyone. We have been having some performance problems with some of our sites lately. We are using WCF (Mutual Authentication with Certificates) to return the data for our site. After our we were having the performance problems our network admin did some sniffing and he found out that there were a LOT of key exchange / security negation request going on. The system was designed to try to avoid this but mabye we are not doing it correctly. We are trying to cache the channel factory in the application object when we start the web site and the despose it when the site goes does so the site should use the channel factory for the entire site.

    So in the global.asax

    Applicaiton_Start:
    Dim loChannelFactory as New ChannelFactory(Of MyWCFReference.IWCFChannel)("WSHttpBinding_IWCF")
    loChannelFactory.Open()
    Application.Add("Channel",loChannelFactory)

    Application_End:
    If Application("Channel")  IsNot Nothing then
          DirectCast(Application("Channel"),ChannelFactory(Of MyWCFReference.IWCFChannel)).Close()
    End If

    We try to limit out calls on every page to 1 call to WCF but some pages might have a few more.


    Friend Function GetWCFProxy()
        Return DirectCast(voWebApp("Channel"), ChannelFactory(Of                                            MyWCFReference.IWCFChannel)).CreateChannel
    End Function


    This is how we make the calls on each one of our pages:
    Dim loWCFService as MyWCFReference.IWCFService = GetWCFProxy()

    loWCFService.MyFunction()

    if loWCFService IsNotNothing Then
        CType(loWCFService,ICommunicationObject).Close()
    End if

    So we are trying to close the proxy but keep the channel alive so we don't have to have an expensive key exchange every call we make. Is there something about how we are caching the channel factory that is making the key exchange happen every time?

    thanks,
    Choclabs
    Thursday, May 14, 2009 2:17 PM

Answers

  • Hi

    well I have just rechecked this:

    I wrote this code on the client

    for (int i = 0; i < 10; i++)
    {
        ChannelFactory<IPing> fact = new ChannelFactory<IPing>("WSHttpBinding_IPing");
    
        fact.Open();
        IPing p = fact.CreateChannel();
        p.Ping();
    }
    then I wrote this code

    ChannelFactory<IPing> fact = new ChannelFactory<IPing>("WSHttpBinding_IPing");
    
    fact.Open();
    
    for (int i = 0; i < 10; i++)
    {
        IPing p = fact.CreateChannel();
        p.Ping();
    }
    

     with message tracing turned on I got the same trace with an RST/Issue, RSTR/Issue and RST/SCT for every Ping request sent in both cases

    Then I wrote this code:

    ChannelFactory<IPing> fact = new ChannelFactory<IPing>("WSHttpBinding_IPing");
    
    fact.Open();
    
    IPing p = fact.CreateChannel();
    
    for (int i = 0; i < 10; i++)
    {
        p.Ping();
    }
    And the negotiation happens only on the first ping request - all others use the existing SCT

    So I was going by empirical evidence - but maybe they have a different set up to my test rig - this was using teh default setting on WSHttpBinding





    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    • Proposed as answer by Marco Zhou Friday, May 15, 2009 5:15 AM
    • Marked as answer by Marco Zhou Wednesday, May 27, 2009 11:00 AM
    Thursday, May 14, 2009 4:14 PM

All replies

  • Its the first call *on the proxy* that does the key exchange. Every proxy sets up its on secure conversation with the service (which is the key exchange). You'll need to cache the proxy not the factory
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    Thursday, May 14, 2009 2:21 PM
  • Its the first call *on the proxy* that does the key exchange. Every proxy sets up its on secure conversation with the service (which is the key exchange). You'll need to cache the proxy not the factory
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett

    Are you 100% sure on that? I actually got this solution from Michele Leroux Bustamante. She had told me that creating the channel sets up the key exchange and on a website you would want to cache the channel factory for the entire site and not cache the proxy.
    According to this blog...creating the channel stack happens when you create the channel and not creating the proxy:
    http://blogs.msdn.com/wenlong/archive/2007/10/27/performance-improvement-of-wcf-client-proxy-creation-and-best-practices.aspx


    Thursday, May 14, 2009 3:24 PM
  • Hi

    well I have just rechecked this:

    I wrote this code on the client

    for (int i = 0; i < 10; i++)
    {
        ChannelFactory<IPing> fact = new ChannelFactory<IPing>("WSHttpBinding_IPing");
    
        fact.Open();
        IPing p = fact.CreateChannel();
        p.Ping();
    }
    then I wrote this code

    ChannelFactory<IPing> fact = new ChannelFactory<IPing>("WSHttpBinding_IPing");
    
    fact.Open();
    
    for (int i = 0; i < 10; i++)
    {
        IPing p = fact.CreateChannel();
        p.Ping();
    }
    

     with message tracing turned on I got the same trace with an RST/Issue, RSTR/Issue and RST/SCT for every Ping request sent in both cases

    Then I wrote this code:

    ChannelFactory<IPing> fact = new ChannelFactory<IPing>("WSHttpBinding_IPing");
    
    fact.Open();
    
    IPing p = fact.CreateChannel();
    
    for (int i = 0; i < 10; i++)
    {
        p.Ping();
    }
    And the negotiation happens only on the first ping request - all others use the existing SCT

    So I was going by empirical evidence - but maybe they have a different set up to my test rig - this was using teh default setting on WSHttpBinding





    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    • Proposed as answer by Marco Zhou Friday, May 15, 2009 5:15 AM
    • Marked as answer by Marco Zhou Wednesday, May 27, 2009 11:00 AM
    Thursday, May 14, 2009 4:14 PM