频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

WCF体验之旅(8):WCF中的Session和Instancing Management

来源:博客园 作者:Artech 出处:巧巧读书 2008-03-18 进入讨论组
上一页 1 2 3 4 5 6 7 下一页 

我们先采用默认的Session和Instance Context Modle,在这之前我们看看整个Solution各个部分的定义:

1、Service Contract:ICalculator

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Artech.SessionfulCalculator.Contract
{
[ServiceContract]
public interface ICalculator
{
[OperationContract(IsOneWay = true)]
void  Adds(double x);

[OperationContract]
double GetResult();
}
}


2、Service Implementation:CalculatorService

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Artech.SessionfulCalculator.Contract;

namespace Artech.SessionfulCalculator.Service
{
public class CalculatorService:ICalculator
{

private double _result;

ICalculator Members#region ICalculator Members

public void Adds(double x)
{
Console.WriteLine("The Add method is invoked and the current session ID is: {0}",

OperationContext.Current.SessionId);
this._result += x;
}

public double GetResult()
{
Console.WriteLine("The GetResult method is invoked and the current session ID is:

{0}", OperationContext.Current.SessionId);
return this._result;
}

#endregion

public CalculatorService()
{
Console.WriteLine("Calculator object has been created");
}

~CalculatorService()
{
Console.WriteLine("Calculator object has been destoried");
}

}
}

为了让大家对Service Instance的创建和回收有一个很直观的认识,我特意在Contructor和Finalizer中作了一些指示性的输出。同时在每个Operation中输出的当前的Session ID

3、Hosting

Program

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Artech.SessionfulCalculator.Service;
using System.Threading;

namespace Artech.SessionfulCalculator.Hosting
{
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();
}
}
}
}


除了Host CalculatorService之外,我还通过一个Timer对象每隔一个很短的时间(0.1s)作一次强制的垃圾回收,使我们通过输出看出Service Instance是否被回收了。

收藏 http://www.qqread.com/other-devtool/j401812.html进入讨论组讨论。
上一页 1 2 3 4 5 6 7 下一页 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章