C# SVC / WCF /Soap Client

Date: 2019-11-11
using System;
using System.ServiceModel;

public class SoapClientWrapper<T> : IDisposable where T : ICommunicationObject, IDisposable
{
    public SoapClientWrapper(T client) => Client = client;
    public T Client { get; private set; }
    private bool disposedValue = false; // To detect redundant calls
    protected virtual void Dispose(bool disposing)
    {
        if (disposedValue || !disposing || Client == null)
            return;
        try
        {
            if (Client.State == CommunicationState.Faulted)
                Client.Abort();
            else
                Client.Close();
        }
        finally
        {
            Client.Dispose();
        }
        disposedValue = true;
    }
    public void Dispose() => Dispose(true);
}


// Example usage:
private static SoapClientWrapper<MyWebserviceSVC> GetSoapClient(string url)
{
	var endpoint = new EndpointAddress(url);
	var securityMode = endpoint.Uri.Scheme.ToLower().Contains("https:") ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None;
	var binding = new BasicHttpBinding(securityMode);

	var client = new MyWebserviceSVC(binding, endpoint);
	ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

	//client.ClientCredentials.UserName.UserName = userName;
	//client.ClientCredentials.UserName.Password = password;
	return new SoapClientWrapper<MyWebserviceSVC>(client);
}

using (var client = GetSoapClient(url))
{
	var response = await client.Client.MyWebserviceCallAsync(clientId, clientKey, message, transactionId, exitCode);
	return response.Body.ExitCode == "200";
}
public class SoapApi
{
	private static WebConnectSoapClient GetSoapClient()
	{
		var client = new WebConnectSoapClient();
		ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

		//var wsHttpBinding = client.Endpoint.Binding as System.ServiceModel.WSHttpBinding;
		//wsHttpBinding.BypassProxyOnLocal = true;
		//wsHttpBinding.UseDefaultWebProxy = false;
		//wsHttpBinding.ProxyAddress = null;
		//client.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["WebserviceUser"];
		//client.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["WebservicePassword"];
		return client;
	}
	public static void WebserviceCloseOrAbort(ICommunicationObject communicationObject)
	{
		if (communicationObject == null)
			return;
		if (communicationObject.State == CommunicationState.Faulted)
			communicationObject.Abort();
		else
			communicationObject.Close();
	}
	private static T Execute<T>(Func<WebConnectSoapClient, T> func)
	{
		using (var client = GetSoapClient())
		{
			try
			{
				client.Open();
				return func(client);
			}
			finally
			{
				WebserviceCloseOrAbort(client);
			}
		}
	}
	public async Task<string> GetOrderMessages()
	{
		string clientId = "";
		string clientKey = "";
		string message = "";
		string transactionId = "";
		string exitCode = "";
		var response = await Execute((client) => client.M10100Async(clientId, clientKey, message, transactionId, exitCode));
		return response.Body.ExitCode;
	}
}
28370cookie-checkC# SVC / WCF /Soap Client