Showing posts with label WCF Client. Show all posts
Showing posts with label WCF Client. Show all posts

Tuesday, July 1, 2014

Step by step how to send a cross-domain Ajax request

        By Carmel Schvartzman


In this tutorial we'll learn how to send a cross-domain Ajax HTTP request using javascript jQuery methods. We'll use the $.ajax() method to render just a sample XML response , fixing the "No 'Access-Control-Allow-Origin' header is present on the requested resource" error.

Let's say you have a web page which sends HTTP GET or HTTP POST requests to a web service (WCF endpoint or OData service) to load the data :





Everything works fine , provided that BOTH the client and the web services are in the SAME DOMAIN. The problem will arouse while the client and the server are hosted in DIFFERENT DOMAINS :





We got an  "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, and there are "provisional" headers displayed.
Why? Because in the case of AJAX cross-domain requests, the browser sends some kind of "PRE-REQUEST" to  the web server, of the type of HTTP OPTIONS verb, informing that a CROSS-DOMAIN violation is being produced. Is then up to the web server to send or not a response to the web client.
That's because cross-domain HTTP requests started from scripts are subject to security restrictions. The 'CORS' (Cross Origin Resource Sharing) policy,  prevents access to methods and properties across DOMAINS, that means,  pages on different  web sites will not successfully retrieve data from some other domain, subdomain, or even internet protocol. For example, some resource loaded from  the domain http://AAAdomain.com)   makes a HTTP request for a resource on OTHER domain such as  http://BBBdomain.com . A web application can only make HTTP requests via AJAX to the domain to which it belongs, and not to other domains.
You can access to in-deep information about CORS policy in this web site.
To solve the  problem, we'll simply set the "Access-Control-Allow-Origin" header on our web server, setting it to "*" , in order to accept cross-domain AJAX HTTP requests from different domains.

Our Ajax request will be as follows:

 $.ajax(              {  

    crossDomain: true,

                url: url,
                type: "GET",
                contentType: "text/plain", 
                dataType: "xml",
                data: "",
                success: function (data)
                {                   
                    $(data).find('entry').each(function (index) {                         
                     
                        var id = $(this).find('BlogID').text();  
                        var title = $(this).find('Title').text();  
                        var text = $(this).find('Text').text();
                        var img = $(this).find('MainPicture').text();                        
                       
                    });                   
                                   },
                error: function (res) {
                    alert(res.status);
                }            });





And inside the <system.webServer> element at the web.config of the web server we'll set the headers to "*" :







<!--<httpProtocol>  --><!--THIS IS FOR CORS POLICY--><!--
      <customHeaders>

          <add name="Access-Control-Allow-Origin" value="*" />
          <add name="Access-Control-Allow-Headers" value="Content-Type" />
          <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
      </customHeaders>
  </httpProtocol>-->  </system.webServer>  



That's all we must get done, to enable cross-domain AJAX requests.

In this tutorial we've learned how to send a cross-domain Ajax request using javascript jQuery methods,  fixing the "No 'Access-Control-Allow-Origin' header is present on the requested resource" error.

That's all!! 
Happy programming.....

כתב: כרמל שוורצמן










Wednesday, June 4, 2014

Step By Step How to manually generate the Proxy classes for a WCF Web Service using the svcutil tool

         By Carmel Schvartzman

In this tutorial we'll learn How to manually generating the Proxy classes for a WCF Web Service using the svcutil.exe tool
When adding a WCF Service Reference to a .Net application , in order to call the WCF Web Service , we essentially need ONLY TWO FILES:
     1)   .cs file : containing all the classes and methods available through the WCF Service, which is called a  Proxy: this represents the WCF Web Service in the Client application. This Proxy allows us to use all methods and classes from the WCF Web Service as if it was a local entity inside your application, 
     2)  web.config file : containing the Web Service endpoint definitions to access to the WCF operations and data contracts.
We'll produce both files by running the svcutil.exe tool on the .wsdl address from the WCF Web Service. Then we'll copy the produced 2 files to our application that, in this example, will be an MVC Asp.Net application.
First we need the url of the Web Service wsdl file. In case you are developing it at your machine, you'll find it at the Properties tab, in the IIS - URL field:


Open .svc file at the url address :

Now open the .wsdl file:



Inside the .wsdl file you'll see the methods ("Operations") that the WCF Service supports:


Open the Command Prompt fro VS2012 : type "svcutil.exe" and the url of the .wsdl file, and also add two more arguments for the 2 files to be created by the tool:
1) /out:  the location and name of the .cs file containing the Proxy classes;
2) /config :  the location and name of the web.config file containing the WCF Endpoint:



The tool will attempt to download the metadata for the WCF Web Service:



After its done, the 2 files had been created :


Open the  .cs file containing the Proxy classes


As you can see, it contains all Operations and Data Contracts for the WCF Service.

The  web.config file contains the WCF Endpoint:



Copy the <system.serviceModel> tag with all its contents, and paste it inside the web.config file of your MVC Client application:



Now copy-paste the .cs file to the Model folder at your MVC Client application :

You'll see that, immediately and without compiling, Visual Studio will get the custom classes and methods of the WCF Service :

Compile and you can use the Proxy classes comfortably in your MVC Client :


If you set a breakpoint and debug the Client, you'll see the data returned by the WCF Service :




That's All !!!! 
In this tutorial we've learned how to manually generate the Proxy classes for a WCF Web Service using the svcutil.exe tool.  

Happy programming.....


כתב: כרמל שוורצמן

Friday, May 23, 2014

Step By Step How to solve the MVC error "Failed to generate code for the service reference" in Visual Studio 2012

         By Carmel Shvartzman

In this tutorial we'll learn How to take care of the MVC error "Failed to generate code for the service reference"  while generating the Proxy classes for a WCF Web Service in Visual Studio 2012
When adding a WCF Service Reference to an MVC application in Visual Studio 2012, there is a frequent issue that resolves in an error, causing the Visual Studio to fail in creating the Proxy classes that represent the WCF Web Service in the Client application. We'll learn how to deal with that error.

The error is produced after we add a service reference to our application, as follows:



The Web Service is found, and we add it to the app:


The error tells us that it was a failure while trying to generate the Proxy code for the Service :



A search at Microsoft Support explains that the bug was fixed in the new version of the .Net Framework 4.5:


The update fixes the case in which the code is not generated :


Indeed ' we can see after the project building, that the Proxy code is empty :


Also, the Web Config file has not been updated to reflect the new Web Service Client Endpoint :



To solve the problem without upgrading the Framework version with the new update available at the Support web site, we just open the Service reference Configuration :



In the Configuration dialog, look for the "Reuse types in referenced assemblies" checkbox :


And uncheck the "Reuse types in referenced assemblies" checkbox .
Build your project again.
Open the Reference.cs file to see the Proxy classes :



This time we got the classes and methods.
Open the web.config file:


As you can see , now the WCF endpoint is referenced.

Now you can use the Service Reference, and its methods :


The WCF Web Service is up and running, and you get the data :



So you can add a new View strongly-typed, using the Proxy classes :


Your View should appear as follows :


This is the composite type sent by the WCF Web Service to our MVC application.


That's All !!!! 
In this tutorial we've learned how to take care of the MVC error "Failed to generate code for the service reference" while generating the Proxy classes for a WCF Web Service  in Visual Studio 2012.  

Happy programming.....


עריכה:  כרמל שוורצמן