zoukankan      html  css  js  c++  java
  • 跟着Artech学习WCF(3) wcf 的状态问题

    开始以为是wcf的session问题 敲了一边代码发现里面没有用session存储数据 经过 自己研究才发现作者是再将wcf的状态存储问题

    项目结构如下

    dddddd

    代码如下

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using Contract;
    
    namespace Client
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                ChannelFactory<ICalculator> calculatorChannelFactory = new ChannelFactory<ICalculator>("httpEndpoint");
                Console.WriteLine("Create a calculator proxy: proxy1");
                ICalculator proxy1 = calculatorChannelFactory.CreateChannel();
                Console.WriteLine("Invocate  proxy1.Adds(1)");
                proxy1.Adds(1);
                Console.WriteLine("Invocate  proxy1.Adds(2)");
                proxy1.Adds(2);
                Console.WriteLine("The result return via proxy1.GetResult() is : {0}", proxy1.GetResult());
                try
                {
                    proxy1.Adds(1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("It is fail to invocate the Add after terminating session because \"{0}\"", ex.Message);
                }
                //(proxy1 as ICommunicationObject).Close();
    
    
                Console.WriteLine("Create a calculator proxy: proxy2");
                ICalculator proxy2 = calculatorChannelFactory.CreateChannel();
                Console.WriteLine("Invocate  proxy2.Adds(1)");
                proxy2.Adds(1);
                Console.WriteLine("Invocate  proxy2.Adds(2)");
                proxy2.Adds(2);
                Console.WriteLine("The result return via proxy2.GetResult() is : {0}", proxy2.GetResult());
                //(proxy2 as ICommunicationObject).Close();
                Console.Read();
    
    
            }
        }
    }

    =================================

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    
    
    namespace Contract
    {    
    
        [ServiceContract(SessionMode=SessionMode.Required)]
       public interface ICalculator
        {
            //调用任何允许初始化会话服务方法(缺省情况下所有的服务方法都自动初始化Session,也就是 IsInitiating=true)。
            //调用任何包含 "IsTerminating=true" 声明的服务方法(缺省情况下所有的服务方法 IsTerminating=false,需要我们显示声明)。
            // Terminating 终止意思  Initiating 启动的意思
            [OperationContract(IsOneWay=true,IsInitiating=true,IsTerminating=false)]
            void Adds(double x);
    
            [OperationContract(IsInitiating = false, IsTerminating = true)]
            double GetResult();
    
        }
    }

    ========================================

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using Sevice;
    using System.ServiceModel;
    
    namespace Hosting2
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
                {
                    host.Opened += delegate
                    {
                        Console.WriteLine("The Calculator service has begun to listen");
                    };
                    host.Open();
                    Timer timer = new Timer(delegate { GC.Collect(); }, null, 0, 100);
                    Console.Read();
                }
            }
        }
    }

    ===========

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Contract;
    using System.ServiceModel;
    namespace Sevice
    {
            [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
       public class CalculatorService:ICalculator
        {
    
           public CalculatorService()
           {
               Console.WriteLine("Calculator object has been created");
           }
    
           ~CalculatorService()
           {
               Console.WriteLine("Calculator object has been destoried");
           }
    
    
           private double _result;
            void ICalculator.Adds(double x)
            {
                Console.WriteLine("The Add method is invoked and the current session ID is: {0}", OperationContext.Current.SessionId);
                this._result += x;
    
            }
    
            double ICalculator.GetResult()
            {
                Console.WriteLine("The GetResult method is invoked and the current session ID is: {0}", OperationContext.Current.SessionId);
                return this._result;
    
            }
        }
    }

    客户端配置如下

      <system.serviceModel>
        <client>
          <endpoint address="http://localhost:9999/SessionfulCalculator"
              binding="wsHttpBinding" contract="Contract.ICalculator"
              name="httpEndpoint" />
        </client>
      </system.serviceModel>

    服务端配置如下

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="CalculatorBehavior">
              <serviceMetadata httpGetEnabled="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="CalculatorBehavior" name="Sevice.CalculatorService">
            <endpoint address="" binding="wsHttpBinding" bindingConfiguration=""
              contract="Contract.ICalculator" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:9999/SessionfulCalculator" />
              </baseAddresses>
            </host>
          </service>
        </services>
      </system.serviceModel>
  • 相关阅读:
    日本自然科学研究机构生理学研究所:研究显示古典音乐有助治疗突发性耳聋(转)
    苹果新专利:视频通话不怕掉帧(转)
    三步解决EntityFramework Code First中的MissingMethodException错误
    Unity3D去掉全屏时的屏幕黑边
    WindowsPhone-GameBoy模拟器开发六--[转]指令系统实现必读:补码
    WindowsPhone-GameBoy模拟器开发五--使用XNA初略实现Gameboy显示系统
    WindowsPhone-GameBoy模拟器开发四--Gameboy显示系统分析
    python继承初始化对象实例时 TypeError: module() takes at most 2 arguments (3 given)
    python __getattr__和__getattribute__ 区别
    python – 将列表拆分为较小的列表
  • 原文地址:https://www.cnblogs.com/qqloving/p/2165474.html
Copyright © 2011-2022 走看看