IserviceProvider Webapi Endpoint pour un développement plus rapide et plus facile.
Si vous avez déjà enregistré des services dans la collection et que vous souhaitez y donner accès via HTTP, alors cartlez simplement un point de terminaison universel comme celui-ci:
app . MapServiceProvider ( "services" , builder . Services ) ;
app . Run ( ) ;Vous pouvez maintenant envoyer des demandes de publication / obtenir à vos services, comme:
GET /services/IYourService/SomeMethod?args=["arg1","arg2","arg3"]
or
POST /services/IYourService/SomeMethod
Content-Type: application/json
["arg1","arg2","arg3"]
Exemple de demande avec les génériques:
GET /services/IYourService/SomeGenericMethod(Int32)?args=[111,222,333]
Les demandes utilisent une notation URL-SAFE pour les types. Par exemple, Dictionary (String-Array (INT32)) équivaut à un dictionnaire <String, int []> .
Si votre méthode a des arguments de type objet comme:
Task < int > ExampleMethod ( object data , CancellationToken cancellationToken ) ;Ensuite, vous devez ajouter le type de Cast en tant que paramètre supplémentaire à la demande:
GET /services/IYourService/ExampleMethod/List(String)?args=[["list_item1","list_item2","list_item3"]]
Pour le téléchargement, il suffit que la méthode renvoie un objet Stream:
Task < Stream > SomeDownloadMethod ( string a , string b , string c , CancellationToken cancellationToken ) ;La demande de téléchargement sera comme ceci:
GET /services/IYourService/SomeDownloadMethod?args=["argA","argB","argC"]
Pour le téléchargement, la méthode doit avoir un argument du flux de type (la position n'a pas d'importance):
Task SomeUploadMethod ( Stream stream , string a , string b , string c , CancellationToken cancellationToken ) ; Demande de téléchargement:
POST /services/IYourService/SomeUploadMethod?args=["argA","argB","argC"]
Content-Type: application/octet-stream
<SomeFileData>
Exemple 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 vous ne souhaitez pas publier tous les services dans la collection, ajoutez simplement un filtre:
app . MapServiceProvider ( "services" , builder . Services
. Where ( x => x . ServiceType . Namespace . StartsWith ( "Example" ) ) ) ;Si l'autorisation est nécessaire, elle est ajoutée par la méthode standard pour iendpointConventionBuilder:
app . MapServiceProvider ( "services" , builder . Services )
. RequireAuthorization ( ) ;La sécurité pour les méthodes peut être ajoutée via des journalistes-décorateurs:
builder . Services . AddScoped < IExampleService , ExampleService > ( ) ;
builder . Services . Decorate < IExampleService , SecureExampleService > ( ) ; Pour se connecter à l'API à partir d'une autre application .NET, utilisez un client existant:
using ServiceProviderEndpoint . Client ;
using var client = new SpeClient ( "https://localhost:7149/services" ) ;
var result = await client
. GetService < IYourService > ( )
. SomeMethod ( "arg1" , "arg2" , "arg3" ) ;