How to call External WCF Service within CRM 2011 Plugins

Step 1: Build and Publish the WCF Service

Step 2: Deploy the WCF Service

Step 3: Create WCF client

Create a WCF client using  ServiceModel Metadata Utility Tool (Svcutil.exe) by using following steps:

  1. On the Start menu click All Programs, and then click Visual Studio 2010. Click Visual Studio Tools and then click Visual Studio 2010 Command Prompt.
  2. Navigate to the directory where you want to place the client code.
  3. Use the command-line tool ServiceModel Metadata Utility Tool (Svcutil.exe) with the appropriate switches to create the client code. The following example generates a code file and a configuration file for the service.

svcutil.exe /language:cs /out:TaxServiceClient.cs http://192.168.124.26:81/TaxProductionService

Step 4: Write Plugin to consume external WCF Service

  1. Add the above generated .CS class into the plugin solution. In Visual Studio, right-click the client project in Solution Explorer and select Add and then Existing Item. Select the TaxServiceClient.cs file generated in the preceding step.
  2. Open the plugin.cs class from the solution. And call the WCF client using channel factory.

#region Calling WCF Service

BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.Name = “BasicHttpBinding_IPOCService”;
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://192.168.124.26:81/TaxProductionService.svc“);
ChannelFactory<IPOCService> factory = new ChannelFactory<IPOCService>(myBinding, endPointAddress);
IPOCService channel = factory.CreateChannel();
taxAmount = channel.GetCalculatedTaxValue(unitPrice);
factory.Close();

#endregion

Step 5: Register the Plugin using registration tool.

You can also download a sample code from below link:
http://code.msdn.microsoft.com/How-to-call-External-WCF-42c4490d

Posted in MS CRM 2011 | Tagged , , | 9 Comments