No transport error while consuming WCF service in a REST way

Discussion No transport error while consuming WCF service in a REST way

  • Monday, September 10, 2012 9:47 AM
     
     

    Hi,

    Here is a small article on when we usually face No Transport error and how to get rid of it.

    I recently worked on a sample application using “app for Office” (New in Visual Studio 11.0)

    What is “app for Office”?

    An app for Office is basically a webpage that is hosted inside an Office client application. You can use apps to extend the functionality of a document, email message, meeting request, or appointment. Apps can run in multiple environments and clients, including rich Office desktop clients, Office Web Apps, mobile browsers, and also on-premises and in the cloud.

    What can an app for Office do?

    An app for Office can do pretty much anything a webpage can do inside the browser, such as the following:

    • Provide an interactive UI and custom logic through      JavaScript. (Develop UI using HTML and JavaScript)
    • Use JavaScript frameworks such as jQuery.
    • Connect to REST endpoints and web services via      HTTP and AJAX.
    • Run server-side code or logic, if the page is      implemented using a server-side scripting language such as ASP or PHP.

    As “app for Office” doesn’t have an option to write server side scripting (unlike code behind or in-line coding feature in ASP.NET/MVC) we will have to go for web service or WCF service and consume the service using any JavaScript framework like Jquery.

    I wrote a simple WCF service to hook up with server side code and consumed the service using Jquery as follows

    $(document).ready(function () {
    $.ajax({
    type: "GET",
    url: "
    http://localhost/MyService.svc/rh/data?id=" + $('#sampleType').val(),
    processData: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    crossDomain: true,
    success: function (data) {

    alert(data)
    }
    },
    error: function (xhr, status, error) {
    alert(error);

    }
    });
    });

    I encountered an error saying “No Transport” when I executed the client application.  I did some investigation on this and found out the root cause that cross-domain request was disabled. But I was really not sure whether it was at my WCF service end or “app for Office” client end. I added Client Access policy and Cross-domain-policy xml files to WCF service in order to enable cross-domain request  so that service will accept any type requests sent form different domains.

    Client Access policy

    <?xml version="1.0" encoding="utf-8" ?>

    <access-policy>

      <cross-domain-access>

        <policy>

          <allow-from http-request-headers="*">

            <domain uri="*"/>

          </allow-from>

          <grant-to>

            <resource include-subpaths="true" path="/"/>

          </grant-to>

        </policy>

      </cross-domain-access>

    </access-policy>

    Cross-domain-policy

    <?xml version="1.0"?>

    <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">

    <cross-domain-policy>

                <allow-http-request-headers-from domain="*" headers="*"/>

    </cross-domain-policy>

    Both the policies should be in different XML files

    But this didn’t solve my problem. After a little bit more investigation I found exactly where and how to enable-cross domain requests.

    Solution

    We need to enable cross-domain requests in environments that do not support cross-domain requests.

    Cors is equal to true if a browser can create an XMLHttpRequest object and if thatXMLHttpRequest object has a withCredentials property. To enable cross-domain requests in environments that do not support cors yet but do allow cross-domain XHRrequests (windows gadget, etc), set $.support.cors = true;”

    You just have to add jQuery.support.cors = true; in your client scripting and it works perfectly fine.

    [Cors- Cross-Origin Resource Sharing]

    A simple example:

    $.support.cors = true;
    $(document).ready(function () {
    $.ajax({
    type: "GET",
    url: "
    http://localhost/MyService.svc/rh/data?id=" + $('#sampleType').val(),
    processData: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    crossDomain: true,
    success: function (data) {

    alert(data)
    }
    },
    error: function (xhr, status, error) {
    alert(error);

    }
    });
    });

    Hope this will be helpful.

    Thanks


All Replies

  • Monday, September 10, 2012 1:10 PM
     
     
    This is fantastic, u resolved!!!

    Software Developer

  • Wednesday, September 12, 2012 7:24 AM
     
     

    good one to save hell lot of time for any developer.....

    Please keep posting such issue to the world.