Call WCF rest service without SVC file and WCF Configuration

Source: Internet
Author: User

1. Create empty web project and add the following class along with its associated interface code

Public class person
{
Public String ID
{
Get;
Set;
}

Public string name
{
Get;
Set;
}
}

[Servicecontract]
Interface ipersonservice
{

[Operationcontract]
Person getpo ();

[Operationcontract]
String sayhello (string name );

[Operationcontract]
Person getperson (string ID );

 

[Operationcontract]
Person insertperson (person );

[Operationcontract]
Person updateperson (string ID, person );

[Operationcontract]
Void deleteperson (string ID );

[Operationcontract]
String Se ();

[Operationcontract]
Stream post (string message, int X, int y );
}

 

 

[Aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. Allowed)]
Public class personservice: ipersonservice
{
Public list <person> personlist = new list <person> ();

[Webget (uritemplate = "se")]
Public String Se ()
{
Return "this is test ";
}

[Webget (uritemplate = "SS")]
Public Person getpo ()
{
Person P = new person ();
P. ID = "testid ";
P. Name = "personname ";
Return P;
}

[Webget (uritemplate = "sayhello ({name})")]
Public String sayhello (string name)
{

Return string. Format ("Hello, {0}", name );

}

[Webget (uritemplate = "Person ({ID})")]
Public Person getperson (string ID)
{
Person P = new person ();
P. ID = ID;
P. Name = "person name ";
Return P;
}

[Webinvoke (uritemplate = "ins", method = "Post", requestformat = webmessageformat. JSON, responseformat = webmessageformat. JSON)]

Public Person insertperson (person)
{

Person P = person;
P. ID = "changedid ";
Return P;

}

[Webinvoke (uritemplate = "Person ({ID})", method = "put")]
Public Person updateperson (string ID, person)
{
VaR A = from P in PS
Where p. ID = ID
Select P;
Return a as person;

}

[Webinvoke (uritemplate = "Person ({ID})", method = "delete")]
Public void deleteperson (string ID)
{
PS. removeat (Int. parse (ID ));

}

[Webinvoke (bodystyle = webmessagebodystyle. Wrapped, requestformat = webmessageformat. JSON, uritemplate = "")]
Public stream post (string message, int X, int y) {return New memorystream (encoding. utf8.getbytes ("<B>" + message + (x + y) + "</B> "));}

}

 

Web. config:

<Configuration>
<System. Web>
<Compilation DEBUG = "true" targetframework = "4.0"/>
</System. Web>
<System. servicemodel>
<Servicehostingenvironment aspnetcompatibilityenabled = "true" minfreemorypercentagetoactivateservice = "1"/>
<Standardendpoints>
<Webhttpendpoint>
<Standardendpoint name = "" helpenabled = "true" automaticformatselectionenabled = "true"/>

</Webhttpendpoint>
</Standardendpoints>


</System. servicemodel>
</Configuration>

 

Minfreememorypercentagetoactivateservice attribute is needed only when the red memory error occurs.

 

HTML page:

Function getperson (){

VaR name = $ ("# name"). Val ();
$. Ajax ({
Cache: false,
Type: "Get ",
Async: false,
URL: "person" + "(" + name + ")",
Datatype: "JSON ",

Processdata: false,

Success: function (MSG ){

Alert (msg. Name );

},

Error: function (ERR ){

// Error handling

Alert ("error occured! "+ Err. msg );

}

});

}

 

Function postdata (){
Alert ("begin ");
$. Ajax ({URL :"/",
Type: "Post ",
Contenttype: "application/JSON ",
Data: '{"X": "1", "Y": "2", "message": "The answer is :"}',
Datatype: "html ",
Success: function (data) {detail ('body'detail .html (data );},
Error: function (ERR) {alert ("error occured! "+ Err. msg );}

});
}

Function insertpeople (){
// Call the method and pass a person object in JSON format. The returned result is a JSON object.
VaR DATA = {name: "Denny", ID: "23 "};
VaR jsonstr = JSON. stringify (data); // format the object into a JSON string
Sendajax ("ins", jsonstr,
Function (MSG ){
Alert (msg. Name + "" + msg. ID + "this is a test case ");
});
}

Function sendajax (URL, Data, success ){
$. Ajax ({
Type: "Post ",
Contenttype: "application/JSON ",
URL: URL,
Data: data,
Processdata: false,
Success: success,
Error: function (XMLHttpRequest, textstatus, errorthrown ){
Alert ("error occured! ");
}
});
}

 

The code above is the Demo code for calling WCF rest service without SVC file using jquery, hopefully it's a basic guideline for all of you :)

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.