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 !


Author: Nishant Rana

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

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

  1. 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

  2. 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

  3. 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

  4. 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

    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

  5. 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

  6. 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

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 )

Google+ photo

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

Connecting to %s