Jul 2, 2012

Calling WCF service within a plug-in

Calling a third party service through plug-in could be an important aspect of a CRM development. I of course had a difficult time to figure out this. At the end of the day below sequence worked for me.

1)  Adding the service to Plug-in code
This step was as same as calling a service from general web application. Add the service URL and you will see it added to the Service Reference section.


Nor difference in calling part also. Something like below...

_service = new Rate();
_service.ChannelFactory.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
 
_service.ChannelFactory.Credentials.Windows.ClientCredential.Domain = "CRM";
_service.ChannelFactory.Credentials.Windows.ClientCredential.UserName = "sumedha.dev1";
_service.ChannelFactory.Credentials.Windows.ClientCredential.Password = "fex@ADm1n";

selltingRate = _service.ReturnSelltingRate(_prodId);

2) Defining the end points

Here is the tricky part. Now you got to define the endpoint of the service in the config of the CRM. You will find web.config of the CRM in C:\Program Files\Microsoft Dynamics CRM\CRMWeb. (Please make sure you get back up before modifying this file, since if it’s modified wrong, CRM will not load at all!)

Now go back to the Visual Studio project and open the app.config file of the same plug-in project. Now grab the endpoint of your service and relevant binding tag. You can select the right endpoint by searching by URL of the service. Then get the “name” attribute of particular tag and search for the Binding tag of same name.

Now paste them in the web.config file of the CRM. Under <system.serviceModel> tag you will see two sections called “client” and “bindings” Endpoint should go under client section. Binding tag should go under bindings section, within the tag either <basicHttpBinding> or <wsHttpBinding>, depending on binding type. Identifying the binding type is not hard since name attribute of binding tag contains the type.

You will find something like this in the web.config of the CRM, if you do it correctly.

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IRateService" 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="Transport">
        <transport clientCredentialType="Windows" proxyCredentialType="None"
            realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="https://xxxxxxxxxxxxxxxxxx/webservices/xxxxxxxxxxxxxxx/Rate.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRateService"
            contract="BusinessServiceClient.IRateService" name="BasicHttpBinding_IRateService" />
</client>
</system.serviceModel>  

Click this to read the previous articles explained how to call a WCF from a client side. Please note some services will work fine for plug-ins but not client side due to security reasons. Only solution for that is to create your own WCF service to consume the third party service and then use your service to read values to CRM forms.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi, thanks for the above article.

    Is it possible to write a WCF service to access CRM 4.0 from asp.net page using Jquery?

    Thanks

    Bala

    ReplyDelete
    Replies
    1. Hi Bala,

      I haven’t tried calling CRM service straight from client scripts. I guess there could be security restrictions. As you suggest, you can write your own WCF service to access methods of the CRM service. You better authentic this way.

      http://www.sumedha8.blogspot.com.au/2012/08/using-crm-webservice-from-custom-web.html

      Now call your own WCF from client script. I haven’t tried Jquery, but javascript this way


      http://www.sumedha8.blogspot.com.au/2012/02/consuming-wcf-from-javascript.html

      Hope this will make some sense.

      Delete