gRPC的四種呼叫方式

2021-09-28 12:30:38 字數 4437 閱讀 3602

普通rpc呼叫,客戶端帶乙個請求物件進行呼叫,服務端返回乙個響應物件。

syntax = "proto3";

option csharp_namespace = "grpcdemoservices";

package greet;

// the greeting service definition.

service greeter

// the request message containing the user's name.

message hellorequest

// the response message containing the greetings.

message helloreply

private

static

async

task

unarystreamcallexample

(greeter.greeterclient client));

console.

writeline

(reply.message)

;}

public

class

greeterservice

:greeter.greeterbase

public

override task

sayhello

(hellorequest request,

servercallcontext context)")

;return task.

fromresult

(new

helloreply);

}}

客戶端帶乙個請求物件進行呼叫,服務端返回多個響應物件

proto同上

private

static

async

task

serverstreamingcallexample

(greeter.greeterclient client)

, cancellationtoken: cts.token))"

);}}

catch

(rpcexception ex) when (ex.statuscode == statuscode.cancelled)

}}

public

class

greeterservice

:greeter.greeterbase

public

override

async

task

sayhellos

(hellorequest request, iserverstreamwriter responsestream,

servercallcontext context)?";

await responsestream.

writeasync

(new

helloreply);

await task.

delay

(1000);

}}}

客戶端帶有多個物件進行請求,服務端返回乙個響應物件

syntax = "proto3";

option csharp_namespace = "grpcdemoservices";

package count;

service counter

// the reqeust message containing the count to increment by

message counterrequest

// the response message containing the current count

message counterreply

private

static

async

task

clientstreamingcallexample

(counter.counterclient client)")

;await call.requeststream.

writeasync

(new

counterrequest);

await task.

delay

(2000);

}await call.requeststream.

completeasync()

;var response =

await call;

console.

writeline

($"count:");

}}

public

void

configureservices

(iservicecollection services)

public

class

incrementingcounter

public

void

increment

(int amount)

}public

class

counterservice

:counter.counterbase

public

override

async task

accumulatecount

(iasyncstreamreader requeststream,

servercallcontext context)")

; _counter.

increment

(request.count);}

return

newcounterreply;}

}

客戶端帶多個請求物件進行呼叫,服務端返回多個響應物件

syntax = "proto3";

option csharp_namespace = "grpcdemoservices";

package race;

service racer

message racemessage

private

static

async

task

bidirectionalstreamingexample

(racer.racerclient client)

; console.

writeline

("ready, set, go!");

using

(var call = client.

readysetgo

(new

calloptions

(headers)))

});// write outgoing messages until timer is complete

var sw = stopwatch.

startnew()

;var sent =0;

while

(sw.elapsed < raceduration));

}// finish call and report results

await call.requeststream.

completeasync()

;await readtask;

console.

writeline

($"messages sent: ");

console.

writeline

($"messages received: ");

}}

public

class

racerservice

:racer.racerbase})

;// write outgoing messages until timer is complete

var sw = stopwatch.

startnew()

;var sent =0;

while

(sw.elapsed < raceduration));

}await readtask;

}}

所有**均可在github倉庫下找到,本文中的**在grpcclientgrpcserver目錄下。

函式的四種呼叫方式

函式作為物件的屬性時,稱為方法。此時函式 即方法 中的this對應是該物件。var myobject 方法呼叫模式,this對應的是myobject物件 myobject.func 3 也可以寫成如下格式 var myobject myobject.func function 方法呼叫模式 myob...

css的四種呼叫方式

樣式的使用有大體有四種方式 外聯式樣式表 內嵌樣式表 元素內定 匯入樣式表 1,外聯式樣式表 新增在html的頭資訊識別符號 head 裡 head link rel stylesheet href css type text css media screen head 其中href是目標文件的ur...

GRPC的四種服務型別

上次簡單介紹了grpc的使用方法,並建立了乙個方法呼叫,在grpc中有四種服務型別,下面分別進行介紹 這就是一般的rpc呼叫,乙個請求物件對應乙個返回物件 proto語法 rpc hello person returns result service override public void hel...