5.WCF 实例
契約:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace Rhythmk.Contracts
{
/// <summary>
/// 對象在每次調(diào)用前創(chuàng)建,在調(diào)用后回收
/// </summary>
[ServiceContract]
public interface IPerCall
{
[OperationContract]
void Add();
[OperationContract]
int GetCounter();
}
/// <summary>
/// 為每個會話創(chuàng)建一個新的 System.ServiceModel.InstanceContext 對象。
/// </summary>
[ServiceContract]
public interface IPerSession
{
[OperationContract]
void Add();
[OperationContract]
int GetCounter();
}
/// <summary>
/// 只有一個 System.ServiceModel.InstanceContext 對象用于所有傳入呼叫,并且在調(diào)用后不回收。如果服務(wù)對象不存在,則創(chuàng)建
/// </summary>
[ServiceContract]
public interface ISingle
{
[OperationContract]
void Add();
[OperationContract]
int GetCounter();
}
}
服務(wù):
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 對象在每次調(diào)用前創(chuàng)建,在調(diào)用后回收。
·InstanceContextMode.PerSession - 為每個會話創(chuàng)建一個新的 System.ServiceModel.InstanceContext 對象。
·InstanceContextMode.Single - 只有一個 System.ServiceModel.InstanceContext 對象用于所有傳入呼叫,并且在調(diào)用后不回收。如果服務(wù)對象不存在,則創(chuàng)建一個。
*/
[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("服務(wù)已經(jīng)啟動,按任意鍵終止服務(wù)!");
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>
客戶端調(diào)用:
protected void btn_Click(object sender, EventArgs e){
InvokePerCall();//每一次調(diào)用的數(shù)值都被初始化,則應(yīng)該輸出都為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/>");
}
輸出:
第一次觸發(fā):
IPerCall:0
IPerSession:2
ISingle:2
第二次觸發(fā):
IPerCall:0
IPerSession:2
ISingle:4
第三次觸發(fā):
IPerCall:0
IPerSession:2
ISingle:6
轉(zhuǎn)載于:https://www.cnblogs.com/rhythmK/archive/2011/06/01/2066502.html
總結(jié)
- 上一篇: vb编程的好帮手--资源文件
- 下一篇: MySQL 当记录不存在时insert,