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

    一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。
  • 相关阅读:
    asp.net调用mysql 存储过程 带 out 返回值,返回刚插入数据库中的自增的ID,LAST_INSERT_ID() 的使用
    如何俘获一个 IT 男的心,让他成为男友然后变成老公?
    MySqlHelper.cs mysql数据库助手类
    mysql 按年度、季度、月度、周、日SQL统计查询,mysql 存储过程 中 in 和 FIND_IN_SET 传递多个参数的使用
    奇怪的母版页里面的 form 表单里面的 enctype="multipart/formdata" html控件上传 FileUpload控件上传 一次多图片上传
    asp.net 连接 Mysql 代码生成器(下载地址)
    Convert.ToInt32、(int)和int.Parse,int.TryParse四者之间的区别:
    在web项目中 使用 WebService 根据IP地址来源搜索实际物理地址,常用的WebServices
    vc6控制台程序利用SoapToolkit3.0调用WebService
    浅议C++/CLI的gcnew关键字
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2066502.html
Copyright © 2011-2022 走看看