WCF service Lightweight service, available for JS invocation
Return value format: XML, Json
Engineering Structure:
Example code:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 6 namespacerestfulwebservices7 {8 Public classUserInfo9 {Ten Public stringName {Get;Set; } One Public intAge {Get;Set; } A Public stringSex {Get;Set; } - } -}
entity class
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Runtime.Serialization;5 usingSystem.ServiceModel;6 usingSystem.Text;7 usingSystem.ServiceModel.Web;8 9 namespacerestfulwebservicesTen { One //Note: Using the rename command on the Refactor menu, you can change the interface name "IService1" in code and configuration files at the same time. A [ServiceContract] - Public InterfaceIService1 - { the [OperationContract] -[WebGet (UriTemplate ="Getlist/{name1}/{name2}", -Bodystyle = webmessagebodystyle.bare, Requestformat = Webmessageformat.json, Responseformat =Webmessageformat.json)] -List<userinfo> GetList (stringNAME1,stringname2); + - [OperationContract] +[WebGet (UriTemplate ="GetUserInfo", ABodystyle = webmessagebodystyle.bare, Requestformat = Webmessageformat.json, Responseformat =Webmessageformat.xml)] at UserInfo GetUserInfo (); - } -}
Service Contract
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Runtime.Serialization;5 usingSystem.ServiceModel;6 usingSystem.Text;7 8 namespacerestfulwebservices9 {Ten //Note: Using the rename command on the Refactor menu, you can change the class name "Service1" in Code, SVC, and configuration files at the same time. One Public classService1:iservice1 A { - PublicList<userinfo> GetList (stringNAME1,stringname2) - { thelist<userinfo> list =NewList<userinfo>(); -UserInfo u1 =NewUserInfo (); -U1. Age = the; -U1. Name =name1; +U1. Sex ="female"; - list. Add (U1); + AUserInfo U2 =NewUserInfo (); atU2. Age = in; -U2. Name =name2; -U2. Sex ="male"; - list. Add (U2); - - returnlist; in } - to PublicUserInfo GetUserInfo () + { -UserInfo U2 =NewUserInfo (); theU2. Age = -; *U2. Name ="Bachelor"; $U2. Sex ="male";Panax Notoginseng returnU2; - } the } +}
Service Implementation
1<?xml version="1.0"?>2<configuration>3<system.web>4<compilation debug="true"targetframework="4.0"/>5</system.web>6<system.serviceModel>7<services>8<service behaviorconfiguration="Getpostbehavior"Name="Restfulwebservices.service1">9<endpoint address=""behaviorconfiguration="getpostendbehaviors"binding="webhttpbinding" Tencontract="Restfulwebservices.iservice1"> One</endpoint> A<endpoint address="Mex"binding="mexhttpbinding"contract="IMetadataExchange"/> -</service> -</services> the<behaviors> -<endpointBehaviors> -<behavior name="getpostendbehaviors"> -<webhttp/> +</behavior> -</endpointBehaviors> +<serviceBehaviors> A<behavior name="Getpostbehavior"> at<servicemetadata httpgetenabled="true"/> -<servicedebug includeexceptiondetailinfaults="false"/> -</behavior> -</serviceBehaviors> -</behaviors> -<servicehostingenvironment multiplesitebindingsenabled="true"/> in</system.serviceModel> -</configuration>
configuration file
Operation Result:
Non-Reference Service invocation Method: Address + method Name Http://localhost:1768/Service1.svc/GetUserInfo
return value in XML format:
Call mode with parameter service: Address + method name + parameter 1+ parameter n http://localhost:1768/Service1.svc/GetList/Jim/Tom
Return value in JSON format:
Sample source Download: Restfulwebservices.rar
RESTful Services (with WCF) introduction
RESTful Services Example