Calling WCF Service in Plugin in CRM

Posted: November 20, 2010 in CRM, CRM 2011, Microsoft Dynamics 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 !

Comments
  1. Ben says:

    Thank you, this was exactly what I needed. It didn’t even come to mind that libraries do not have config files :/

  2. [...] Calling WCF Service in Plugin in CRM [...]

  3. traffikbuster.com says:

    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.

  4. Roshan says:

    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?

  5. Milan Hingu says:

    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.

  6. Anonymous says:

    Thanks mate, very useful!!

    • Eswar says:

      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

  7. Rajesh Thakur says:

    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.

  8. Anonymous says:

    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

  9. sander says:

    Thanks for the post, man! Helped me a great deal.

  10. Shai says:

    You loosing the flexibility of changing the settings on the config file, by hard coding everything into the code.
    See this blog for more elegant solution.

    http://weblogs.asp.net/pabloretyk/archive/2010/01/17/override-wcf-client-settings-for-custom-config-file-using-duplexchannel.aspx

Share your thoughts

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Connecting to %s