Tuesday, 18 June 2013

Consuming a WCF from JavaScript

This is just to give simple steps to call a WCF from client side.

1) Browse the web service URL. Then you know your web service is available. You will see a page like below one.

2) Now click relevant link in the page. Search for SOAPAction tag relevant to method you wish to use, So that you know the SOAPAction value.

3) Now get the request SOAP envelops.

a) If you have the code of WCF service run the code in Visial Studio that results a pop up of WCF Test Client. Then double click the method you need and pass some values to parameters shown in Formatted tab. Then click the XML tab to grab the Request SOAP envelop. Just omit the Header Tag from the XML you get here.

b) If the web service is provided by some other party, you can simply request the soap envelops.

4) Now we got everything we need, call the relevant method of the Web Service through below code. xmlHttp.ResponseXML will return the result.
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', 'http://localhost:5555/ISV/PricingWCF/WcfIntermediateService.Service1.svc', false);
        xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
        xmlhttp.setRequestHeader('SOAPAction', 'http://tempuri.org/IService1/GetSalesRate');

        var data = '';
        data += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
        data += '<s:Body>';
        data += '<GetSalesRate xmlns="http://tempuri.org/">';
        data += '<productcode>AEC0023678</productcode>';
        data += '</GetSalesRate>';
        data += '</s:Body>';
        data += '</s:Envelope>';

        xmlhttp.send(data);


How to call External WCF Service within CRM 2011 Plugins

Step 1: Build and Publish the WCF Service

Step 2: Deploy the WCF Service


Step 3: Create WCF client


Create a WCF client using  ServiceModel Metadata Utility Tool (Svcutil.exe) by using following steps:


On the Start menu click All Programs, and then click Visual Studio 2010. Click Visual Studio Tools and then click Visual Studio 2010 Command Prompt.


Navigate to the directory where you want to place the client code.


Use the command-line tool ServiceModel Metadata Utility Tool (Svcutil.exe) with the appropriate switches to create the client code. The following example generates a code file and a configuration file for the service.


svcutil.exe /language:cs /out:TaxServiceClient.cs http://192.168.124.26:81/TaxProductionService


Step 4: Write Plugin to consume external WCF Service


Add the above generated .CS class into the plugin solution. In Visual Studio, right-click the client project in Solution Explorer and select Add and then Existing Item. Select the TaxServiceClient.cs file generated in the preceding step.

Open the plugin.cs class from the solution. And call the WCF client using channel factory.

#region Calling WCF Service


BasicHttpBinding myBinding = new BasicHttpBinding();myBinding.Name = “BasicHttpBinding_IPOCService”;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://192.168.124.26:81/TaxProductionService.svc“);ChannelFactory<IPOCService> factory = new ChannelFactory<IPOCService>(myBinding, endPointAddress);IPOCService channel = factory.CreateChannel();taxAmount = channel.GetCalculatedTaxValue(unitPrice);factory.Close();


#endregion


Step 5: Register the Plugin using registration tool.


You can also download a sample code from below link:

http://code.msdn.microsoft.com/How-to-call-External-WCF-42c4490d

Friday, 3 May 2013


Understanding Plug-ins in MS CRM 2011

A plug-in is custom business logic (code) that you can integrate with Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online to modify or augment the standard behavior of the platform. Another way to think about plug-ins is that they are handlers for events fired by the Microsoft Dynamics CRM platform. You can subscribe, also known as registering, a plug-in to a known set of events to have your code run when the event occurs.

Plug-in will be part of Transactions and will trace the exception during execution.


• PreEvent  : Stage 10: Pre-validation

• PreEvent  : Stage 20: Pre-operation

• Core Operation : Stage 30: MainOperation

• PostEvent   : Stage 40: Post-operation

• PostEvent   : Stage 50: Post-operation (deprecated*)

Stage 20,30,40 are Transaction Scope.


Sync/Async Execution

Plugins can execute in
- Synchronously during pipeline execution

- Asynchronously – queued for later execution


Transaction Support

CRM 2011 Stages support plugin execution inside the database transaction
- Stages 20 & 40

Uncaught exceptions force a rollback
IExecutionContext.IsInTransaction
Transaction spans CRM DB operations only
- No distributed transaction support

Plugins registered outside transaction stage will participate in transaction if executed as nested pipeline of transactional parent.