Calling WCF Service in Plugin in CRM


Suppose this is our simple WCF Service.

[ServiceContract]
   public interface IService1
   {
       [OperationContract]
       string GetData(); 
     
   }

public class Service1 : IService1
   {
       public string GetData()
       {
           return “Hello World”+DateTime.Now.ToLongTimeString();
       }
     
   }

Now if we add its service reference in our plugin and then deploy it, while running we would receive this error.

Could not find default endpoint element that references contract ‘ServiceReference1.IService1’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

The reason for this is because the configuration information for the WCF service from the client side is missing. As class library won’t have their own config file.

Suppose if we add service reference to the above Service in a windows application, we can find the following information being added to the app.config file

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
    <!–<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name=”BasicHttpBinding_IService1″ closeTimeout=”00:01:00″
                    openTimeout=”00:01:00″ receiveTimeout=”00:10:00″ sendTimeout=”00:01:00″
                    allowCookies=”false” bypassProxyOnLocal=”false” hostNameComparisonMode=”StrongWildcard”
                    maxBufferSize=”65536″ maxBufferPoolSize=”524288″ maxReceivedMessageSize=”65536″
                    messageEncoding=”Text” textEncoding=”utf-8″ transferMode=”Buffered”
                    useDefaultWebProxy=”true”>
                    <readerQuotas maxDepth=”32″ maxStringContentLength=”8192″ maxArrayLength=”16384″
                        maxBytesPerRead=”4096″ maxNameTableCharCount=”16384″ />
                    <security mode=”None”>
                        <transport clientCredentialType=”None” proxyCredentialType=”None”
                            realm=”” />
                        <message clientCredentialType=”UserName” algorithmSuite=”Default” />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address=”http://localhost:58844/Service1.svc” binding=”basicHttpBinding”
                bindingConfiguration=”BasicHttpBinding_IService1″ contract=”ServiceReference1.IService1″
                name=”BasicHttpBinding_IService1″ />
        </client>
    </system.serviceModel>–>
</configuration>

So now in case of our plugin we need to define the binding and endpoint information programmatically, something like this

try

{

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://localhost:58844/Service1.svc&#8221;);

ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client(myBinding, endPointAddress);

MessageBox.Show(myClient.GetData());

}

catch (Exception EX)

{

 

throw EX;

}

      
 

This way we would be able to access our WCF service inside plugin.

Hope it helps !


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

21 thoughts on “Calling WCF Service in Plugin in CRM”

  1. Hello, I´m trying to apply this code over the online version of CRM 2011. The only way to publish a plugin in an online version is in a “sandbox” mode. Could you tell me is this solution will work fine?
    Thanks

    Like

  2. Hello,

    I have created WCF service which internally update the my SQL table and I using one of the WCF method in my CRM 2011 online plug-in but plug-in throws exception. I have used same way to call the WCF method as mentioned in the post.

    Is WCF service is allowed in CRM 2011 online plug-in or not?

    Thanks in Advance.

    Like

    1. Hi , I am able to call the WCF if the plugin registered in Post , Async operation . But I need to call the WCF in Synchronous mode . When i tried this am getting “Generic SQL Error”.
      Please help me to sort out this

      Like

  3. Hi ,
    Your post saved my time a lot.

    I am calling a AX WCF web service in my Plugin and so i am using your way.

    But it gives me error.
    When i debugged i got “null” in UserName.

    What can i do in this ?
    Is anything i am missing here.

    Like

  4. Hi Nishant,

    I have created a service with a method that returns the string “Testing”. The WCF service is hosted in Windows Azure and I have created a Console Application which can call the method successfully. However, when consumed from within a plug-in, I receive the following error:

    System.Runtime.Serialization.InvalidDataContractException: Type ‘System.Threading.Tasks.Task’1 [System.String] cannot be serialized. Consider marking it with the DataContractAttribute attribute and marking all of its members you want serialized with the DataMemberAttribute attribute…

    I am testing from an on-premise implementation of CRM, but this will also need to work for CRM 2011 online. Any ideas of how I can fix this?

    Like

  5. You’ve made some decent points there. I checked on the net for more information about the issue and found most people will go along with your views on this site.

    Like

  6. Unfortunately, different people produce this hormone at different rates, which is why
    some people have trouble generating lean muscle growth. ‘We want to carefully research the potential benefits of velvet antler supplements for supporting the immune system, anti-aging, muscle strength and endurance, and sexual vitality. Fillers, binders, lubricants, and the like do not have to be listed and often aren’t.

    Like

  7. Hi Nishanth ,

    I am using the same format only but still I am getting the same error. May I know why.

    What is in the url “&#8221” argument ? http://localhost:58844/Service1.svc&#8221;

    Actually I need to call a Rest API from my plugin (Using RestSharp) but I am getting error so I thought at least I can create SOAP service from plugin and call the rest api from that.

    Liked by 1 person

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.