日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

5.WCF 实例

發布時間:2025/3/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

轉載于:https://www.cnblogs.com/rhythmK/archive/2011/06/01/2066502.html

總結

以上是生活随笔為你收集整理的5.WCF 实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。