WCF和IIS宿主的ASP.NET 共享会话

简介:

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= "http://files.cnblogs.com/cnblogsfans/WCFASPSession.rar" >Demo Sample source code</a>
本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2010/05/18/1738227.html如需转载请自行联系原作者

王德水
相关文章
|
12月前
|
开发框架 .NET Apache
301重定向代码合集(iis,asp,php,asp.net,apache)
301重定向代码合集(iis,asp,php,asp.net,apache)
278 0
|
开发框架 安全 .NET
『部署实操』使用 IIS 在 Windows 上托管 ASP.NET Core
使用 IIS 在 Windows 上托管 ASP.NET Core
634 0
『部署实操』使用 IIS 在 Windows 上托管 ASP.NET Core
|
.NET 开发框架
IIS&ASP.NET 站点IP跳转到域名
前言:先到微软的 https://www.iis.net/downloads/microsoft/url-rewrite  下载URL Rewrite 目标:输入ip跳转到域名所在的网站 比如58的115.
1672 0
|
网络安全 Windows
IIS多站点共享端口
如何支持多站点共用一个SSL端口
801 0
|
文件存储 SQL .NET
在 NAS SMB 卷上用 Visual Studio 调试 ASP.NET + SQLServer 项目并部署到 IIS 服务
阿里云 NAS SMB 卷具有高可用高吞吐大容量等特点,可以很好地解决网站和数据库项目的存储问题。本文将介绍如何在 NAS SMB 卷上用 Visual Studio 调试 ASP.NET + SQLServer 项目并把项目部署到 IIS 服务作为正式服务开放出去。
2448 0
在 NAS SMB 卷上用 Visual Studio 调试 ASP.NET + SQLServer 项目并部署到 IIS 服务
|
监控 .NET C++
1.4部署到IIS「深入浅出ASP.NET Core系列」
原文:1.4部署到IIS「深入浅出ASP.NET Core系列」 希望给你3-5分钟的碎片化学习,可能是坐地铁、等公交,积少成多,水滴石穿,谢谢关注。 很多人第一次在IIS中部署Asp.Net Core App的人都会遇到问题,会发现原来的部署方式无法运行Asp.Net Core App程序。
1377 0
|
监控 .NET C++
1.4部署到IIS「深入浅出ASP.NET Core系列」
希望给你3-5分钟的碎片化学习,可能是坐地铁、等公交,积少成多,水滴石穿,谢谢关注。 很多人第一次在IIS中部署Asp.Net Core App的人都会遇到问题,会发现原来的部署方式无法运行Asp.Net Core App程序。
2289 0
|
.NET
【ASP.NET Core快速入门】(二)部署到IIS
原文:【ASP.NET Core快速入门】(二)部署到IIS 配置IIS模块 ASP.NET Core Module载地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-module?ta...
1306 0
|
.NET 开发框架 Windows
在IIS上部署你的ASP.NET Core项目
原文:在IIS上部署你的ASP.NET Core项目 概述 与ASP.NET时代不同,ASP.NET Core不再是由IIS工作进程(w3wp.exe)托管,而是使用自托管Web服务器(Kestrel)运行,IIS则是作为反向代理的角色转发请求到Kestrel不同端口的ASP.NET Core程序中,随后就将接收到的请求推送至中间件管道中去,处理完你的请求和相关业务逻辑之后再将HTTP响应数据重新回写到IIS中,最终转达到不同的客户端(浏览器,APP,客户端等)。
3190 1