Falcon.SugarApi/Falcon.SugarApi.WebService
2023-02-16 15:38:44 +08:00
..
Falcon.SugarApi.WebService.csproj
Readme.md 修改WebService说明 2023-02-16 15:38:44 +08:00
WebServiceClient.cs
WebServiceClientOption.cs
WebServiceExtend.cs

WebService服务器帮助

服务端

  1. 首先定义协议及其实现。
  2. 使用IServiceCollection.AddWebServices注册服务及其实现。
  3. 使用IApplicationBuilder.UseRouting()应用路由中间件。
  4. 调用IApplicationBuilder.UseWebServiceEndpoint方法应用中间件。

客户端

  1. 调用WebServiceClient.CreateWebServiceClient泛型方法创建客户端。泛型为服务端应以的协议方法返回协议对应的通信实现。

实例

服务端:

    /// <summary>
    /// Web服务协议
    /// </summary>
    [ServiceContract]
    public interface IWebSvc
    {
        [OperationContract]
        public string HellowWorld(string name);
    }

    /// <summary>
    /// Web服务协议实现
    /// </summary>
    public class WebSvc:IWebSvc
    {
        public string HellowWorld(string name) {
            return $"Hellow world {name}!";
        }
    }

    //Program.cs

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

客户端:


    using System.ServiceModel;


    /// <summary>
    /// Web服务协议,与服务端匹配
    /// </summary>
    [ServiceContract]
    internal interface IWebSvc
    {
        [OperationContract]
        Task<string> HellowWorldAsync(string name);
    }
    
    //Program.cs

    using Falcon.SugarApi.WebService;

    var url="http://localhost:5000/WebService.asmx";
    var svc = WebServiceClient.CreateWebServiceClient<IWebSvc>(url);
    var result=await svc.HellowWorldAsync("Jeck");