Monday, July 7, 2014

Cross domain call for WCF service from jquery ajax

A very good read for cross domain call in HTML5. Jquery can be used to make the CORS request. Configuration of both the client and  the server is needed for making the CORS request. Below is a good article on this :-
http://www.html5rocks.com/en/tutorials/cors/#toc-introduction

Below is link to one more blog I came across on this subject.
http://weblogs.asp.net/stevewellens/calling-wcf-services-with-jquery-stripped-down

It can be followed easily for any POC. It basically involves calling a WCF service from jquery ajax.

Below is the code i used for WCF Service :-

The IService1.cs file stripped of the contract attribute, which have been moved to the class file itself.


    public interface IService1
    {

        string GetNames(string p);
        
    }
The Service.cs has been redefined with contract attributes :-

  
[ServiceContract]
    public class Service1 : IService1
    {


        [OperationContract]
        [WebInvoke(Method = "POST",
                   BodyStyle = WebMessageBodyStyle.WrappedRequest,
                   ResponseFormat = WebMessageFormat.Json)]
        public string GetNames(string p)
        {
            Dictionary names = new Dictionary();

            names.Add(1, "Keerthi");
            names.Add(2, "Keeith");
            names.Add(3, "Kay");
            names.Add(4, "Keny");
            names.Add(5, "Ness");
            names.Add(6, "May");
            names.Add(7, "Ferro");
            string json = JsonConvert.SerializeObject(names.Where(x => x.Value.StartsWith(p, true, CultureInfo.CurrentCulture)).ToDictionary(x => x.Key, x => x.Value), Formatting.None);
            return json;

        }
    }

The WCF configuration file looks like below :-
 
 



  
    
    
    
  

  
   
    
      
        
          
          
          
          
        
      
    
    
    
      
        
      
    
  
  
    

  


The WCF Service is hosted on port as shown in the url attribute of the ajax function.GetNames is the method name in the wcf service that gets called.
The AspWebAjax.aspx page is as below :-
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AspWebAjax.aspx.cs" Inherits="TestWebApp.AspWebAjax" %>





    Ajax Web Service Call

     
    
    
    
    
    
    


    
Name:
Chosen Value:

Screenshot of the autocomplete feature achieved :-


Tuesday, July 1, 2014

Jquery Ajax autocomplete for ASP.NET

In this example I show you can create an autocomplete in  jquery which makes ajax call  to code behind web method.

I use couple of things:

1. JSON.net
2. jquery-ui
3. json.js

Here is the AspAjaxDev.aspx page used :



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AspAjaxDev.aspx.cs" Inherits="TestWebApp.AspAjaxDev" %>






    Ajax works
    
    
    
    
    
    

    



    
Name:
Chosen Value:


The codebehind web method to which calls were made:-


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetNames(string p)
        {
            Dictionary names = new Dictionary();

            names.Add(1, "Keerthi");
            names.Add(2, "Keeith");
            names.Add(3, "Kay");
            names.Add(4, "Keny");
            names.Add(5, "Ness");
            names.Add(6, "May");
            names.Add(7, "Ferro");
            string json = JsonConvert.SerializeObject(names.Where(x => x.Value.StartsWith(p, true, CultureInfo.CurrentCulture)).ToDictionary(x -> x.Key, x -> x.Value), Formatting.None);
            return json;
}
       

Tuesday, June 24, 2014

Deploying ASP.NET site in IIS

Deploying your web application into  IIS is one of the major tasks for  a developer before moving into UAT or testin. I found the following link useful

http://www.codeproject.com/Articles/28693/Deploying-ASP-NET-Websites-on-IIS

It has  a lot of screenshot to help your to easily understand the process. It talk of deploying asp.net site into IIS 7.0 server but also applicable to later versions of IIS servers.

For creating new website in IIS 7.5 below is the link :-
http://www.dotnetfunda.com/articles/show/1247/how-to-host-aspnet-application-on-the-web-server-iis

Friday, June 13, 2014

Reflection and Attributes

Attributes describe the behavior of .net objects like class, method, function, interface etc to the runtime.
There predefined attributes and custom attributes that can be defined by user. The custom attributes are used to store the information.

Here is link on simple use of custom attributes :-
http://www.tutorialspoint.com/csharp/csharp_attributes.htm

Reflection is a set of classes that one uses to explore the .net objects during the runtime itself.

Below link gets the example of how reflection can be used to explore on custom attributes :-
http://www.tutorialspoint.com/csharp/csharp_attributes.htm

Thursday, June 12, 2014

Working with Generics

Generics in C# help you to create light weight class that can take any type of parameter as input. This sort of helps in maximum code reuse.
I came across an excellent explanation on Generics and its uses on MSDN.

Below is the link :-
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx

Here is a brief tutorial on using generics :-
http://www.tutorialspoint.com/csharp/csharp_generics.htm