Call WCF Service from Dynamics CRM using AJAX

A couple of days back, I had one of my ex-colleagues call me regarding a problem he had been facing when making jQuery AJAX calls to JSON WCF Services from Dynamics CRM. I had encountered this same problem some years back, when I had just started my career as a CRM Developer. And since I toiled hard to find a resolution for that back then, it’s something that remains quite vibrant in my brain. I told him the solution only to get an appreciation the next day that it had worked for him. I was glad to have helped someone fix this without having to scour through the internet for solutions; and hence decided to put it up in my blog when a different friend of mine called me a few days back again with the same problem (except this time there was no Dynamics CRM involved). Now, to the problem. The problem was while making jQuery Ajax calls to a custom RESTful (JSON) WCF Service from Dynamics CRM, a network related error popped up. Even setting “cors: true” in the client jQuery code didn’t help. In case you’re wondering what “cors” refers to, it means Cross Origin Resource Sharing. By enabling it, you are allowing cross-domain communication from the browser. Anyways, since that didn’t help, this is where the following solution comes into play. The trick here is to make some changes in the server side, that is, in the WCF Service code to make the request happen successfully. The changes – that’s exactly what we’re going to see in this article. But instead of going straight to the solution, let’s try to reproduce the issue and then apply the fix.

Call WCF Service from Dynamics CRM – The Problem

Let’s start with a very basic JSON WCF Service. Just one simple method, that takes in a single argument and prepends a fixed string “MyWCFResponse” to it. This is just for demonstration, so to keep it simple it’s GET, so the parameter is passed in the URL itself.

Call WCF Service from Dynamics CRM 2013 using AJAX – Image 01

The contents of “IService.cs” being as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RestWcfService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(UriTemplate = "getdata/{value}", Method = "GET", ResponseFormat =WebMessageFormat.Json)]
Response GetData(string value);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
 
namespace RestWcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "getdata/{value}", Method = "GET", ResponseFormat =WebMessageFormat.Json)]
        Response GetData(string value);
    }
}

The implemented class “Service.svc.cs” has the following contents:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RestWcfService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service : IService
{
public Response GetData(string value)
{
return new Response()
{
Data = "MyWCFResponse" + value,
Status = "OK"
};
}
}

public class Response
{
public string Data { get; set; }
public string Status { get; set; }
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
 
namespace RestWcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service : IService
    {
        public Response GetData(string value)
        {
            return new Response()
            {
                Data = "MyWCFResponse" + value,
                Status = "OK"
            };
        }
    }
 
    public class Response
    {
        public string Data { get; set; }
        public string Status { get; set; }
    }
}

And a simple “web.config” to enable JSON behavior:

<?xml version="1.0"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RestWcfService.Service">
<endpoint address="" binding="webHttpBinding" contract="RestWcfService.IService"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>

</configuration>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0"?>
<configuration>
 
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="RestWcfService.Service">
        <endpoint address="" binding="webHttpBinding"  contract="RestWcfService.IService"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
 
</configuration>

Now, after you have built the solution and deployed it in IIS, we are going to try and invoke it from Dynamics CRM using a jQuery Ajax call. One thing to be noted here is that, the Dynamics CRM is installed in a different server than the one where we have hosted the WCF Service. Although, some development environments follow everything-inside-a-single-box nomenclature, when it comes to production environments, Dynamics CRM servers are always maintained separately from additional custom components. So, we are going to simulate a production environment nomenclature here.

Call WCF Service from Dynamics CRM 2013 using AJAX – Image 02

We are going to be using the following piece of code On Load of Contact form in Dynamics CRM to fill up a particular field with the value returned from the WCF Service.

function ContactOnLoad()
{
var fname = Xrm.Page.getAttribute("firstname").getValue();

if(fname != null || fname != 'undefined' || fname != "")
{
jQuery.support.cors = true;
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: 'http://192.168.164.139/RestWcfService/Service.svc/getdata/' + fname,
processData: false,
dataType: "json",
success: function (response) {
if(response != null || response != 'undefined')
{
var respData = response.Data;
Xrm.Page.getAttribute("lastname").setValue(respData);
}
},
error: function (a, b, c) {
alert(a.responseText);
}
});
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function ContactOnLoad()
{
var fname = Xrm.Page.getAttribute("firstname").getValue();
 
if(fname != null || fname != 'undefined' || fname != "")
{
                jQuery.support.cors = true;
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: 'http://192.168.164.139/RestWcfService/Service.svc/getdata/' + fname,
processData: false,
dataType: "json",
success: function (response) {
if(response != null || response != 'undefined')
{
var respData = response.Data;
Xrm.Page.getAttribute("lastname").setValue(respData);
}
},
error: function (a, b, c) {
alert(a.responseText);
}
});
}
}

For demo purposes, we are simply fetching the First Name of the Contact record, passing it to the WCF Service and updating the Last Name of the Contact with the response received from the Service. The code looks okay, except when you run it, it encounters an error.

Call WCF Service from Dynamics CRM 2013 using AJAX – Image 03

Debugging the code shows that it’s going directly to the error callback, contrary to what we had expected.

Call WCF Service from Dynamics CRM 2013 using AJAX – Image 04

You might be clueless about what’s going on. This where F12 (IE developer tools) comes to aid. Popping it up by pressing F12 shows the actual cause behind the problem – “Network Error, Access is denied”.

Call WCF Service from Dynamics CRM 2013 using AJAX – Image 05

Call WCF Service from Dynamics CRM – The Solution

Now that we have been reproduce the issue, let’s go ahead and fix it. Open up your WCF Service code in Visual Studio. Add the “Global.asax” file to your project by right clicking on the Project > Add > New Item > Global Application Class.

Call WCF Service from Dynamics CRM 2013 using AJAX – Image 06

Call WCF Service from Dynamics CRM 2013 using AJAX – Image 07

Open up the “Global.asax” file and add the following piece of code:

protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
 
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}

Save the file, recompile your Service code and re-deploy it to IIS. That’s it! Now head back to Dynamics CRM and try opening up a Contact record, you should be able to see the effect of the code!

Call WCF Service from Dynamics CRM 2013 using AJAX – Image 08

See how the last name is replaced with (“MyWCFResponse” + First Name) which was returned by the WCF Service. Opening up the Developer Tools (F12) shows that there are no network related errors as were shown earlier.

Call WCF Service from Dynamics CRM 2013 using AJAX – Image 09

Well, that’s it. If you’re reading it right now, I hope this saved some of your time and helped you! =) And this is not something specific for Dynamics CRM, it’s something that works for any jQuery ajax calls.

MSCRM 通过Ajax调用WCF服务的更多相关文章

  1. ajax内调用WCF服务

    WCF可以当作WebService一样被调用,在html内通过ajax调用WCF服务的方法如下: 1.新建一个WCF服务的网站项目: 2.在项目内增加一个新项:启用了ajax的WCF服务: 3.在对应 ...

  2. WCF入门教程(四)通过Host代码方式来承载服务 一个WCF使用TCP协议进行通协的例子 jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding System.ServiceModel.WSHttpBinding协议 学习WCF笔记之二 无废话WCF入门教程一[什么是WCF]

    WCF入门教程(四)通过Host代码方式来承载服务 Posted on 2014-05-15 13:03 停留的风 阅读(7681) 评论(0) 编辑 收藏 WCF入门教程(四)通过Host代码方式来 ...

  3. jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding

    Jquery ajax调用WCF服务 例子效果如下:原界面 点击按钮GetList get后,通过指定的Url获取数据添加到table 新建一个控制台项目,添加IContract.cs,DBServi ...

  4. 【原创经验分享】JQuery(Ajax)调用WCF服务

    最近在学习这个WCF,由于刚开始学 不久,发现网上的一些WCF教程都比较简单,感觉功能跟WebService没什么特别大的区别,但是看网上的介绍,就说WCF比WebService牛逼多少多少,反正我刚 ...

  5. 实现jquery.ajax及原生的XMLHttpRequest跨域调用WCF服务的方法

    关于ajax跨域调用WCF服务的方法很多,经过我反复的代码测试,认为如下方法是最为简便的,当然也不能说别人的方法是错误的,下面就来上代码,WCF服务定义还是延用上次的,如: namespace Wcf ...

  6. 实现jquery.ajax及原生的XMLHttpRequest调用WCF服务的方法

    废话不多说,直接讲解实现步骤 一.首先我们需定义支持WEB HTTP方法调用的WCF服务契约及实现服务契约类(重点关注各attribute),代码如下: //IAddService.cs namesp ...

  7. 实现在GET请求下调用WCF服务时传递对象(复合类型)参数

    WCF实现RESETFUL架构很容易,说白了,就是使WCF能够响应HTTP请求并返回所需的资源,如果有人不知道如何实现WCF支持HTTP请求的,可参见我之前的文章<实现jquery.ajax及原 ...

  8. [转]学习 WCF (6)--学习调用WCF服务的各种方法

    转自:http://www.cnblogs.com/gaoweipeng/archive/2009/07/26/1528263.html 作者这篇博文写得很全面. 根据不同的情况,我们可以用不同的方法 ...

  9. VS2010中使用Jquery调用Wcf服务读取数据库记录

    VS2010中使用Jquery调用Wcf服务读取数据库记录 开发环境:Window Servere 2008 +SQL SERVE 2008 R2+ IIS7 +VS2010+Jquery1.3.2 ...

随机推荐

  1. 如何用C语言封装 C++的类,在 C里面使用

    本文给出了一种方法.基本思想是,写一个 wrapper文件,把 C++类封装起来,对外只提供C语言的接口,和 C++i相关的都在  wrapper的实现文件里实现. 1. apple.h #ifnde ...

  2. AIX上面Oracle数据库相关启动

    1,启动停止Oracle实例 (1) su -oracle (2) echo $ORACLE_SID (3) sqlplus /nolog //以不登录到数据库的方式进入sqlplus环境 (4) c ...

  3. Android笔记:Fragment与ViewPager组合时,如何在FragmentActicity获取Fragment对象

    项目中有一处需要监听宿主的控件,再去更新Fragment页面,由此必须去获取Fragment里的对象; 在FragmentActivity + Fragment组合里是可以使用以下方案: MedicF ...

  4. Java中的==、equals、hasCode方法

    == java 的数据类型分为“基本数据类型” 和“引用数据类型”在基本数据类型的比较中,== 比的就是基本数据类型变量中所保存的值在引用数据类型的比较中,== 才比较的是变量所指向的对象的地址. e ...

  5. [个人翻译]GitHub指导文件(GitHub Guides[Hello World])

    [个人翻译]GitHub指导文件(GitHub Guides[Hello World]) Mirage_j个人翻译,欢迎转载,最好标明出处http://www.cnblogs.com/mirageJ/ ...

  6. BOM数据基础 - Mobox物料编码管理及实现

    1 企业现状 在企业日常经营过程中会产生大量的文档,如设计图纸.变更单.计算书.设计方案等,如果是制造企业还会产生大量的产品.组成产品的零部件等物料,这些数据在进入信息系统前都需要有一个唯一的标识,也 ...

  7. asp.net权限认证:OWIN实现OAuth 2.0 之授权码模式(Authorization Code)

    asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...

  8. ArcGIS制图表达Representation-符号制作

    ArcGIS制图表达Representation-符号制作 by 李远祥 在ArcGIS的符号里面,存在着两种符号体系,一种是传统的标准符号体系,一种是制图表达符号体系.标准符号几乎被绝大部分ArcG ...

  9. 微端启动器LAUNCHER的制作之MFC版二(下载)

    用了C#再回来用C++写真的有一种我已经不属于这个世界的感觉.C++的下载就没有C#那么方便了,我用的是libcurl.dll,官网上下载的源码自己cmake出来编译的,c++的库引用有debug和r ...

  10. css3绘制腾讯logo

    CSS3绘制的腾讯LOGO,下边是对比图. 演示地址