2023-02-16 15:25:37 +08:00
|
|
|
|
## WebService服务器帮助
|
|
|
|
|
|
|
|
|
|
### 服务端
|
|
|
|
|
|
2023-02-16 15:38:44 +08:00
|
|
|
|
1. 首先定义协议及其实现。
|
|
|
|
|
2. 使用IServiceCollection.AddWebServices注册服务及其实现。
|
|
|
|
|
2. 使用IApplicationBuilder.UseRouting()应用路由中间件。
|
2023-02-16 15:25:37 +08:00
|
|
|
|
3. 调用IApplicationBuilder.UseWebServiceEndpoint方法应用中间件。
|
|
|
|
|
|
|
|
|
|
### 客户端
|
|
|
|
|
|
2023-02-16 15:38:44 +08:00
|
|
|
|
1. 调用WebServiceClient.CreateWebServiceClient泛型方法创建客户端。泛型为服务端应以的协议,方法返回协议对应的通信实现。
|
2023-02-16 15:25:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 实例
|
|
|
|
|
|
|
|
|
|
服务端:
|
|
|
|
|
~~~c#
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Web服务协议
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ServiceContract]
|
|
|
|
|
public interface IWebSvc
|
|
|
|
|
{
|
|
|
|
|
[OperationContract]
|
|
|
|
|
public string HellowWorld(string name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 15:38:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Web服务协议实现
|
|
|
|
|
/// </summary>
|
2023-02-16 15:25:37 +08:00
|
|
|
|
public class WebSvc:IWebSvc
|
|
|
|
|
{
|
|
|
|
|
public string HellowWorld(string name) {
|
|
|
|
|
return $"Hellow world {name}!";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 15:38:44 +08:00
|
|
|
|
//Program.cs
|
|
|
|
|
|
2023-02-16 15:25:37 +08:00
|
|
|
|
using Falcon.SugarApi.WebService;
|
|
|
|
|
using Server;
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
builder.Services.AddWebService<IWebSvc,WebSvc>();
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
app.UseWebServiceEndpoint<IWebSvc>("/WebService.asmx");
|
|
|
|
|
|
|
|
|
|
app.Run();
|
|
|
|
|
|
|
|
|
|
~~~
|
|
|
|
|
|
|
|
|
|
客户端:
|
|
|
|
|
~~~c#
|
|
|
|
|
|
|
|
|
|
using System.ServiceModel;
|
2023-02-16 15:38:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Web服务协议,与服务端匹配
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ServiceContract]
|
2023-02-16 15:25:37 +08:00
|
|
|
|
internal interface IWebSvc
|
|
|
|
|
{
|
2023-02-16 15:38:44 +08:00
|
|
|
|
[OperationContract]
|
2023-02-16 15:25:37 +08:00
|
|
|
|
Task<string> HellowWorldAsync(string name);
|
|
|
|
|
}
|
2023-02-16 15:38:44 +08:00
|
|
|
|
|
|
|
|
|
//Program.cs
|
2023-02-16 15:25:37 +08:00
|
|
|
|
|
|
|
|
|
using Falcon.SugarApi.WebService;
|
|
|
|
|
|
2023-02-16 15:38:44 +08:00
|
|
|
|
var url="http://localhost:5000/WebService.asmx";
|
|
|
|
|
var svc = WebServiceClient.CreateWebServiceClient<IWebSvc>(url);
|
2023-02-16 15:25:37 +08:00
|
|
|
|
var result=await svc.HellowWorldAsync("Jeck");
|
|
|
|
|
|
|
|
|
|
~~~
|