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-SAFE 표기법을 사용합니다. 예를 들어, Dictionary (String-Array (int32))는 Dictionary <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>
자바 스크립트 예 :
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 ( ) ;방법에 대한 보안은 면세기를 통해 추가 할 수 있습니다.
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" ) ;