1、同步请求回复操作
对服务操作来说,同步请求回复消息交换是最普通的模式
2、异步访问服务操作
请求-响应服务操作会令客户端阻塞直到服务操作执行结束,异步方式可以采用回调方式解决等待问题
注意:服务端不知道客户端在使用异步编程,客户端的异步模式无需服务器参与操作
、
代码:
class
Program
{
static
int
c = 0;
static
void
Main(
string
[] args)
{
StockServiceClient proxy =
new
StockServiceClient();
IAsyncResult arGetPrice;
for
(
int
i = 0; i < 10; i++)
{
arGetPrice = proxy.BeginGetPrice(
"msft"
, GetPriceCallback, proxy);
InterLocker.Increment(
ref
c);
}
while
(c > 0)
{
Thread.Sleep(1000);
Console.WriteLine(
"Waiting... Calls outstanding:{0}"
, c);
}
proxy.Close();
Console.WriteLine(
"Done!"
);
}
//Asynchronous callbacks for displaying results.
static
void
GetPriceCallback(IAsyncResult ar)
{
double
d = ((StockServiceClient)ar.AsyncState).EndGetPrice(ar);
Interlocked.Decrement(
ref
c);
}
}
3、单向操作
向服务器发送消息却不需要回应,只需要知道发送成功即可,[OperationContract(IsOneWay =
true
)] 即可实现
代码:
[ServiceContract]
public
interface
IStockService
{
[OperationContract(IsOneWay =
true
)]
void
DoBigAnalysisFast(
string
ticker);
[OperationContract]
void
DoBigAnalysisSlow(
string
ticker);
}
4、双工操作
双工操作使得服务器向客户端发送消息成为可能,信道一旦建立,可以向任一方发送未经请求的消息