【IT168技术文档】
我们先采用默认的Session和Instance Context Modle,在这之前我们看看整个Solution各个部分的定义:
Service Contract:ICalculator
Service Implementation:CalculatorServiceusing System; using System.Collections.Generic; using System.Text; using System.ServiceModel; namespace Artech.SessionfulCalculator.Contract { [ServiceContract] public interface ICalculator { [OperationContract(IsOneWay = true)] void Adds(double x); [OperationContract] double GetResult(); } }
using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; using Artech.SessionfulCalculator.Contract; namespace Artech.SessionfulCalculator.Service { public class CalculatorService:ICalculator { private double _result; ICalculator Members#region ICalculator Members public void Adds(double x) { Console.WriteLine("The Add method is invoked and the current session ID is: {0}", OperationContext.Current.SessionId); this._result += x; } public double GetResult() { Console.WriteLine("The GetResult method is invoked and the current session ID is: {0}", OperationContext.Current.SessionId); return this._result; } #endregion public CalculatorService() { Console.WriteLine("Calculator object has been created"); } ~CalculatorService() { Console.WriteLine("Calculator object has been destoried"); } } }