A .NET Core 2 SOAP Service Cheatsheet

Published on 07 November 2017

If you have to consume (SAP) SOAP services with .NET Core 2 you might find the following steps and code samples helpful. UPDATE: Chris van Sluijsveld pointed out that the Connected Services feature makes this easier!

  • Generate the client proxy code with Visual Studio's "Add service reference" function in a full (old) .NET Framework project
  • copy the generated C# code to your .NET Core project and keep the generated config for reference
  • Use the Connected Services feature of Visual Studio 2017 in your project to generate a reference to your SOAP service Connected Services Connected Services
  • add the System.ServiceModel.Http package to your .NET Core project
  • Build the service client in your .NET Core 2 project by looking at the config created earlier
  • Look for the proxy class based on System.ServiceModel.ClientBase to create your service client
  • The sample code below uses basic username/password authentication over HTTPS - your mileage may vary
public void CallSoap()
        {
            System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.Transport);
                binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;

                MyServiceContractInterfaceClient client = new MyServiceContractInterfaceClient(
                    binding,new System.ServiceModel.EndpointAddress("https://svc.company.com:55555/SOAP/MessageServlet?param=&interface=FOO"));
                client.ClientCredentials.UserName.UserName = "USER";
                client.ClientCredentials.UserName.Password = "PW";

 //there is no ServicePointManager.ServerCertificateValidationCallback
 //in netcore           client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication
                {
                    CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.Custom,
                    CustomCertificateValidator = new MyX509CertificateValidator("CN=Contoso.com")
                };

            
                string result = client.MyServiceContractInterfaceCall("arg");
        }


    public class MyX509CertificateValidator : System.IdentityModel.Selectors.X509CertificateValidator
    {
        string allowedIssuerName;

        public MyX509CertificateValidator(string allowedIssuerName)
        {
            if (allowedIssuerName == null)
            {
                throw new ArgumentNullException("allowedIssuerName");
            }

            this.allowedIssuerName = allowedIssuerName;
        }

        public override void Validate(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate)
        {
            //Validate!
        }
    }