zoukankan      html  css  js  c++  java
  • 5.WCF 实例

    契约:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.ServiceModel;
    namespace Rhythmk.Contracts
    {
    /// <summary>
    /// 对象在每次调用前创建,在调用后回收
    /// </summary>

    [ServiceContract]
    public interface IPerCall
    {
    [OperationContract]
    void Add();
    [OperationContract]
    int GetCounter();
    }
    /// <summary>
    /// 为每个会话创建一个新的 System.ServiceModel.InstanceContext 对象。
    /// </summary>
    [ServiceContract]
    public interface IPerSession
    {
    [OperationContract]
    void Add();
    [OperationContract]
    int GetCounter();

    }
    /// <summary>
    /// 只有一个 System.ServiceModel.InstanceContext 对象用于所有传入呼叫,并且在调用后不回收。如果服务对象不存在,则创建
    /// </summary>

    [ServiceContract]
    public interface ISingle
    {
    [OperationContract]
    void Add();
    [OperationContract]
    int GetCounter();

    }
    }

    服务:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Rhythmk.Contracts;
    using System.ServiceModel;

    namespace Rhythmk.Services
    {
    /*
    ServiceBehavior
    ·InstanceContextMode.PerCall - 新的 System.ServiceModel.InstanceContext 对象在每次调用前创建,在调用后回收。
    ·InstanceContextMode.PerSession - 为每个会话创建一个新的 System.ServiceModel.InstanceContext 对象。
    ·InstanceContextMode.Single - 只有一个 System.ServiceModel.InstanceContext 对象用于所有传入呼叫,并且在调用后不回收。如果服务对象不存在,则创建一个。
    */
    [ServiceBehavior(InstanceContextMode
    =InstanceContextMode.PerCall)]
    public class PerCallService:IPerCall
    {
    private int Counter = 0;
    public void Add()
    {
    this.Counter++;
    }

    public int GetCounter()
    {
    return this.Counter;
    }


    }

    [ServiceBehavior(InstanceContextMode
    = InstanceContextMode.PerSession)]
    public class PerSessionService:IPerSession
    {

    private int Counter = 0;
    public void Add()
    {
    this.Counter++;
    }

    public int GetCounter()
    {
    return this.Counter;
    }

    }

    [ServiceBehavior(InstanceContextMode
    = InstanceContextMode.Single)]
    public class SingleService:ISingle
    {

    private int Counter = 0;
    public void Add()
    {
    this.Counter++;
    }

    public int GetCounter()
    {
    return this.Counter;
    }


    }
    }

    寄宿:

    ServiceHost hostPerCallService = new ServiceHost(typeof(PerCallService));
    hostPerCallService.Open();
    ServiceHost hostPerSessionService
    = new ServiceHost(typeof(PerSessionService));
    hostPerSessionService.Open();
    ServiceHost hostSingleService
    = new ServiceHost(typeof(SingleService));
    hostSingleService.Open();
    Console.WriteLine(
    "服务已经启动,按任意键终止服务!");
    Console.ReadKey();
    <?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.PerCallService" behaviorConfiguration="metaBehavior" >
    <endpoint address=""
    binding
    ="wsHttpBinding" bindingConfiguration="" contract="Rhythmk.Contracts.IPerCall" >
    </endpoint>
    <host>
    <baseAddresses>
    <add baseAddress="http://127.0.0.1:1234/Rhythmk.Services.PerCallService"/>
    </baseAddresses>
    </host>
    </service>

    <service name="Rhythmk.Services.PerSessionService" behaviorConfiguration="metaBehavior" >
    <endpoint address=""
    binding
    ="wsHttpBinding" bindingConfiguration="" contract="Rhythmk.Contracts.IPerSession" >
    </endpoint>
    <host>
    <baseAddresses>
    <add baseAddress="http://127.0.0.1:1234/Rhythmk.Services.PerSessionService"/>
    </baseAddresses>
    </host>
    </service>

    <service name="Rhythmk.Services.SingleService" behaviorConfiguration="metaBehavior" >
    <endpoint address=""
    binding
    ="wsHttpBinding" bindingConfiguration="" contract="Rhythmk.Contracts.ISingle" >
    </endpoint>
    <host>
    <baseAddresses>
    <add baseAddress="http://127.0.0.1:1234/Rhythmk.Services.SingleService"/>
    </baseAddresses>
    </host>
    </service>

    </services>
    </system.serviceModel>
    </configuration>

    客户端调用:

    protected void btn_Click(object sender, EventArgs e)
    {
    InvokePerCall();
    //每一次调用的数值都被初始化,则应该输出都为0;
    InvokePerSession();// 输出为2
    InvokeSingle();//累加 每一次都会加
    }
    public void InvokePerSession()
    {
    ClientPerSession.PerSessionClient proxy
    = new Rhythmk.test.ClientPerSession.PerSessionClient();
    proxy.Add();
    proxy.Add();
    Response.Write(
    "IPerSession:" + proxy.GetCounter().ToString() + "<br/>");
    }
    public void InvokePerCall()
    {
    ClientPerCall.IPerCall proxy
    = new ClientPerCall.PerCallClient();
    proxy.Add();
    proxy.Add();
    Response.Write(
    "IPerCall:" + proxy.GetCounter().ToString() + "<br/>");
    }

    public void InvokeSingle()
    {
    // ClientPerSession.PerSessionClient proxy = new Rhythmk.test.ClientPerSession.PerSessionClient();
    ClientSingle.ISingle proxy = new ClientSingle.SingleClient();
    proxy.Add();
    proxy.Add();
    Response.Write(
    "ISingle:" + proxy.GetCounter().ToString() + "<br/>");
    }

    输出:

    第一次触发:
    IPerCall:0
    IPerSession:2
    ISingle:2
    第二次触发:
    IPerCall:0
    IPerSession:2
    ISingle:4
    第三次触发:
    IPerCall:0
    IPerSession:2
    ISingle:6

    一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。
  • 相关阅读:
    八款常用的 Python GUI 开发框架推荐
    scrapy + mogoDB 网站爬虫
    tkinter界面卡死的解决办法
    通过核心API启动单个或多个scrapy爬虫
    爬虫怎样绕过验证码?
    Python爬虫入门教程 33-100 电影评论数据抓取 scrapy
    Python开发 之 Websocket 的使用示例
    StringBuffer详解
    其实python正则表达式就这样简单明了
    ABAP和Java里的单例模式攻击
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2066502.html
Copyright © 2011-2022 走看看