�������أ�������ḻ�ȫ������վ��

��ҳ �� ��̿��� �� VC(VC++) �� WCFѧϰ��Instance context model(ʵ��ģ��) �� Session���Ự�� �Ĺ�ϵ

WCFѧϰ��Instance context model(ʵ��ģ��) �� Session���Ự�� �Ĺ�ϵ

��������������� ��Դ����վʱ�䣺2010/10/14 9:19:37�����С��A-A+

����

���ߣ��������������8226�����ۣ�1����ǩ��

        ��WCF�ж�Session��Ĭ��֧�ֵģ����Ǻ�ASP.NET�е�֧����ȫ��ͬ��˵��Session����ô�϶��з����(Service)�Ϳͻ���(Client)���ͻ���ͨ������(Proxy)�����ʷ���ˣ�����Session�����ں�Proxy�����ڰ󶨡��Էֲ�ʽ�ij�����ԣ�����ҵ���Ҫ�����ǻ�����������:
        ��һ������˲��ñ���ͻ��˵�״̬��ÿ�οͻ��˵ķ��ʶ��Ƕ����ģ�
        �ڶ����������Ҫ���ֿͻ��˵�״̬��ÿ�οͷ��˵�������õ�ͬһ��Session; 
        �������ڶ��û��й���ͬһ��ʵ��(��ʵ��������ASP.NET�е�Application������ȫ��һ��)��
       �������Ǿ��������������Ҫ�󣬷���һ����WCF�е�ʵ�ַ�ʽ. �Լ�����ʵ�ַ�ʽ�����ƺ�ȱ�㡣����������ʹ�ó�����WCF�Ѿ��ṩ�����õ�֧�֣������Instancing Management��������ʵ�ַ�ʽ�ǣ�Instance context model��������ô˵��Instance Context Mode�����Ų�ͬ��Session���֡�WCF�������� Instance Context Mode,���Ƿֱ���:
        >> Per-Call��ÿ�εĿͻ����������һ���µķ���ʵ���� ������Net Remoting��SingleCallģʽ, �����ַ�ʽ�£��������չ������ǿ�ģ�������������з��������Ƹ�Ϊ���ԡ���������Ƶ���ش���������ʵ��������������һ����Ӱ�졣��Ȼ���ϵ�ţ�ˣ��Ѿ����ǵ�ʹ���̳߳صķ�ʽ������Ƶ���ش���������ʵ��(�ο���ַ:http://www.cnblogs.com/artech/archive/2008/08/05/1260594.html)
        >> PerSession: �������Ҫ���ֿͻ��˵�״̬��Ϊÿ�οͻ������ӷ���һ������ʵ��,�ͻ��˵�ÿ�ε��ã���ʹ�õ�ͬһ��ʵ�������ʵ�����٣��ͻ��˵ĵ��û��׳��쳣��������Net Remoting�Ŀͻ��˼���ģʽ������wcf��Ĭ��֧�ַ�ʽ. ����ÿ���ͻ��˶���Ҫά��һ���Ự����Ҫռ�ý϶����Դ���������Ự״̬��������ڶ�������Ŀͻ��ˣ��򴴽�ר�ŵķ���ʵ���Ĵ���̫��
        >> Singleton: ���пͻ��˶��ԣ���ֻ��һ������ʵ��,������˱�host��ʱ�򣬾ͻᴴ�������ҽ���һ������ʵ������Ӧ�ͻ��˷�����õ������ڶ���ͻ��������£������ֻ�ᴦ��һ���ͻ��˵������������ŶӵȺ���������ϵͳ������������ӦЧ�ʡ�ϵͳ���������϶��������ص�ƿ��
�ô��ǣ����Թ�������.
      �ر��ǿ�����㣺
      ��һ:�������� Instance context model �����Dz��������е�Binding��֧�� Session ����Per-Call��Singleton���ԣ�bindingӰ�첻�󡣶�PerSession���ͱ���ʹ���ض���Binding, �ſ���,��������ָ�����PerCall�����±��      

Binding

Session mode

Context mode

Async Dispose()

Instance mode

Basic

Allowed/NotAllowed

PerCall/PerSession

Yes

PerCall

TCP, IPC

Allowed/Required

PerCall

No

PerCall

TCP, IPC

Allowed/Required

PerSession

Yes

PerSession

WS (no security, no reliability)

NotAllowed/Allowed

PerCall/PerSession

Yes

PerCall

WS (with security or reliability)

Allowed/Required

PerSession

Yes

PerSession

WS (with security or reliability)

NotAllowed

PerCall/PerSession

Yes

PerCall

 

      �ڶ�������PerSession, Ϊÿ�οͻ������ӷ���һ������ʵ���������ķ���������Դ�����Կ��԰�PerSession��PerCall���ʹ�á����ض���������������ȫ�����ڿͻ��˵�Message�а����ض����û�****��������PerCall��������ÿ������PerSession����Ȼ�������ض�������¡�
      �������������ִ�д��룺
      1������������Լ

    [ServiceContract(Namespace = "http://www.cnblogs.com/wanqiming/")]
    public interface IInstanceContextMode
    {
        //������Լ
        [OperationContract]
        void SayHello();
    }

 

      2��ʵ��PerCall��PerSession��Single�ķ�����

    #region PerCall

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class WCFServicePerCall : IInstanceContextMode, IDisposable
    {
        //����ʵ������
        private int mCcount = 0;
        //���캯��
        public WCFServicePerCall()
        {
            Console.WriteLine("WCFServicePerCall Instance is Created ");
        }
        //ʵ�ֽӿڶ���ķ���
        public void SayHello()
        {
            mCcount++;
            Console.WriteLine("WCFServicePerCall Instance Count is: {0} ", mCcount);
        }
        //ʵ�ֽӿڶ���ķ���Dispose
        public void Dispose()
        {
            Console.WriteLine("WCFServicePerCall Instance is disposed ");
        }
    }
    #endregion

 

    #region PerSession
    //3.������.�Ự����
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class WCFServicePerSession : IInstanceContextMode
    {
        //����ʵ������
        private int mCcount = 0;
        //���캯��
        public WCFServicePerSession()
        {
            Console.WriteLine("WCFServicePerSession Instance is Created ");
        }
        //ʵ�ֽӿڶ���ķ���
        public void SayHello()
        {
            mCcount++;
            Console.WriteLine("WCFServicePerSession Instance Count is: {0} ", mCcount);
        }
        //ʵ�ֽӿڶ���ķ���Dispose
        public void Dispose()
        {
            Console.WriteLine("WCFServicePerSession Instance is disposed ");
        }
    }
    #endregion

    #region Single
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class WCFServiceSingleTon : IInstanceContextMode
    {
        //����ʵ������
        private int mCcount = 0;
        //���캯��
        public WCFServiceSingleTon()
        {
            Console.WriteLine("WCFServiceSingleTon Instance is Created ");
        }
        //ʵ�ֽӿڶ���ķ���
        public void SayHello()
        {
            mCcount++;
            Console.WriteLine("WCFServiceSingleTon Instance Count is: {0} ", mCcount);
        }
        //ʵ�ֽӿڶ���ķ���Dispose
        public void Dispose()
        {
            Console.WriteLine("WCFServiceSingleTon Instance is disposed ");
        }
    }
    #endregion

       ����������˵������ļ�

      <behaviors>
        <serviceBehaviors>
          <behavior name="DefaultBehavior">
            <serviceMetadata httpGetEnabled="true" />
            <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100" maxConcurrentInstances="100" />
            <serviceDebug includeExceptionDetailInFaults="true" />
          </behavior>
        </serviceBehaviors>
      </behaviors>

 

      <services>
          
        <service  name="Fish.ServiceImpl.WCFServicePerCall">
          <endpoint address="" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <endpoint address="" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:8080/Fish/WCFServicePerCall/>
              <add baseAddress="net.tcp://localhost:808/Fish/WCFServicePerCall"/>
            </baseAddresses>
          </host>
        </service>

        <service name="Fish.ServiceImpl.WCFServicePerSession">
          <endpoint address="" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <endpoint address="" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <host>
            <baseAddresses>
              <add baseAddress="net.tcp://localhost:808/Fish/WCFServicePerSession"/>
              <add baseAddress="http://localhost:8080/Fish/WCFServicePerSession/>
            </baseAddresses>
          </host>
        </service>
       
        <service behaviorConfiguration="DefaultBehavior" name="Fish.ServiceImpl.WCFServiceSingleTon">
          <endpoint address="" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <endpoint address="" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <host>
            <baseAddresses>
              <add baseAddress="net.tcp://localhost:808/Fish/WCFServiceSingleTon"/>
              <add baseAddress="http://localhost:8080/Fish/WCFServiceSingleTon/>
            </baseAddresses>
          </host>
        </service>
          

       ���ģ�Host

 

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                List<ServiceHost> hosts = new List<ServiceHost>();
                hosts.Add(new ServiceHost(typeof(WCFServicePerCall)));          //PerCall
                hosts.Add(new ServiceHost(typeof(WCFServicePerSession)));       //PerSession
                hosts.Add(new ServiceHost(typeof(WCFServiceSingleTon)));        //SingleTon
                
                hosts.ForEach(host => host.Open());

                Console.WriteLine("All services are started...");
                Console.Read();
            }
            catch(Exception e)
            {
                Console.WriteLine("������Ϣ��:"+e.ToString());
            }
        }
    }

 

      ���壺�ͻ���ִ�д���

class Program
    {
        static void Main(string[] args)
        {
            PerCall();

            PerSession();

            Single();

            Console.WriteLine("  ");

        }

        #region PerCall
        private static void PerCall()
        {
            var perCall = new ChannelFactory<IInstanceContextMode>("tcpPerCall").CreateChannel();
            perCall.SayHello();
            perCall.SayHello();
            perCall.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(perCall);
            
            var tcpPerCall = new ChannelFactory<IInstanceContextMode>("tcpPerCall").CreateChannel();
            tcpPerCall.SayHello();
            tcpPerCall.SayHello();
            tcpPerCall.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(tcpPerCall);

 

        }
        #endregion

        #region PerSession
        private static void PerSession()
        {
            var perSession = new ChannelFactory<IInstanceContextMode>("basicHttpPerSession").CreateChannel();
            perSession.SayHello();
            perSession.SayHello();
            perSession.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(perSession);

            var tcpPerSession = new ChannelFactory<IInstanceContextMode>("tcpPerSession").CreateChannel();
            tcpPerSession.SayHello();
            tcpPerSession.SayHello();
            tcpPerSession.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(tcpPerSession);


        }
        #endregion

        #region Single
        private static void Single()
        {
            var perSession = new ChannelFactory<IInstanceContextMode>("basicHttpSingleTon").CreateChannel();
            perSession.SayHello();
            perSession.SayHello();
            perSession.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(perSession);

            var tcpPerSession = new ChannelFactory<IInstanceContextMode>("tcpSingleTon").CreateChannel();
            tcpPerSession.SayHello();
            tcpPerSession.SayHello();
            tcpPerSession.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(tcpPerSession);


        }
        #endregion
    }



      �������ͻ��������ļ�
    <client>
      <endpoint name="httpPerCall" address="http://localhost:8080/Fish/WCFServicePerCall" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="tcpPerCall" address="net.tcp://localhost:808/Fish/WCFServicePerCall" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="basicHttpPerSession" address="http://localhost:8080/Fish/WCFServicePerSession" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="tcpPerSession" address="net.tcp://localhost:808/Fish/WCFServicePerSession" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="basicHttpSingleTon" address="http://localhost:8080/Fish/WCFServiceSingleTon" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="tcpSingleTon" address="net.tcp://localhost:808/Fish/WCFServiceSingleTon" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
    </client>
      ִ�еĽ�����£�

�������

�Ķ����ĺ�����ʲô����? ���� �˸�������!

  • 2791 ϲ��ϲ��
  • 2101 ����
  • 800 �ѹ��ѹ�
  • 1219 ����
  • 4049 Χ��Χ��
  • 5602 ��������
��������
��������
�������� �鿴��������(1)
�dz�:
����: ���� �� �� �Ҳ�Ҫ ���� �� ������ �ͻ� ʺ ����
����: 0/500 (����������Ҫ������˲�����ʾ)

���ೣ�����