Consume an external WCF service from a custom workflow/plugin

AIM: To call an external WCF service from a custom workflowor a plugin.

Since custom workflows and plugins are essentially class libraries, there is no associated config file which can hold the binding details of the WCF service. So, to consume a WCF service, a proxy file needs to be generated using svcutil.exe. This command needs to executed from the command prompt. Now to generate the proxy file, navigate to the location of the svcutil.exe on the machine and execute the following:

svcutil.exe /language:cs /out:proxy.cs /config:app.config http://server:port/servicename.svc

A proxy.cs file is generated at the location of the svcutil.exe. This proxy.cs needs to be included in the workflow or plugin solution.

The binding for the WCF serviceshould be set in the code as follows:

BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.Name = "<Give a Binding Name here>";
myBinding.Security.Mode = BasicHttpSecurityMode.None;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress endPointAddress = new EndpointAddress(" http://server:port/servicename.svc");
WCFClient client = new WCFClient(myBinding, endPointAddress);
Theclient’ object will now be able to access the WCF service methods for e.g. client.insertDetails("Parameter_1", " Parameter_2");

1 comment:

Popular Posts