.Net之SoapCore简单使用

最近在工作中,需要同时放出来WebAPI接口和支持Soap协议的WebService接口,在dotNetFramework时候玩过WebService,这可是好久没再碰过了,没想到现在居然遇到了。只好迎难而上。

介绍

本来是在ASP.NetCore中使用了组件SoapCore来使用Soap协议。

支持以下框架:

  • .NET 5.0(使用 ASP.NET Core 5.0)

  • .NET Core 3.1(使用 ASP.NET Core 3.1)

  • .NET Core 2.1(使用 ASP.NET Core 2.1)

  • .NET Standard 2.0(使用 ASP.NET Core 2.1)

官网:https://github.com/DigDes/SoapCore

操作

准备工作

为了省事,我还在之前的文章demo上面操作,地址是:https://gitee.com/AZRNG/my-example ,分支是:inmemory_soap ,当前项目已经包含一些WebAPI接口,我要实现使用Soap协议也放出这些接口,共用UserService类。

img

开始编写接口

环境:dotnet5.0 + SoapCore 1.1.0.10

安装组件

<PackageReference Include="SoapCore" Version="1.1.0.10" />

ConfigureServices中注入SoapCore

services.AddSoapCore();

新建User WebService

/// <summary>
/// User WebService
/// </summary>
[ServiceContract]
public class UserContractImpl
{
    private readonly IUserService _userService;
    private readonly IMapper _mapper;

    public UserContractImpl(IUserService userService,
        IMapper mapper)
    {
        _userService = userService;
        _mapper = mapper;
    }

    /// <summary>
    /// 查询用户列表
    /// </summary>
    /// <returns></returns>
    [OperationContract]
    public async Task<List<User>> GetListAsync()
    {
        return await _userService.GetListAsync();
    }

    /// <summary>
    /// 查询详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [OperationContract]
    public async Task<User> GetDetailsAsync(string id)
    {
        return await _userService.GetDetailsAsync(id);
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [OperationContract]
    public async Task<string> AddAsync(AddUserVm dto)
    {
        return await _userService.AddAsync(dto);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    [OperationContract]
    public async Task<int> DeleteAsync(string id)
    {
        return  await _userService.DeleteAsync(id);
    }
}

ConfigureServices中注入

services.AddTransient<UserContractImpl>();

Configure中配置终结点路由

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();

    var binging = new BasicHttpBinding();
    binging.ReaderQuotas.MaxStringContentLength = int.MaxValue;
    endpoints.UseSoapEndpoint<UserContractImpl>("/UserContractImpl.asmx", binging, SoapSerializer.DataContractSerializer);
});

访问地址:http://localhost:5000/UserContractImpl.asmx

img

WebApi程序客户端

新建dotnet5.0项目

img

选中项目右键=>添加=>服务引用=>WCF Web Service

img

输入url,点击go出来服务

img

下一步

img

最后一直下一步直到完成

img

这个时候vs已经帮我们生成了调用的方法,后期地址有变动可以直接去修改这个代码。

ConfigureServices中注册

services.AddSingleton<UserContractImpl>(new UserContractImplClient(UserContractImplClient.EndpointConfiguration.BasicHttpBinding));

控制器注入

private readonly UseService.UserContractImpl _userContractImpl;

public HomeController( UseService.UserContractImpl userContractImpl)
{
    _userContractImpl = userContractImpl;
}

使用里面的接口

var result = await _userContractImpl.AddAsync(new UseService.AddUserVm
{
    Account = "123",
    PassWord = "456",
    Sex = UseService.SexEnum.Man
});
var list = await _userContractImpl.GetListAsync();

通过先调用添加接口然后调用查询接口可以查询到我们刚才添加到的数据。

控制台程序

向上面一样将Soap服务引用到项目中

事例一:直接构建UserContractImplClient

var client = new UserContractImplClient(UserContractImplClient.EndpointConfiguration.BasicHttpBinding);
var str = await client.AddAsync(new AddUserVm
{
    Account = "23456",
    PassWord = "456",
    Sex = SexEnum.Noknow
});
var list = client.GetListAsync();

事例二:

// 创建 HTTP 绑定对象
var binding = new BasicHttpBinding();
// 根据 WebService 的 URL 构建终端点对象
var endpoint = new EndpointAddress(@"http://localhost:5000/UserContractImpl.asmx");
// 创建调用接口的工厂,注意这里泛型只能传入接口
var factory = new ChannelFactory<UserContractImplChannel>(binding, endpoint);
// 从工厂获取具体的调用实例
var callClient = factory.CreateChannel();
// 调用具体的方法,这里是 GetListAsync 方法。
var result = await callClient.GetListAsync();

参考文档

https://github.com/DigDes/SoapCore

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值