Hi ,
in my previous post I have explained how to interact WCF Services with Microsoft Dynamics CRM.
In this post I would like to explain how to invoke the Wcf services with CRM service using plugin c#.
• For calling external wcf services within our crm service, we can generate WCF Client Class similar to our early bound class generation.
Goto Visual Studio Tools –> Developer Command
• Use Svcutil.exe and map to the desired location where your client code has to get generated.
Type the following command : svcutil.exe followed by language type , Early Bound class Name.cs , Hosted Url of your WCF Application
svcutil.exe /language:cs /out:CreateLead.cs http://****:8081/Service1.svc
Add the generated CreateLead.cs class in your application and the namespace reference in your plugin class file
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using System.Net; using System.ServiceModel; using CreateLead; // Generated WCF Class namespace Crm_Plugin { public class WCF_Integration : IPlugin { public void Execute(IServiceProvider serviceProvider) { //Extract the tracing service for use in debugging sandboxed plug-ins. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); //<snippetFollowupPlugin1> // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); //</snippetFollowupPlugin1> //<snippetFollowupPlugin2> // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parameters. Entity entity = (Entity)context.InputParameters["Target"]; // Invoke WCF SERVICE BasicHttpBinding myBinding = new BasicHttpBinding(); myBinding.Name = "BasicHttpBinding_IService1"; 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://******:8080/Service1.svc"); ChannelFactory<IService1> SerFactor = new ChannelFactory<IService1>(myBinding, endPointAddress); IService1 chanl = SerFactor.CreateChannel(); // Passing Parameter to the Wcf Class CreateLead string Result =chanl.CreateLead(entity.GetAttributeValue<string>("firstname").ToString(), entity.GetAttributeValue<string>("lastname").ToString(), entity.GetAttributeValue<string>("emailaddress1").ToString()); SerFactor.Close(); } } } }
* Note ,
i have registered my plugin in pre create Contact Entity .
Passing [ First Name , Last Name , Email Address ] 3 parameters to out CreateLead WCF Method .