WCF揭秘—可靠性会话功能
WCF揭秘—可靠性会话功能
C# Code:
namespace Pro.Model { //建立数据契约 [DataContract] public class Person { [DataMember] public int ID { get; set; } [DataMember] public string Name { get; set; } [DataMember] public int Age { get; set; } } } namespace Pro.Service { // 建立服务契约接口“IPersonService”。 [ServiceContract] public interface IPersonService { [OperationContract] Person GetPerson(); } // 实现服务契约“PersonService”。 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class PersonService : IPersonService { public Person GetPerson() { Person person = new Person(); person.ID = 0; person.Name = "Leslie"; person.Age = 31; return person; } } class Program { //启动服务 static void Main(string[] args) { Console.WriteLine("Service star!"); ServiceHost host1 = new ServiceHost(typeof(PersonService)); host1.Open(); Console.ReadKey(); host1.Close(); } } }
<configuration>
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="false" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <netTcpBinding> <binding name="defaultNetTcpBinding"> <!--打开可靠性会话功能,把过期时间配置为10秒--> <reliableSession enabled="true" inactivityTimeout="00:00:10"/> </binding> </netTcpBinding> </bindings> <services> <service name="Pro.Service.PersonService"> <!--绑定defaultNetTcpBinding--> <endpoint address="" bindingConfiguration="defaultNetTcpBinding" binding="netTcpBinding" contract="Pro.Service.IPersonService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="net.Tcp://localhost:6000/Pro.Service/PersonService/" /> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/C#" runat="server"> protected void btn1_Click(object sender, EventArgs e) { //注意在调用完成后把服务销毁 using (PersonService.PersonServiceClient personService = new PersonService.PersonServiceClient()) { Pro.Model.Person person = personService.GetPerson(); Label1.Text = person.Name; } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server"></asp:Label> <asp:Button ID="btn1" runat="server" Text="Button" OnClick="btn1_Click"/> </div> </form> </body> </html>
<configuration> <system.web> <compilation debug="true" targetFramework="4.0"/> </system.web> <system.serviceModel> <bindings> <netTcpBinding> <binding name="NetTcpBinding_IPersonService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/> <!--启动可靠性会话,把过期时间设置为10秒--> <reliableSession inactivityTimeout="00:00:10" enabled="true"/> <security mode="Transport"> <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/> <message clientCredentialType="Windows"/> </security> </binding> </netTcpBinding> </bindings> <client> <endpoint address="net.tcp://localhost:6000/Pro.Service/PersonService/" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IPersonService" contract="PersonService.IPersonService" name="NetTcpBinding_IPersonService"> <identity> <dns value="localhost"/> </identity> </endpoint> </client> </system.serviceModel> </configuration>
值得注意的一点是,在某些不常用到的页面中(比如在Login登录页面),客户往往在长时间内只调用一次服务,所以应该注意对服务对象进行及时销毁,否则客户再次登录时就会出来错误。 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/C#" runat="server"> static PersonService.PersonServiceClient personService = new PersonService.PersonServiceClient(); //只在页面初次调用时,personService才会被新建,如果超过限时没有被再次调用,就会出现错误 protected void btn1_Click(object sender, EventArgs e) { Pro.Model.Person person = personService.GetPerson(); Label1.Text = person.Name; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server"></asp:Label> <asp:Button ID="btn1" runat="server" Text="Button" OnClick="btn1_Click"/> </div> </form> </body> </html> 来源:http://www.cnblogs.com/leslies2/archive/2011/08/08/2129422.html 本文来源:
参考文档:
WCF 可靠性会话(reliableSession)中的inactivityTimeout和receiveTimeout 测试报告 基础安全会话在可靠会话完全完成之前出错,可靠会话出错。 WCF服务保持在线状态(Keep Alive),长时间闲置状态下不断线解决方案 WCF服务错误:System.TimeoutException: 请求通道在等待 00:01:00 以后答复时超时 WCF服务错误:MessageSecurityException: 从另一方收到未进行安全处理或安全处理不正确的错误 框架提供关闭当前窗体,除此之外全部关闭窗体的功能(弹出菜单) WCF错误:服务器无法处理该请求IncludeExceptionDetailInFaults 连接WCF服务错误:调用方未由服务进行身份验证,无法满足对安全令牌的请求 有否支持客户自己用execl做模板实现打印报表的功能? winform界面布局设计和WCF的应用方面,你对这方面研究多吗? WCF:ServiceDebugBehavior.IncludeExceptionDetailInFaults 属性设置 最近框架新加了功能,各版本及文档同步未完成 开发框架新增单据自动生成功能 解决方案:WCF客户端无法获取服务端抛出的异常详细信息 增值功能:数据字典自动生成流水号解决方案
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |