Recipe of creating a SOAP-server
Instantiate an empty ASP.NET Core project.
Modify LaunchSettings.json to look like this:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "",
"sslPort": 0
}
},
"profiles": {
"SoapServer": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": "true",
"applicationUrl": "",
"jsWebView2Debugging": true
}
}
}
So that when looking at project->Debug in Visual Studio , you will see:
Ensure that the following packages are installed in Nuget:
Create a folder a name it, let's say Models.
In that folder create an interface called ISampleService with methods Ping and GetToday and looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Threading.Tasks;
namespace SoapServer.Models
{
[ServiceContract]
public interface ISampleService
{
[OperationContract]
string Ping(string s);
[OperationContract]
DateTime GetToday();
}
}
and in the same folder add the respective class SampleService that uses the interface above.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//https://github.com/DigDes/SoapCore
namespace SoapServer.Models
{
public class SampleService: ISampleService
{
public string Ping(string s)
{
return s + ", my great friend.... !!!!!";
}
public DateTime GetToday()
{
return DateTime.Now;
}
}
}
In the StartUp.cs file add the following code to ConfigureServices and Configure
using Microsoft.Extensions.DependencyInjection.Extensions;
using SoapCore;
using SoapServer.Models;
.
.
public void ConfigureServices(IServiceCollection services)
{
services.AddSoapCore();
services.TryAddSingleton();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.UseSoapEndpoint
("/Service.asmx", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
});
}
Assuming that the Soap-Webservice is accessed on port 8081
modify code in Program.cs:
//Delete all methods and replace it with the following:
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:8081")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup
()
.ConfigureLogging(x =>
{
x.AddDebug();
x.AddConsole();
})
.Build();
host.Run();
}
Hereafter when running the application, it shows this:
How to create a Soap-client:
Create a Console application, ensure that the Nuget package is installed:
Modify Program.cs so that it has the following content:
using SoapServer.Models; using System; using System.ServiceModel; //BasicHttpBinding, EndpointAddress and ChannelFactory are //members of this assembly/Namespace static void Main(string[] args) { var binding = new BasicHttpBinding(); var endpoint = new EndpointAddress(new Uri(string.Format("http://{0}:8081/Service.asmx", Environment.MachineName))); var channelFactory = new ChannelFactory
Which should result in the outcome: