Hi Folks!,
I was facing a problem while invoking DELETE method in WCF. Here is how i managed to do it.
Step 1. Create a WCF Service.
Create a new WCF service and name it as MyService.svc
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace WCFASPNET
{
public class MyService : MyServiceInterface
{
public string GetAllUsers()
{
//return all users
}
public string DeleteDept(String ID)
{
//delete department
}
}
Step 2. Add operation contracts in the WCF Service Interface file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WCFASPNET
{
[ServiceContract]
public interface MyServiceInterface
{
[OperationContract]
[WebGet(UriTemplate = "/MyUsersList", ResponseFormat = WebMessageFormat.Json)]
string GetAllUsers();
[OperationContract]
[WebInvoke(Method = "DELETE", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/DeleteDept/{ID}")]
string DeleteDept(String ID);
}
}
Step 3. Add Cross Domain Ajax Code in Global.asax file.
Add the Application_BeginRequest event in Global.asax file
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("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
Step 4. Configure web.config file.
<system.serviceModel>
<services>
<service name="WCFASPNET.MyService" behaviorConfiguration="RESTBehavior">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="ServiceBinding" behaviorConfiguration="MyEndpointBehavior" contract="WCFASPNET.MyServiceInterface"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding crossDomainScriptAccessEnabled="true" name="ServiceBinding">
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="RESTBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
<endpointBehaviors>
<behavior name="MyEndpointBehavior">
<webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="false" helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Step 5. Call WCF Delete from jquery
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="js/jquery-1.4.1.js"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
//getuser();
deleteuser();
function deleteuser() {
$.ajax({
type: "DELETE",
url: "MyService.svc/DeleteDept/1",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Play with response returned in JSON format.
alert(data.toSource());
},
error: function (msg) {
alert("Error" + msg);
}
});
}
function getuser() {
$.ajax({
type: "GET",
url: "MyService.svc/MyUsersList",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Play with response returned in JSON format.
alert(data.toSource());
},
error: function (msg) {
alert("Error" + msg);
}
});
}
</script>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
I was facing a problem while invoking DELETE method in WCF. Here is how i managed to do it.
Step 1. Create a WCF Service.
Create a new WCF service and name it as MyService.svc
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace WCFASPNET
{
public class MyService : MyServiceInterface
{
public string GetAllUsers()
{
//return all users
}
public string DeleteDept(String ID)
{
//delete department
}
}
Step 2. Add operation contracts in the WCF Service Interface file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WCFASPNET
{
[ServiceContract]
public interface MyServiceInterface
{
[OperationContract]
[WebGet(UriTemplate = "/MyUsersList", ResponseFormat = WebMessageFormat.Json)]
string GetAllUsers();
[OperationContract]
[WebInvoke(Method = "DELETE", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/DeleteDept/{ID}")]
string DeleteDept(String ID);
}
}
Step 3. Add Cross Domain Ajax Code in Global.asax file.
Add the Application_BeginRequest event in Global.asax file
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("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
Step 4. Configure web.config file.
<system.serviceModel>
<services>
<service name="WCFASPNET.MyService" behaviorConfiguration="RESTBehavior">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="ServiceBinding" behaviorConfiguration="MyEndpointBehavior" contract="WCFASPNET.MyServiceInterface"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding crossDomainScriptAccessEnabled="true" name="ServiceBinding">
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="RESTBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
<endpointBehaviors>
<behavior name="MyEndpointBehavior">
<webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="false" helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Step 5. Call WCF Delete from jquery
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="js/jquery-1.4.1.js"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
//getuser();
deleteuser();
function deleteuser() {
$.ajax({
type: "DELETE",
url: "MyService.svc/DeleteDept/1",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Play with response returned in JSON format.
alert(data.toSource());
},
error: function (msg) {
alert("Error" + msg);
}
});
}
function getuser() {
$.ajax({
type: "GET",
url: "MyService.svc/MyUsersList",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Play with response returned in JSON format.
alert(data.toSource());
},
error: function (msg) {
alert("Error" + msg);
}
});
}
</script>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
No comments:
Post a Comment
Comments are welcome, Please join me on my Linked In account
http://in.linkedin.com/pub/ritesh-tandon/21/644/33b