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

This entry was posted in MS CRM 2011 and tagged , , . Bookmark the permalink.

9 Responses to How to call External WCF Service within CRM 2011 Plugins

  1. Mandar says:

    Great Post. Helps a lot!!!

  2. Simsie says:

    I’ve followed this process but on the plugin I get the following error : Business Process Error – Type ‘System.Threading.Tasks.Task`1
    [System.Int32]’ cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

    Do you have any idea why I am getting this? Is it to do with the fact that my WCF service doesn’t have asynch calls?

    • admin says:

      Simsie, You need to serialize the WCF service..DataMembers as [Serializable()] in your interface class.

      • Simsie says:

        I’ve added Serializable() to my interface class but it hasn’t made a difference. I don’t think it should anyway because the datamemember attribute is serializable anyway. [System.Runtime.Serialization.DataMemberAttribute()]

        This has got me stumped. I am setting the step to be synchronous anyway so I don’t see why it should be using Task

  3. Tobin Zerba says:

    I have tried this but I get a runtime error:
    Could not load type ‘System.ServiceModel.HttpBindingBase’ from assembly ‘System.ServiceModel” – The exception message is “Operation could destabilize the runtime.” What am I missing?

  4. Lenin says:

    Just to confirm KC suggested “you may try Generate WCF client proxy with “/syncOnly” that actually worked for me, and I am writing a plugin to fire from CRM 2013 Online calling an external WCF service.
    Thanks KC

Leave a Reply to KC Cancel reply

Your email address will not be published. Required fields are marked *