zoukankan      html  css  js  c++  java
  • WCF系列教程之WCF实例化

    本文参考自http://www.cnblogs.com/wangweimutou/p/4517951.html,纯属读书笔记,加深记忆

    一、理解WCF实例化机制

    1、WCF实例化,是指对用户定义的服务对象以及与其相关的实例上下文对象的生存期的控制,也就是说每一个服务类其实就是一个服务实例,客户端在调用服务端中的服务方法时,需要实例化一个服务端代理类对象,实例化就是对这个对象的生命周期的管理。

    2、实例化行为(使用 System.ServiceModel.ServiceBehaviorAttribute.InstanceContextMode 属性进行设置),InstanceContextMode属性有两个作用:

    (1)、控制如何创建 InstanceContext 以响应传入的消息

    (2)、每个 InstanceContext 都与一个用户定义服务对象相关联,因此设置 InstanceContextMode 属性也可以控制用户定义服务对象的实例化

    默认情况下,InstanceContextMode 枚举定义了实例化模式。可以使用下列实例化模式:

    (1)、PerCall:单调模式,为每个客户端请求创建一个新的 InstanceContext(以及相应的服务对象)。

    (2)、PerSession:会话模式,这是InstanceContextMode的默认值,为每个新的客户端会话创建一个新的 InstanceContext(以及相应的服务对象),并在该会话的生存期内对其进行维护(这需要使用支持会话的绑定).

    (3)、Single:单例模式,单个 InstanceContext(以及相应的服务对象)处理应用程序生存期内的所有客户端请求。

    二、实例化模式PerCall、PerSession、Single详解

    1、单调模式

    单调模式中,WCF总是创建一个新的服务实例上下文来处理请求对象,即客户端每调用一次方法就会创建一个实例上下文对象,调用完成靠GC完成释放对象(但是GC的回收具有不确定性,所以会有延迟),在调用下一个方法会创建下一个实例上下文,因此,一个会话通道可能会出现多个实例上下文对象。

    代码实例:

    1、WCF服务层搭建:新建契约层、服务层、和WCF宿主,添加必须的引用(这里不会的参考本人前面的随笔),配置宿主,生成解决方案,打开Host.exe,开启服务。具体的代码如下:

    IPerCall.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace IService
    {
        [ServiceContract
        (
        SessionMode=SessionMode.Required
        )
        ]
        public interface IPerCall
        {
            [OperationContract]
            int AddMethod(int a, int b);
            [OperationContract]
            int AddMethod1(int a, int b);
            [OperationContract]
            string GetInstanceId();//获取实例Id
            [OperationContract]
            int GetInstanceCount();    //获取实例数
            [OperationContract]
            int GetOperationCount();    //获取调用操作方法的计数器
            
        }
    }

    PerCall.cs

    using IService;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Service
    {
        [ServiceBehavior
        (
        InstanceContextMode=InstanceContextMode.PerCall//设置当前服务类的实例化模式为单调模式,每调用一次方法,生成一个上下文实例
        )]
        public class PerCall:IPerCall
        {
            int operationCount;
            static int instanceCount;
            string instanceId;
            public PerCall() {
                instanceCount++;
                instanceId = Guid.NewGuid().ToString();
            }
            public string GetInstanceId()
            {
                return instanceId;
            }
    
            public int GetOperationCount()
            {
                return operationCount;
            }
    
            public int AddMethod(int a, int b)
            {
                operationCount++;
                return a + b;
            }
    
            public int AddMethod1(int a, int b)
            {
                operationCount++;;
                return a + b;
            }
    
    
            public int GetInstanceCount()
            {
                return instanceCount;
            }
        }
    }

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Host
    {
        
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(Service.PerCall))) 
                {
                    host.Open();
                    Console.WriteLine("服务已启动,按任意键中止...");
                    Console.ReadKey(true);
                    host.Close();
                }
            }
        }
    }

    App.Config

    <?xml version="1.0"?>
    <configuration>
      <system.serviceModel>
    
        <services>
          <service name="Service.PerCall" behaviorConfiguration="mexBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:1234/PerCall/"/>
              </baseAddresses>
            </host>
            <endpoint address="" binding="wsHttpBinding" contract="IService.IPerCall" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
    
        <behaviors>
          <serviceBehaviors>
            <behavior name="mexBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>

    ok,打开Host.exe

    服务开启成功!

    2、新建一个名为Client的客户端控制台应用程序,添加对http://localhost:1234/PerCall/的引用

    (1)、单调模式

    program.cs代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                Client.PerCallClientNS.PerCallClient client = new PerCallClientNS.PerCallClient();
                Console.WriteLine("1+2={0},这是第{1}个实例上下文对象,实例ID是:{2},方法的操作数:{3}", client.AddMethod(1, 2),client.GetInstanceCount(),client.GetInstanceId(),client.GetOperationCount());
                Console.WriteLine("3+3={0},这是第{1}个实例上下文对象,实例ID是:{2},方法的操作数:{3}", client.AddMethod1(3, 3), client.GetInstanceCount(), client.GetInstanceId(), client.GetOperationCount());
                Console.ReadKey();
            }
        }
    }

    通过结果分析,客户端通过会话通过每调用一次服务端的方法,就会创建一个服务端上下文实例,通过这个变量就可以看出

    所以除非当前通道关闭,要不然只要调用一次当前服务端的方法,那么实例树还会继续上升。

    (2)、单例模式(Single)

     在单例模式中,WCF只会创建一个实例上下文来处理服务的所有的请求调用对象,即PerCall只会被实例化一次,不管调用的请求对象是在同一个客户端还是在不同的客户端.

    代码实例,在上面代码的基础上将PerCall类的ServiceBehavior属性

     [ServiceBehavior
        (
        InstanceContextMode=InstanceContextMode.PerCall//设置当前服务类的实例化模式为单调模式,每调用一次方法,生成一个上下文实例
        )]

    修改成

    [ServiceBehavior
        (
        InstanceContextMode=InstanceContextMode.Single//设置当前服务类的实例化模式为单例模式,只生成一个上下文实例,所以客户端共享这一个实例
        )]

    重新部署工程

    不管执行多少次,当前总是第一个实例,说明当前上下文只生成了一个,应为没有执行构造函数,所以只能是1,但是操作数的增加,说明了,客户端共享了这一个实例,只要当前通道不关闭,那么操作数会一直增加。

    (3)、会话模式(PerSession)

    在会话模式(PerSession)中,WCF会对客户端与服务端的每一个会话通道创建一个实例上下文。即不同的客户端在各自的会话通道的实例上下文中处理请求对象。

    在上面代码的基础上将PerCall类的ServiceBehavior属性的InstanceContextMode,修改成

    [ServiceBehavior
        (
        InstanceContextMode=InstanceContextMode.PerSession//设置当前服务类的实例化模式为会话模式,WCF会为每一个会话创建一个上下文实例
        )]

    重新部署工程

    每调用一次客户端,生成一个上下文实例,每次都是新的实例

  • 相关阅读:
    记一次诡异的调优
    java动态代理学习笔记
    c#反射机制学习和利用反射获取类型信息
    php开启ssl的方法
    关于java中split的使用
    c#使用反射调用类型成员示例
    C#关于反射加载的问题
    Twitter:使用Netty 4来减少GC开销
    Java中如何修改Jar中的内容
    Android中自定义视图View之---前奏篇
  • 原文地址:https://www.cnblogs.com/GreenLeaves/p/6876287.html
Copyright © 2011-2022 走看看