這一節
接下來
請求答復模式是我們最常見和最常用的模式
服務器代碼
[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(int value)
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite)
// TODO: 在此添加您的服務操作
}
public class Service
{
public string GetData(int value)
{
System
return string
}
}
客戶端
private void button
{
//請求響應模式
//等待服務器端執行完畢
SingleService
string result = client
MessageBox
}
單程模式是客戶端向服務器端發送消息後
使用單程模式的時候
單程模式的操作契約的返回值都為void
服務器端契約
[OperationContract(IsOneWay=true)]
void SingleGetData(string name)
客戶端
private void button
{
//單程模型
//不用等待服務器端
SingleService
client
MessageBox
}
雙工模式就是客戶端可以向服務器端發送信息
調用客戶端的方法
雙工模式實際上可以分為單程雙工模式和請求答復雙工模式
服務器端契約
[ServiceContract(CallbackContract = typeof(ICallBackDuplService))]
public interface IDuplService
{
[OperationContract]
void DoWork()
[OperationContract(IsOneWay=true)]
void GetDuplData(string data)
}
public interface ICallBackDuplService
{
[OperationContract(IsOneWay=true)]
void ShowMsg(string msg)
}
public class DuplService : IDuplService
{
public void DoWork()
{
}
#region IDuplService 成員
public void GetDuplData(string data)
{
string str=
//獲取當前操作的上下文
ICallBackDuplService callback = OperationContext
callback
}
}
客戶端代碼
private void button
{
InstanceContext instanceContext = new InstanceContext(new CallBackDuplService())
DuplService
client
}
public class CallBackDuplService:DuplService
{
#region IDuplServiceCallback 成員
public void ShowMsg(string msg)
{
//顯示數據
System
}
#endregion
}
服務器端契約
[ServiceContract(CallbackContract = typeof(ICallBackDuplService))]
public interface IDuplService
{
[OperationContract]
void DoWork()
[OperationContract(IsOneWay = true)]
void HelloDupl(string name)
}
public interface ICallBackDuplService
{
[OperationContract]
void ConsoleMsg(string msg)
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode
public class DuplService : IDuplService
{
#region IDuplService 成員
public void HelloDupl(string name)
{
string str =
ICallBackDuplService callback = OperationContext
callback
}
#endregion
}
客戶端
private void button
{
InstanceContext instanceContext = new InstanceContext(new CallBackDuplService())
DuplService
client
}
public class CallBackDuplService:DuplService
{
#region IDuplServiceCallback 成員
public void ShowMsg(string msg)
{
//顯示數據
System
}
#endregion
#region IDuplServiceCallback 成員
public void ConsoleMsg(string msg)
{
System
}
#endregion
}
From:http://tw.wingwit.com/Article/program/net/201311/12127.html