{"id":2837,"date":"2019-11-11T16:20:04","date_gmt":"2019-11-11T15:20:04","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2837"},"modified":"2022-07-25T08:33:24","modified_gmt":"2022-07-25T07:33:24","slug":"c-soap-client","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-soap-client\/","title":{"rendered":"C# SVC \/ WCF \/Soap Client"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.ServiceModel;\n\npublic class SoapClientWrapper&lt;T> : IDisposable where T : ICommunicationObject, IDisposable\n{\n    public SoapClientWrapper(T client) => Client = client;\n    public T Client { get; private set; }\n    private bool disposedValue = false; \/\/ To detect redundant calls\n    protected virtual void Dispose(bool disposing)\n    {\n        if (disposedValue || !disposing || Client == null)\n            return;\n        try\n        {\n            if (Client.State == CommunicationState.Faulted)\n                Client.Abort();\n            else\n                Client.Close();\n        }\n        finally\n        {\n            Client.Dispose();\n        }\n        disposedValue = true;\n    }\n    public void Dispose() => Dispose(true);\n}\n\n\n\/\/ Example usage:\nprivate static SoapClientWrapper&lt;MyWebserviceSVC> GetSoapClient(string url)\n{\n\tvar endpoint = new EndpointAddress(url);\n\tvar securityMode = endpoint.Uri.Scheme.ToLower().Contains(\"https:\") ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None;\n\tvar binding = new BasicHttpBinding(securityMode);\n\n\tvar client = new MyWebserviceSVC(binding, endpoint);\n\tServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;\n\n\t\/\/client.ClientCredentials.UserName.UserName = userName;\n\t\/\/client.ClientCredentials.UserName.Password = password;\n\treturn new SoapClientWrapper&lt;MyWebserviceSVC>(client);\n}\n\nusing (var client = GetSoapClient(url))\n{\n\tvar response = await client.Client.MyWebserviceCallAsync(clientId, clientKey, message, transactionId, exitCode);\n\treturn response.Body.ExitCode == \"200\";\n}\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class SoapApi\n{\n\tprivate static WebConnectSoapClient GetSoapClient()\n\t{\n\t\tvar client = new WebConnectSoapClient();\n\t\tServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;\n\n\t\t\/\/var wsHttpBinding = client.Endpoint.Binding as System.ServiceModel.WSHttpBinding;\n\t\t\/\/wsHttpBinding.BypassProxyOnLocal = true;\n\t\t\/\/wsHttpBinding.UseDefaultWebProxy = false;\n\t\t\/\/wsHttpBinding.ProxyAddress = null;\n\t\t\/\/client.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings[\"WebserviceUser\"];\n\t\t\/\/client.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings[\"WebservicePassword\"];\n\t\treturn client;\n\t}\n\tpublic static void WebserviceCloseOrAbort(ICommunicationObject communicationObject)\n\t{\n\t\tif (communicationObject == null)\n\t\t\treturn;\n\t\tif (communicationObject.State == CommunicationState.Faulted)\n\t\t\tcommunicationObject.Abort();\n\t\telse\n\t\t\tcommunicationObject.Close();\n\t}\n\tprivate static T Execute&lt;T>(Func&lt;WebConnectSoapClient, T> func)\n\t{\n\t\tusing (var client = GetSoapClient())\n\t\t{\n\t\t\ttry\n\t\t\t{\n\t\t\t\tclient.Open();\n\t\t\t\treturn func(client);\n\t\t\t}\n\t\t\tfinally\n\t\t\t{\n\t\t\t\tWebserviceCloseOrAbort(client);\n\t\t\t}\n\t\t}\n\t}\n\tpublic async Task&lt;string> GetOrderMessages()\n\t{\n\t\tstring clientId = \"\";\n\t\tstring clientKey = \"\";\n\t\tstring message = \"\";\n\t\tstring transactionId = \"\";\n\t\tstring exitCode = \"\";\n\t\tvar response = await Execute((client) => client.M10100Async(clientId, clientKey, message, transactionId, exitCode));\n\t\treturn response.Body.ExitCode;\n\t}\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-2837","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2837"}],"version-history":[{"count":8,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2837\/revisions"}],"predecessor-version":[{"id":4227,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2837\/revisions\/4227"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}