Call WCF service from plugin in Dynamics CRM 2011

Create a custom entity to maintain all configuration values, In my case I created entity like below and added caseservice details(Name: CaseServiceURL, Column: CaseService, Value: http://localhost/CaseService/Case.svc)

Entity Name: ls_settings

Attributes: Name, Column, Value

After you have created a service, generate the service proxy file using svcutil.exe tool from visual studio command line.

Svcutil.exe http://localhost/CaseService/Case.svc?wsdl

This will generate a configuration file and a code file that contains the client class. Add proxy code file to your plugin project and use the generated client class to call the Service.

Here CaseProxy is ServiceProxy class, ICase is Service interface.

        // send CaseId to other application
        private void SendCase(IOrganizationService service, Guid caseId)
            //Use same binding used in WCF service config
            var binding = new BasicHttpBinding();

            //Get Case service URL
            string endPointAddress = GetEndpointAddress(service);

            if (!string.IsNullOrEmpty(endPointAddress))
                //specify EndpointURL
                var endPoint = new EndpointAddress(endPointAddress);

                //ChannelFactory to specify binding and EndpointURL

                //Here CaseProxy is my ServiceProxy class, ICase is my service interface
                var channelFactory = new ChannelFactory<CaseProxy.ICase>(binding, endPoint);

                //create instance of CaseService
                CaseProxy.ICase caseClient = null;

                    //Create a channel
                    caseClient = channelFactory.CreateChannel();

                    //Call Service Method to send the case

                    //Close WCF service
                catch (Exception ex)
                    if (caseClient != null)
                    throw new Exception(string.Format("Error occured while sending case request to other application: {0}", ex.Message), ex);

        // Get Case service URL from Settings entity
        private string GetEndpointAddress(IOrganizationService service)
            string endPointAddress = string.Empty;

            string fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true' distinct='false'>
                                                    <entity name='ls_settings'>
                                                        <attribute name='ls_value' />
                                                        <filter type='and'>
                                                            <condition attribute='ls_column' operator='eq' value='CaseServiceURL'/>

            var req = new FetchExpression(fetchXML);

            EntityCollection results = service.RetrieveMultiple(req);

            if (results.Entities.Count > 0)
                endPointAddress = results.Entities[0].GetAttributeValue<string>("value");
            return endPointAddress;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s