RcMan
RcMan

Reputation: 913

WCF service is instansiating on each call

I have a WCF service on the client side and a wcf service on the server side. Each of the services has this line:

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]

I am using basicHttpBinding with no security.

I have methods on the client service:

private static ChannelFactory<IDatabaseProvider> _channel;
private static IDatabaseProvider _proxy;
private static DataTransferClient _client;

private bool IsChannelStateNeedToInit()
{
        return _proxy == null || ((IClientChannel)_proxy).State == System.ServiceModel.CommunicationState.Faulted || ((IClientChannel)_proxy).State == System.ServiceModel.CommunicationState.Closed;
}
public bool ConnectService()
{
        try
        {
            if (_channel == null || _proxy == null || IsChannelStateNeedToInit())
            {
                _channel = new ChannelFactory<IDatabaseProvider>("DatabaseProviderEndpointClient");
                _proxy = _channel.CreateChannel();
                _proxy.ConnectService();
            }
            return _proxy.IsDataBaseConnected(); 
        }
        catch (Exception ex)
        {
            Logger.Instance.LogFatal(ex);
            return false; 

        }
}

My client service is hosted by windows service, and my server service is hosted by IIS7. On each instantiation of the wcf service(of the server) I write to the event log, and I see that on each call my WCF service is instantiating even if IsChannelStateNeedToInit() return false.

On one of my testing environment I even get this exception on the server side.

Failed to InsertBulkToDatabase. EXCEPTION: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.

My Web config File:

<serviceBehaviors>
    <behavior name="DefaultServiceBehavior">
      <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="200" maxConcurrentInstances="200"/>
    </behavior>
  </serviceBehaviors>

My app config

<basicHttpBinding>
    <binding name="BasicHttpBindConfig" closeTimeout="00:10:00" openTimeout="00:10:00"
      receiveTimeout="00:10:00" sendTimeout="00:10:00" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxBufferSize="6553600" maxReceivedMessageSize="6553600"
      transferMode="Buffered">
      <security mode="None">
      </security>
    </binding>
  </basicHttpBinding>

How can I avoid this type of instantiating on each call ? Why does my service not instantiating only on different clients ?

Upvotes: 1

Views: 173

Answers (1)

Rui
Rui

Reputation: 4886

basicHttpBinding does not support sessions. That is the reason why you get a new instance per call. See: http://msdn.microsoft.com/en-us/library/ms731092(v=vs.110).aspx

You can avoid this hassle in the future by annotating your service contract with [ServiceContract(SessionMode = SessionMode.Required)] An exception will be thrown if you try to use the service contract in an endpoint with a binding that does not support sessions

Upvotes: 4

Related Questions