IServiceProvider WebAPI Endpoint para un desarrollo más rápido y más fácil.
Si ya ha registrado servicios en la colección y desea darles acceso a través de HTTP, simplemente asigne un punto final universal como este:
app . MapServiceProvider ( "services" , builder . Services ) ;
app . Run ( ) ;Ahora puede enviar solicitudes de publicación/recibir sus servicios, como:
GET /services/IYourService/SomeMethod?args=["arg1","arg2","arg3"]
or
POST /services/IYourService/SomeMethod
Content-Type: application/json
["arg1","arg2","arg3"]
Solicitud de ejemplo con genéricos:
GET /services/IYourService/SomeGenericMethod(Int32)?args=[111,222,333]
Las solicitudes usan notación segura de URL para tipos. Por ejemplo, el diccionario (string-array (int32)) es equivalente al diccionario <string, int []> .
Si su método tiene argumentos de tipo de objeto como:
Task < int > ExampleMethod ( object data , CancellationToken cancellationToken ) ;Luego debe agregar el tipo de Cast como un parámetro adicional a la solicitud:
GET /services/IYourService/ExampleMethod/List(String)?args=[["list_item1","list_item2","list_item3"]]
Para descargar, es suficiente que el método devuelva un objeto de transmisión:
Task < Stream > SomeDownloadMethod ( string a , string b , string c , CancellationToken cancellationToken ) ;La solicitud de descarga será así:
GET /services/IYourService/SomeDownloadMethod?args=["argA","argB","argC"]
Para cargar, el método debe tener un argumento de flujo de tipo (la posición no importa):
Task SomeUploadMethod ( Stream stream , string a , string b , string c , CancellationToken cancellationToken ) ; Solicitud de carga:
POST /services/IYourService/SomeUploadMethod?args=["argA","argB","argC"]
Content-Type: application/octet-stream
<SomeFileData>
Ejemplo de 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 ,
} ) ; Si no desea publicar todos los servicios en la colección, simplemente agregue un filtro:
app . MapServiceProvider ( "services" , builder . Services
. Where ( x => x . ServiceType . Namespace . StartsWith ( "Example" ) ) ) ;Si se necesita autorización, entonces se agrega mediante el método estándar para IendPointConventionBuilder:
app . MapServiceProvider ( "services" , builder . Services )
. RequireAuthorization ( ) ;Se puede agregar seguridad para los métodos a través de los decoradores de Scrutor:
builder . Services . AddScoped < IExampleService , ExampleService > ( ) ;
builder . Services . Decorate < IExampleService , SecureExampleService > ( ) ; Para conectarse a API desde otra aplicación .NET, use un cliente existente:
using ServiceProviderEndpoint . Client ;
using var client = new SpeClient ( "https://localhost:7149/services" ) ;
var result = await client
. GetService < IYourService > ( )
. SomeMethod ( "arg1" , "arg2" , "arg3" ) ;