博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
跟着Artech学习WCF(3) wcf 的状态问题
阅读量:4983 次
发布时间:2019-06-12

本文共 4370 字,大约阅读时间需要 14 分钟。

开始以为是wcf的session问题 敲了一边代码发现里面没有用session存储数据 经过 自己研究才发现作者是再将wcf的状态存储问题

项目结构如下

 

代码如下

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using Contract;namespace Client{    class Program    {        static void Main(string[] args)        {            ChannelFactory
calculatorChannelFactory = new ChannelFactory
("httpEndpoint"); Console.WriteLine("Create a calculator proxy: proxy1"); ICalculator proxy1 = calculatorChannelFactory.CreateChannel(); Console.WriteLine("Invocate proxy1.Adds(1)"); proxy1.Adds(1); Console.WriteLine("Invocate proxy1.Adds(2)"); proxy1.Adds(2); Console.WriteLine("The result return via proxy1.GetResult() is : {0}", proxy1.GetResult()); try { proxy1.Adds(1); } catch (Exception ex) { Console.WriteLine("It is fail to invocate the Add after terminating session because \"{0}\"", ex.Message); } //(proxy1 as ICommunicationObject).Close(); Console.WriteLine("Create a calculator proxy: proxy2"); ICalculator proxy2 = calculatorChannelFactory.CreateChannel(); Console.WriteLine("Invocate proxy2.Adds(1)"); proxy2.Adds(1); Console.WriteLine("Invocate proxy2.Adds(2)"); proxy2.Adds(2); Console.WriteLine("The result return via proxy2.GetResult() is : {0}", proxy2.GetResult()); //(proxy2 as ICommunicationObject).Close(); Console.Read(); } }}

=================================

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace Contract{        [ServiceContract(SessionMode=SessionMode.Required)]   public interface ICalculator    {        //调用任何允许初始化会话服务方法(缺省情况下所有的服务方法都自动初始化Session,也就是 IsInitiating=true)。        //调用任何包含 "IsTerminating=true" 声明的服务方法(缺省情况下所有的服务方法 IsTerminating=false,需要我们显示声明)。        // Terminating 终止意思  Initiating 启动的意思        [OperationContract(IsOneWay=true,IsInitiating=true,IsTerminating=false)]        void Adds(double x);        [OperationContract(IsInitiating = false, IsTerminating = true)]        double GetResult();    }}

========================================

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using Sevice;using System.ServiceModel;namespace Hosting2{    class Program    {        static void Main(string[] args)        {            using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))            {                host.Opened += delegate                {                    Console.WriteLine("The Calculator service has begun to listen");                };                host.Open();                Timer timer = new Timer(delegate { GC.Collect(); }, null, 0, 100);                Console.Read();            }        }    }}

===========

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Contract;using System.ServiceModel;namespace Sevice{        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]   public class CalculatorService:ICalculator    {       public CalculatorService()       {           Console.WriteLine("Calculator object has been created");       }       ~CalculatorService()       {           Console.WriteLine("Calculator object has been destoried");       }       private double _result;        void ICalculator.Adds(double x)        {            Console.WriteLine("The Add method is invoked and the current session ID is: {0}", OperationContext.Current.SessionId);            this._result += x;        }        double ICalculator.GetResult()        {            Console.WriteLine("The GetResult method is invoked and the current session ID is: {0}", OperationContext.Current.SessionId);            return this._result;        }    }}

 

 

客户端配置如下

服务端配置如下

转载于:https://www.cnblogs.com/qqloving/archive/2011/09/03/2165474.html

你可能感兴趣的文章
网页使用MD5加密
查看>>
JS 基础
查看>>
HBase shell 中的十六进制数值表示
查看>>
Python3 中 configparser 模块解析配置的用法详解
查看>>
新手android环境搭建、debug调试及各种插件安装__图文全解
查看>>
未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 win2008R2 X64 IIS7.5
查看>>
Diffuse贴图+Lightmap+Ambient
查看>>
矩阵树定理
查看>>
[算法]Evaluate Reverse Polish Notation
查看>>
go语言之进阶篇接口的定义和实现以及接口的继承
查看>>
SmartPhone手机网站的制作
查看>>
自适应全屏与居中算法
查看>>
构建之法阅读笔记(一)
查看>>
帮助你设计的50个自由和新鲜的图标集
查看>>
Glusterfs[转]
查看>>
javascript缩写
查看>>
GA来源分析
查看>>
常用统计指标
查看>>
iOS设置圆角矩形和阴影效果
查看>>
在博客园的第一篇文章,先简单自述一下吧
查看>>