最近在工作中,需要同时放出来WebAPI接口和支持Soap协议的WebService接口,在dotNetFramework时候玩过WebService,这可是好久没再碰过了,没想到现在居然遇到了。只好迎难而上。
本来是在ASP.NetCore中使用了组件SoapCore来使用Soap协议。
支持以下框架:
为了省事,我还在之前的文章demo上面操作,地址是: gitee.com/AZRNG/my-ex… ,分支是:inmemory_soap ,当前项目已经包含一些WebAPI接口,我要实现使用Soap协议也放出这些接口,共用UserService类。
开始编写接口
环境: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
WebApi程序客户端
新建dotnet5.0项目
选中项目右键=>添加=>服务引用=>WCF Web Service
输入url,点击go出来服务
最后一直下一步直到完成
这个时候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();