1. Create a WCF Project
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. Create a. SVC file in the WEB Project
<% @ Servicehost Language = "C #" DEBUG = "true" service = "wcfaspsession. service1" %>
3. modify web. config
Add <servicehostingenvironment aspnetcompatibilityenabled = "true"> </servicehostingenvironment> and set allwocookies to true.
<System. servicemodel> <bindings> <wshttpbinding> <binding name = "canonical" closetimeout = "00:01:00" opentimeout = "00:01:00" receivetimeout = "00:10:00" sendtimeout = "00:01:00" bypassproxyonlocal = "false" transactionflow = "false" hostnamecomparisonmode = "strongwildcard" maxbufferpoolsize = "524288" maxcompute edmessagesize = "65536" messageencoding = "text" textencoding = "UTF-8" usedefawebwebproxy = "Tru E "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" negotiateser Vicecredential = "true" algorithmsuite = "default"/> </Security> </binding> </wshttpbinding> </bindings> <client> <endpoint address = "http: // localhost: 8000/service1.svc "binding =" wshttpbinding "bindingconfiguration =" canonical "Contract =" servicereference1.iservice1 "name =" canonical "> <identity> <DNS value =" localhost "/> </identity> </Endpoint> </client> <servicehostingenviro Nment aspnetcompatibilityenabled = "true"> </servicehostingenvironment> <services> <service name = "wcfaspsession. service1 "> <endpoint address =" "binding =" wshttpbinding "Contract =" wcfaspsession. iservice1 "> <! -- Upon deployment, the following identity element shoshould 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 shoshould 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> demo sample source code