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" ) ) ) ;許可が必要な場合は、IENDPointConventionBuilderの標準方法によって追加されます。
app . MapServiceProvider ( "services" , builder . Services )
. RequireAuthorization ( ) ;メソッドのセキュリティは、Scrutor-Decoratorsを介して追加できます。
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" ) ;