Call an external WCF service from a custom workflow or a plugin in MS CRM
As we know very well custom workflow and plugins are the class libraries and obviously there is no configuration file associated with class libraries projects and that is the reason we have to follow below approach to call an external WCF service from the plugin or workflow.
First of all a proxy file needs to be generated using svcutil.exe. This command needs to execute from the command prompt. To generate the proxy file, navigate to the location of the svcutil.exe on the machine (C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools) and execute the following:
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools>svcut
il.exe /language:cs /out:proxy.cs /config:app.config <SVC Service>
After executing the command a proxy.cs file is generated at the location of the svcutil.exe. This proxy.cs should be included in the workflow or plugin solution.
Once the generated proxy.cs file added into the project, for the bindings of WCF service below code should be written where you wanted to call the service.
BasicHttpBinding serviceBinding = new BasicHttpBinding();
serviceBinding.Name = "<Binding Name>";
serviceBinding.Security.Mode = BasicHttpSecurityMode.None;
serviceBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
serviceBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
serviceBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress endPointAddress = new EndpointAddress("http:<ServiceURLPaTH>.svc");
ServiceClient serclient = new ServiceClient(serviceBinding, endPointAddress);
Now you can use Serclient object to call the service methods.
Hope this will be helpful.