Tuesday, October 28, 2014

How to make wcf service and how to call by ajax?

step 1: first I make the interface...

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

using System.ServiceModel.Web;

[ServiceContract]

public interface IService

{

[OperationContract]

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]

string GetCustomers(string prefix);

[OperationContract]

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]

string GetCustomers1();

[OperationContract]

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]

string GetData();

}

step 2: second Make the Service...

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

using System.Data.SqlClient;

using System.Configuration;

using System.Web.Script.Serialization;

using System.ServiceModel.Activation;

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class Service : IService

{

public string GetCustomers(string prefix)

{
List<object> customers = new List<object>();



using (SqlConnection conn = new SqlConnection())

{

conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

using (SqlCommand cmd = new SqlCommand())

{

cmd.CommandText = "select FirstName+ ''+ CASE when LastName IS null then '' end as ContactName,CustomerId from tblcustomer Where " +

" email like @prefix + '%'";

cmd.Parameters.AddWithValue("@prefix", prefix);

cmd.Connection = conn;

conn.Open();

using (SqlDataReader sdr = cmd.ExecuteReader())

{

while (sdr.Read())

{

customers.Add(new

{

Id = sdr["CustomerId"],

Name = sdr["ContactName"]

});

}

}

conn.Close();

}

return (new JavaScriptSerializer().Serialize(customers));

}

}

public string GetData()

{

return "pppp";

}

public string GetCustomers1()

{
List<object> customers = new List<object>();



using (SqlConnection conn = new SqlConnection())

{

string prefix = "g";

conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

using (SqlCommand cmd = new SqlCommand())

{

cmd.CommandText = "select FirstName+ ''+ CASE when LastName IS null then '' end as ContactName,CustomerId from tblcustomer Where " +

" email like @prefix + '%'";

cmd.Parameters.AddWithValue("@prefix", prefix);

cmd.Connection = conn;

conn.Open();

using (SqlDataReader sdr = cmd.ExecuteReader())

{

while (sdr.Read())

{

customers.Add(new

{

Id = sdr["CustomerId"],

Name = sdr["ContactName"]

});

}

}

conn.Close();

}

return (new JavaScriptSerializer().Serialize(customers));

}

}

}

step 3: change web.config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior1">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior1">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="ServiceBehavior1" name="Service">
<endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="ServiceAspNetAjaxBehavior1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>

step 4:
Note: make the service and then deployment in iis and get url like http://localhost/webs
 

step 5: cal any where by ajax....



function LoadStatixText() {

var sId = 0;

$.ajax({

type: "GET",

contentType: "application/json; charset=utf-8",

url: "http://localhost/webs/services/service.svc/GetCustomers1",

data: '{}',

processData: false,

success: function (response){alert(response.d);},

failure: function (response) {

alert(2);

},

error: function (response) {

alert(response.d)

}

});

}

 


No comments:

Post a Comment