惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Heimdal Security Blog
SecWiki News
SecWiki News
H
Hacker News: Front Page
N
News | PayPal Newsroom
L
LINUX DO - 最新话题
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
AI
AI
C
Cybersecurity and Infrastructure Security Agency CISA
Scott Helme
Scott Helme
PCI Perspectives
PCI Perspectives
S
Securelist
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cyberwarzone
Cyberwarzone
A
Arctic Wolf
Forbes - Security
Forbes - Security
T
Tor Project blog
Spread Privacy
Spread Privacy
WordPress大学
WordPress大学
I
Intezer
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
博客园 - 司徒正美
W
WeLiveSecurity
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
P
Palo Alto Networks Blog
Google DeepMind News
Google DeepMind News
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
V
Vulnerabilities – Threatpost
Jina AI
Jina AI
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
Simon Willison's Weblog
Simon Willison's Weblog
Project Zero
Project Zero
T
Threatpost
P
Privacy International News Feed
人人都是产品经理
人人都是产品经理
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 蝈蝈

LAMPR Ver 1.0 发布 - 轻松搞定LAMP环境 在Windows Server下集成Apache、Tomcat和IIS Windows Workflow 学习笔记二 序语言流行度最新排行榜,C和C++正在衰落 Windows Workflow学习笔记 EAI,ESB与SOA How to Building ASP.NET AJAX Controls 使用页面Gzip压缩提速 Microsoft AJAX Library 简介 Configuration Section Designer示例 开源的内容管理系统 Windows Live Writer截屏插件 试用Windows Live Writer写博客 wcf step by step:消息交换模式 wcf step by step:如何保护你的wcf服务 wcf step by step之异常处理 wcf step by step:改进HelloWorld程序 wcf step by step:host服务与多次Endpoint wcf step by step之HelloWorld
wcf step by step:服务的状态
蝈蝈 · 2008-03-07 · via 博客园 - 蝈蝈

默认wcf服务的类是无状态的,就像web service一样,不能保存状态信息。即每次调用都是创建一个新的对象,所以一般我们都不会在wcf服务类中添加属性。
上面只是默认情况,因为wcf为我们提供了配置选项,我们只需显式指定ServiceBehaviorAttribute的InstanceContextMode的值即可实现有状态的服务,还可以实现全局的。ServiceBehavior是一个类特性,所以我们不能在接口上应用。InstanceContextMode是一个枚举类型,包含PerCallPerSessionSingle三个值。从字面上我们就可以看出他们的意思。注意服务的状态其实是与客户端代理类有相同的生命周期,也就是说我们可以通过调用客户端代理类的close方法来显示清除一个服务类的状态信息。另外,BasicHttpBinding的binding是没有状态的,所以我们不能用这个bind来让服务类保持状态。
PerCall
 每次调用都会创建新的对象,虽然我们没有显式创新,但代理对象会帮我们做这些。如
服务端
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class EmployeeService : IEmployeeService
    {
        private int count = 0;

        #region IEmployeeService Members

        public void ShowInvokeNumber()
        {
            count++;
            Console.WriteLine("count = {0}", count);
        }

        #endregion
    }

客户端
EmployeeServiceProxy proxy = new EmployeeServiceProxy("netEmployeeService");
    proxy.ShowInvokeNumber();
    proxy.ShowInvokeNumber();
    proxy.ShowInvokeNumber();
虽然我们只创建了一个代理对象,服务器端的对象会在每次调用完后进行释放,当再次调用时,会创建一个新的对象。

PerSession
简单的说它每个客户端共享一个对象,只在第一次调用时创建。

Single
是所以客户端共享一个对象。

通过我提供的演示demo,可以很清楚看到这其中的差别。注意服务器端的输出
percall输出的是
count = 1
count = 1
count = 1
persession输出的是
count = 1
count = 2
count = 3
如果我们多启动一个客户端则会有增加如下输出
count = 1
count = 2
count = 3
single输出的是
count = 1
count = 2
count = 3
增加一个客户端,输入变为
count = 4
count = 5
count = 6

还有一个

OperationBehavior应用于操作上的特性我们需要注意,它的ReleaseInstanceMode指定了4个枚举值。默认是None,还有AfterCall,BeforeAndAfterCall,BeforeCall,如果应用了这个特性,则对象在调用这个方法时会检查OperationBehavior特性的ReleaseInstanceMode的值,它如果它指定为非none,则在调用根据设置的值来在调用前或后释放该对象。

demo下载