ServiceProviderEndpoint
1.0.0
IserviceProvider WebAPI端點,以更快,更輕鬆的開發。
如果您已經在集合中註冊了服務,並且希望通過HTTP訪問它們,則只需映射一個像這樣的通用端點:
app . MapServiceProvider ( "services" , builder . Services ) ;
app . Run ( ) ;現在,您可以將帖子/獲取請求發送到您的服務,例如:
GET /services/IYourService/SomeMethod?args=["arg1","arg2","arg3"]
or
POST /services/IYourService/SomeMethod
Content-Type: application/json
["arg1","arg2","arg3"]
帶有仿製藥的示例請求:
GET /services/IYourService/SomeGenericMethod(Int32)?args=[111,222,333]
請求將URL安全符號用於類型。例如,字典(String-array(int32))等效於字典<string,int []> 。
如果您的方法具有對像類型參數,則類型為:
Task < int > ExampleMethod ( object data , CancellationToken cancellationToken ) ;然後,您需要在請求中添加鑄件的類型作為附加參數:
GET /services/IYourService/ExampleMethod/List(String)?args=[["list_item1","list_item2","list_item3"]]
對於下載,該方法返回流對象就足夠了:
Task < Stream > SomeDownloadMethod ( string a , string b , string c , CancellationToken cancellationToken ) ;下載請求就是這樣:
GET /services/IYourService/SomeDownloadMethod?args=["argA","argB","argC"]
為了上傳,該方法必須具有類型流的參數(位置無關緊要):
Task SomeUploadMethod ( Stream stream , string a , string b , string c , CancellationToken cancellationToken ) ; 上傳請求:
POST /services/IYourService/SomeUploadMethod?args=["argA","argB","argC"]
Content-Type: application/octet-stream
<SomeFileData>
JavaScript示例:
let file = document . getElementById ( 'some-input' ) . files [ 0 ] ;
let response = await fetch ( '/services/IYourService/SomeUploadMethod?args=' + encodeURIComponent ( JSON . stringify ( [ "argA" , "argB" , "argC" ] ) ) , {
method : 'POST' ,
headers : { 'content-type' : file . type || 'application/octet-stream' } ,
body : file ,
} ) ; 如果您不想在集合中發布所有服務,請添加一個過濾器:
app . MapServiceProvider ( "services" , builder . Services
. Where ( x => x . ServiceType . Namespace . StartsWith ( "Example" ) ) ) ;如果需要授權,則可以通過IendpointConconcentionBuilder的標準方法添加它:
app . MapServiceProvider ( "services" , builder . Services )
. RequireAuthorization ( ) ;方法可以通過審查者添加:
builder . Services . AddScoped < IExampleService , ExampleService > ( ) ;
builder . Services . Decorate < IExampleService , SecureExampleService > ( ) ; 要從另一個.NET應用程序連接到API,請使用現有客戶端:
using ServiceProviderEndpoint . Client ;
using var client = new SpeClient ( "https://localhost:7149/services" ) ;
var result = await client
. GetService < IYourService > ( )
. SomeMethod ( "arg1" , "arg2" , "arg3" ) ;