WCF和IIS宿主的ASP.NET 共享会话
2010-05-18 14:40 敏捷的水 阅读(1201) 评论(0) 编辑 收藏 举报1. 建立WCF工程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.Web; using System.ServiceModel.Activation; namespace WCFASPSession { [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service1 : IService1 { public int Add( int value) { int result = GetResult() + value; HttpContext.Current.Session[ "Result" ] = result; return result; } public int GetResult() { if (HttpContext.Current.Session[ "Result" ] == null ) { HttpContext.Current.Session[ "Result" ] = 0; } return ( int )HttpContext.Current.Session[ "Result" ]; } } } |
2. 在web工程里建一个.svc文件
1 | <%@ ServiceHost Language= "C#" Debug= "true" Service= "WCFASPSession.Service1" %> |
3. 修改web.config
主要是添加<serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment> ,还有allwoCookies一定要设为true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <system.serviceModel> <bindings> <wsHttpBinding> <binding name= "WSHttpBinding_IService1" closeTimeout= "00:01:00" openTimeout= "00:01:00" receiveTimeout= "00:10:00" sendTimeout= "00:01:00" bypassProxyOnLocal= "false" transactionFlow= "false" hostNameComparisonMode= "StrongWildcard" maxBufferPoolSize= "524288" maxReceivedMessageSize= "65536" messageEncoding= "Text" textEncoding= "utf-8" useDefaultWebProxy= "true" allowCookies= "true" > <readerQuotas maxDepth= "32" maxStringContentLength= "8192" maxArrayLength= "16384" maxBytesPerRead= "4096" maxNameTableCharCount= "16384" /> <reliableSession ordered= "true" inactivityTimeout= "00:10:00" enabled= "false" /> <security mode= "Message" > <transport clientCredentialType= "Windows" proxyCredentialType= "None" realm= "" /> <message clientCredentialType= "Windows" negotiateServiceCredential= "true" algorithmSuite= "Default" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address= "http://localhost:8000/Service1.svc" binding= "wsHttpBinding" bindingConfiguration= "WSHttpBinding_IService1" contract= "ServiceReference1.IService1" name= "WSHttpBinding_IService1" > <identity> <dns value= "localhost" /> </identity> </endpoint> </client> <serviceHostingEnvironment aspNetCompatibilityEnabled= "true" ></serviceHostingEnvironment> <services> <service name= "WCFASPSession.Service1" > <endpoint address= "" binding= "wsHttpBinding" contract= "WCFASPSession.IService1" > <!-- Upon deployment, the following identity element should be removed or replaced to reflect the identity under which the deployed service runs. If removed, WCF will infer an appropriate identity automatically. --> <identity> <dns value= "localhost" /> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> <!-- This endpoint does not use a secure binding and should be secured or removed before deployment --> <endpoint address= "mex" binding= "mexHttpBinding" contract= "IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled= "True" /> <!-- To receive exception details in faults for debugging purposes, set the value below to true . Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults= "False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <a href= "https://files.cnblogs.com/cnblogsfans/WCFASPSession.rar" >Demo Sample source code</a> |
扫码关注公众号,了解更多管理,见识,育儿等内容

作者: 王德水
出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。
出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
2009-05-18 [Tips]:Read value from Resource file in C#