yunvn 发表于 2015-8-14 10:09:34

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

  1. 建立WCF工程
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文件

<%@ ServiceHost Language=&quot;C#&quot; Debug=&quot;true&quot; Service=&quot;WCFASPSession.Service1&quot;%>
  3. 修改web.config
  主要是添加<serviceHostingEnvironment aspNetCompatibilityEnabled=&quot;true&quot;></serviceHostingEnvironment> ,还有allwoCookies一定要设为true.

<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name=&quot;WSHttpBinding_IService1&quot;closeTimeout=&quot;00:01:00&quot;
openTimeout=&quot;00:01:00&quot; receiveTimeout=&quot;00:10:00&quot; sendTimeout=&quot;00:01:00&quot;
bypassProxyOnLocal=&quot;false&quot; transactionFlow=&quot;false&quot; hostNameComparisonMode=&quot;StrongWildcard&quot;
maxBufferPoolSize=&quot;524288&quot; maxReceivedMessageSize=&quot;65536&quot; messageEncoding=&quot;Text&quot;
textEncoding=&quot;utf-8&quot; useDefaultWebProxy=&quot;true&quot; allowCookies=&quot;true&quot;>
<readerQuotas maxDepth=&quot;32&quot; maxStringContentLength=&quot;8192&quot; maxArrayLength=&quot;16384&quot;
maxBytesPerRead=&quot;4096&quot; maxNameTableCharCount=&quot;16384&quot; />
<reliableSession ordered=&quot;true&quot; inactivityTimeout=&quot;00:10:00&quot;
enabled=&quot;false&quot; />
<security mode=&quot;Message&quot;>
<transport clientCredentialType=&quot;Windows&quot; proxyCredentialType=&quot;None&quot;
realm=&quot;&quot; />
<message clientCredentialType=&quot;Windows&quot; negotiateServiceCredential=&quot;true&quot;
algorithmSuite=&quot;Default&quot; />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address=&quot;http://localhost:8000/Service1.svc&quot; binding=&quot;wsHttpBinding&quot;
bindingConfiguration=&quot;WSHttpBinding_IService1&quot; contract=&quot;ServiceReference1.IService1&quot;
name=&quot;WSHttpBinding_IService1&quot;>
<identity>
<dns value=&quot;localhost&quot; />
</identity>
</endpoint>
</client>
<serviceHostingEnvironment aspNetCompatibilityEnabled=&quot;true&quot;></serviceHostingEnvironment>
<services>
<service name=&quot;WCFASPSession.Service1&quot;>
<endpoint address=&quot;&quot; binding=&quot;wsHttpBinding&quot; contract=&quot;WCFASPSession.IService1&quot;>
<!--
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=&quot;localhost&quot;/>
</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=&quot;mex&quot; binding=&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot;/>
</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=&quot;True&quot;/>
<!-- 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=&quot;False&quot;/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Demo Sample source code
页: [1]
查看完整版本: WCF和IIS宿主的ASP.NET 共享会话