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" ) ;