Project Structure:
Rhythmk. Contracts // Contract
Rhythmk. Services // Service
Rhythmk. wcfsvc // service host
Rhythmk. Test // Test
-----------------------------------------------------------------
Note: Object consistency can be ensured through session, and object consistency can be ensured through binding.Binding= "Wshttpbinding"Implement session Status
1. rhythmk. Contracts // Contract
Using System. servicemodel;
/// Sessionmode-gets or sets whether a session is allowed, not allowed, or required
/// Sessionmode. Allowed-specify that the Protocol also supports sessions when session binding is passed in (default)
/// Sessionmode. required-specify that the Protocol requires session binding. If the binding is not configured to support sessions, an exception is thrown.
/// Sessionmode. notallowed-specify that the Protocol never supports session binding.
[Servicecontract (sessionmode = Sessionmode. required, namespace = " Http://wwww.wangkun.com " )]
Public Interface Icalculate
{
/// Isinitiating-gets or sets a value that indicates whether the method can start a session (if any) on the server.
/// Isterminating-gets or sets a value that indicates whether the server closes the session after a service operation sends a reply message (if any.
[Operationcontract (isinitiating = True , Isterminating = False )]
String Getsessionid ();
[Operationcontract (isinitiating = True , Isterminating = False )]
Void Initnum ();
[Operationcontract (isinitiating = True , Isterminating = False )]
Void Addnum ( Int X );
[Operationcontract (isinitiating = True , Isterminating = False )]
Int Getnum ();
}
2. rhythmk. Services // Service
Using Rhythmk. contracts;
Using System. servicemodel. activation;
Using System. servicemodel;
/// <Summary>
/// Calculator
/// </Summary>
/// /// <Summary>
/// Interface to demonstrate session Status
/// </Summary>
/// <Remarks>
/// Instancecontextmode-gets or sets the value indicating when a new service object is created.
/// Instancecontextmode. persession-creates a new system. servicemodel. instancecontext object for each session.
/// The default value of instancecontextmode is instancecontextmode. persession.
/// </Remarks>
[Servicebehavior (instancecontextmode = Instancecontextmode. persession)]
[Aspnetcompatibilityrequirements (requirementsmode = Aspnetcompatibilityrequirementsmode. Required)]
Public Class Calculate: icalculate
{
# Region Icalculate Member
Private Int _ Num;
Public String Getsessionid ()
{
Return Operationcontext. Current. sessionid;
}
Public Void Initnum ()
{
_ Num = 0 ;
}
Public Void Addnum ( Int X)
{
_ Num = _ Num + X;
}
Public Int Getnum ()
{
Return _ Num;
}
# Endregion
}
3. rhythmk. wcfsvc // service host
<%@ Servicehost Language="C #"Service="Rhythmk. Services. Calculate" %>
Configuration:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< System. servicemodel >
< Behaviors >
< Servicebehaviors >
< Behavior Name = "Metabehavior" >
< Servicemetadata Httpgetenabled = "True" />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >
< Services >
< Service Name = "Rhythmk. Services. Calculate" Behaviorconfiguration = "Metabehavior" >
< Endpoint Address = ""
Binding = "Wshttpbinding" Bindingconfiguration = "" Contract = "Rhythmk. Contracts. icalculate" >
</ Endpoint >
< Host >
< Baseaddresses >
< Add Baseaddress = "Http: // localhost: 3654/One/wscalculate. SVC" />
</ Baseaddresses >
</ Host >
</ Service >
</ Services >
</ System. servicemodel >
</ Configuration >
4. rhythmk. Test // Test
Using system. servicemodel;
Namespace rhythmk. Test
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
If (session ["proxy"] = NULL)
{
Session ["proxy"] = new myservice. calculateclient ();
}
Myservice. icalculate proxy = session ["proxy"] As myservice. calculateclient;
Proxy. initnum ();
}
}
Protected void btnset_click (Object sender, eventargs E)
{
Myservice. icalculate proxy = session ["proxy"] As myservice. calculateclient;
Proxy. addnum (10 );
}
Protected void btnget_click (Object sender, eventargs E)
{
Myservice. icalculate proxy = session ["proxy"] As myservice. calculateclient;
Response. Write (proxy. getsessionid ());
Response. Write (proxy. getnum (). tostring ());
}
}
}