Membuat Project WCF pada Visual Studio 2012
sourcecode : GetUser.svc.cd
1: using System;
2: using System.Data;
3: using System.Collections.Generic;
4: using System.Linq;
5: using System.Runtime.Serialization;
6: using System.ServiceModel;
7: using System.ServiceModel.Activation;
8: using System.Text;
9: using System.Data.SqlClient;
10: using System.ServiceModel.Web;
11: namespace JsonWcfService
12: {
13: public class GetUser : IGetUser
14: {
15: [WebInvoke(Method = "GET",
16: ResponseFormat = WebMessageFormat.Json,
17: RequestFormat = WebMessageFormat.Json,
18: BodyStyle = WebMessageBodyStyle.Wrapped,
19: UriTemplate = "GetJson")]
20: public List<User> GetUserGrade()
21: {
22: List<User> mylist = new List<User>();
23: using (SqlConnection conn = new SqlConnection("server=(local);database=osn2013;Integrated Security=SSPI;"))
24: {
25: conn.Open();
26: string cmdStr = String.Format("SELECT * FROM View_UserGrade ORDER BY Grade DESC");
27: SqlCommand cmd = new SqlCommand(cmdStr, conn);
28: SqlDataReader rd = cmd.ExecuteReader();
29: if (rd.HasRows)
30: {
31: while (rd.Read())
32: {
33: mylist.Add(new User(
34: rd.GetString(0),
35: rd.GetString(1),
36: rd.GetString(2),
37: rd.GetString(3),
38: rd.GetInt32(4)
39: ));
40: }
41: }
42: conn.Close();
43: }
44: return mylist;
45: }
46: }
47: [DataContract]
48: public class User
49: {
50: [DataMember]
51: public string userid{ get; set; }
52: [DataMember]
53: public string name{ get; set; }
54: [DataMember]
55: public string country { get; set; }
56: [DataMember]
57: public string college { get; set; }
58: [DataMember]
59: public int grade { get; set; }
60: public User(string UserID, string Name, string Country, string Collage, int Grade)
61: {
62: userid = UserID;
63: name = Name;
64: country = Country;
65: college = Collage;
66: grade = Grade;
67: }
68: }
69: }
1: using System.IO;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5: using System.Runtime.Serialization;
6: using System.ServiceModel;
7: namespace JsonWcfService
8: {
9: [ServiceContract]
10: public interface IGetUser
11: {
12: [OperationContract]
13: List<User> GetUserGrade();
14: }
15: }
sourcecode: webconfig.config1: <?xml version="1.0"?>
2: <configuration>
3: <system.web>
4: <compilation debug="true" targetFramework="4.0" />
5: <authentication mode="None" />
6: </system.web>
7: <system.serviceModel>
8: <serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>
9: <standardEndpoints>
10: <webScriptEndpoint>
11: <standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
12: </webScriptEndpoint>
13: </standardEndpoints>
14: <bindings>
15: <webHttpBinding>
16: <binding name="NewBinding0" />
17: </webHttpBinding>
18: </bindings>
19: <services>
20: <service behaviorConfiguration="EmpServiceBehaviour" name="JsonWcfService.GetUser">
21: <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
22: bindingConfiguration="NewBinding0" contract="JsonWcfService.IGetUser" />
23: </service>
24: </services>
25: <behaviors>
26: <endpointBehaviors>
27: <behavior name="web">
28: <webHttp defaultOutgoingResponseFormat="Json" />
29: </behavior>
30: </endpointBehaviors>
31: <serviceBehaviors>
32: <behavior name="EmpServiceBehaviour">
33: <serviceMetadata httpGetEnabled="true" />
34: <serviceDebug includeExceptionDetailInFaults="false" />
35: </behavior>
36: <behavior name="">
37: <serviceMetadata httpGetEnabled="true" />
38: <serviceDebug includeExceptionDetailInFaults="false" />
39: </behavior>
40: </serviceBehaviors>
41: </behaviors>
42: </system.serviceModel>
43: <system.webServer>
44: <modules runAllManagedModulesForAllRequests="true"/>
45: </system.webServer>
46: </configuration>
No comments:
Post a Comment