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

你可能感兴趣的:(WCF)