Recently, in my work, I need to put out WebAPI interface and WebService interface supporting Soap protocol at the same time. I played WebService when I was in dotNetFramework, but I haven’t touched WebService for a long time. I didn’t expect to encounter it now. We had to grasp the nettle.

introduce

ASP.NetCore originally used the component SoapCore to use the Soap protocol.

The following frameworks are supported:

  • .NET 5.0 (using ASP.NET Core 5.0)
  • .NET Core 3.1 (using ASP.NET Core 3.1)
  • .net Core 2.1 (with ASP.NET Core 2.1)
  • .NET Standard 2.0 (using ASP.NET Core 2.1)

Liverpoolfc.tv: github.com/DigDes/Soap…

operation

The preparatory work

In order to save trouble, I also work on the previous article demo, the address is: gitee.com/AZRNG/my-ex… The branch is: Inmemory_SOAP. The current project already includes some WebAPI interfaces, which I will implement using the Soap protocol and also release, sharing the UserService class.

img

Start writing the interface

Environment: Dotnet5.0 + SoapCore 1.1.0.10

Installation of components

The < PackageReference Include = "SoapCore" Version = "1.1.0.10" / >Copy the code

Inject SoapCore into ConfigureServices

services.AddSoapCore();
Copy the code

New 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> [OperationContract] public Async Task<List<User>> GetListAsync() { return await _userService.GetListAsync(); } / / / < summary > for more information / / / / / / < summary > / / / < param name = "id" > < param > / / / < returns > < / returns > [OperationContract] public  async Task<User> GetDetailsAsync(string id) { return await _userService.GetDetailsAsync(id); } / / / < summary > / / / add / / / < summary > / / / < param name = "dto" > < param > / / / < returns > < / returns > [OperationContract] public async Task<string> AddAsync(AddUserVm dto) { return await _userService.AddAsync(dto); } /// <summary> // delete /// </summary> /// <param name="id"></param> [OperationContract] public async Task<int> DeleteAsync(string id) { return await _userService.DeleteAsync(id); }}Copy the code

ConfigureServices inject

services.AddTransient<UserContractImpl>();
Copy the code

Configure Configure endpoint routing

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

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

Access the address: http://localhost:5000/UserContractImpl.asmx

img

WebApi application client

Create a Dotnet5.0 project

img

Right-click the project => Add => Service reference =>WCF Web Service

img

Enter the URL and click Go to serve

img

The next step

img

And then you go all the way to the next step

img

At this time vs has already generated the method called for us. If the address changes later, we can directly modify this code.

ConfigureServices registered in

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

Controller injection

private readonly UseService.UserContractImpl _userContractImpl;

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

Use the interface inside

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

The data we just added can be queried by calling the add interface and then the query interface.

Console program

Reference the Soap service to the project as above

Example 1: Build UserContractImplClient directly

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();
Copy the code

Case 2:

Var binding = new BasicHttpBinding(); / / according to the web service URL to build var endpoint termination point object = new EndpointAddress (@ "http://localhost:5000/UserContractImpl.asmx"); Var Factory = new ChannelFactory<UserContractImplChannel>(binding, endpoint) Var callClient = factory.createchannel (); var callClient = factory.createchannel (); // Call the specific method, in this case the GetListAsync method. var result = await callClient.GetListAsync();Copy the code

Reference documentation

Github.com/DigDes/Soap…