Advertisement
Guest User

Untitled

a guest
May 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. // IService1.cs
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Runtime.Serialization;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Web;
  9. using System.Text;
  10.  
  11. namespace MojWebSerwis2
  12. {
  13. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
  14. [ServiceContract]
  15. public interface IService1
  16. {
  17. [OperationContract]
  18. [WebGet(UriTemplate = "/books")]
  19. List<Book> getAll();
  20.  
  21. [OperationContract]
  22. [WebGet(UriTemplate = "/books/{id}", ResponseFormat = WebMessageFormat.Xml)]
  23. Book getById(string Id);
  24.  
  25. [OperationContract]
  26. [WebInvoke(UriTemplate = "/books/{id}", Method = "PUT", RequestFormat = WebMessageFormat.Xml)]
  27. string update(string Id, Book element);
  28.  
  29. [OperationContract]
  30. [WebGet(UriTemplate = "/books/year/{id}", ResponseFormat = WebMessageFormat.Xml)]
  31. List<Book> getByYear(string id);
  32.  
  33. [OperationContract]
  34. [WebInvoke(UriTemplate = "/books/add/{id}", Method = "PUT", RequestFormat = WebMessageFormat.Xml)]
  35. string add(string Id, Book element);
  36.  
  37. [OperationContract]
  38. [WebInvoke(UriTemplate = "/books/delete/{id}", Method = "GET", RequestFormat = WebMessageFormat.Xml)]
  39. string delete(string Id);
  40.  
  41.  
  42. [OperationContract]
  43. [WebGet(UriTemplate = "/json/books", ResponseFormat = WebMessageFormat.Json)]
  44. List<Book> getJsonAll();
  45.  
  46. [OperationContract]
  47. [WebGet(UriTemplate = "/json/books/{id}", ResponseFormat = WebMessageFormat.Json)]
  48. Book getJsonById(string Id);
  49.  
  50. [OperationContract]
  51. [WebInvoke(UriTemplate = "/json/books/{id}", Method = "PUT", RequestFormat = WebMessageFormat.Json)]
  52. string updateJson(string Id, Book element);
  53.  
  54. [OperationContract]
  55. [WebGet(UriTemplate = "/json/books/year/{id}", ResponseFormat = WebMessageFormat.Json)]
  56. List<Book> getByYearJson(string id);
  57.  
  58. [OperationContract]
  59. [WebInvoke(UriTemplate = "/json/books/add/{id}", Method = "PUT", RequestFormat = WebMessageFormat.Json)]
  60. string addJson(string Id, Book element);
  61.  
  62. [OperationContract]
  63. [WebInvoke(UriTemplate = "/json/books/delete/{id}", Method = "GET", RequestFormat = WebMessageFormat.Json)]
  64. string deleteJson(string Id);
  65.  
  66.  
  67. }
  68.  
  69.  
  70. // Use a data contract as illustrated in the sample below to add composite types to service operations.
  71. [DataContract]
  72. public class Book
  73. {
  74. string bookId;
  75. string author;
  76. string title;
  77. string publisher;
  78. int yearOfPublishing;
  79.  
  80. [DataMember]
  81. public string BookIdValue
  82. {
  83. get { return bookId; }
  84. set { bookId = value; }
  85. }
  86.  
  87. [DataMember]
  88. public string AuthorValue
  89. {
  90. get { return author; }
  91. set { author = value; }
  92. }
  93.  
  94. [DataMember]
  95. public string TitleValue
  96. {
  97. get { return title; }
  98. set { title = value; }
  99. }
  100.  
  101. [DataMember]
  102. public string PublisherValue
  103. {
  104. get { return publisher; }
  105. set { publisher = value; }
  106. }
  107.  
  108. [DataMember]
  109. public int YearOfPublishingValue
  110. {
  111. get { return yearOfPublishing; }
  112. set { yearOfPublishing = value; }
  113. }
  114.  
  115. public Book(string id, string author, string title, string publisher, int year)
  116. {
  117. this.bookId = id;
  118. this.author = author;
  119. this.title = title;
  120. this.publisher = publisher;
  121. this.yearOfPublishing = year;
  122. }
  123. }
  124. }
  125.  
  126.  
  127.  
  128. // Service1.svc.cs
  129.  
  130. using System;
  131. using System.Collections.Generic;
  132. using System.Linq;
  133. using System.Runtime.Serialization;
  134. using System.ServiceModel;
  135. using System.ServiceModel.Web;
  136. using System.Text;
  137.  
  138. namespace MojWebSerwis2
  139. {
  140. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
  141. // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
  142. [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
  143. public class Service1 : IService1
  144. {
  145. private static List<Book> books;
  146.  
  147. public Service1()
  148. {
  149. books = new List<Book>
  150. {
  151. new Book("ABC1", "J.K. Rowling", "Harry Potter i Komnata Tajemnic", "WYDEX", 2001)
  152. ,new Book("ABC2", "A. A. Milne", "Kubuś Puchatek", "Helion", 1979)
  153. ,new Book("ABC3", "Henryk Sienkiewicz", "Quo Vadis", "MojaLektura", 2011)
  154. };
  155. }
  156.  
  157. public string add(string Id, Book element)
  158. {
  159. if (books.Find(book => book.BookIdValue == element.BookIdValue) != null)
  160. {
  161. return "Ksiazka o tym id juz istnieje!";
  162. }
  163. books.Add(element);
  164. return "Dodano.";
  165. }
  166.  
  167. public string addJson(string Id, Book element)
  168. {
  169. return add(Id, element);
  170. }
  171.  
  172. public string delete(string Id)
  173. {
  174. var found = books.Find(book => book.BookIdValue == Id);
  175. if (found == null)
  176. {
  177. return "Ksiazka o tym id nie istnieje!";
  178. }
  179. books.Remove(found);
  180. return "Usunieto ksiazke.";
  181. }
  182.  
  183. public string deleteJson(string Id)
  184. {
  185. return delete(Id);
  186. }
  187.  
  188. public List<Book> getAll()
  189. {
  190. return books;
  191. }
  192.  
  193. public Book getById(string Id)
  194. {
  195. return books.Find(book => book.BookIdValue == Id);
  196. }
  197.  
  198. public List<Book> getByYear(string year)
  199. {
  200. int yearInt = int.Parse(year);
  201. return books.Where(book => book.YearOfPublishingValue == yearInt).ToList();
  202. }
  203.  
  204. public List<Book> getByYearJson(string year)
  205. {
  206. return getByYear(year);
  207. }
  208.  
  209. public List<Book> getJsonAll()
  210. {
  211. return getAll();
  212. }
  213.  
  214. public Book getJsonById(string Id)
  215. {
  216. return getById(Id);
  217. }
  218.  
  219. public string update(string Id, Book element)
  220. {
  221. if (element == null)
  222. {
  223. throw new ArgumentNullException("Blad update");
  224. }
  225. int idx = books.FindIndex(b => b.BookIdValue == element.BookIdValue);
  226. if (idx == -1)
  227. return "Nie mozna zaktualizowac elementu o id=" + Id;
  228. books.RemoveAt(idx);
  229. books.Add(element);
  230. return "Zaktualizowano element o id=" + Id;
  231. }
  232.  
  233. public string updateJson(string Id, Book element)
  234. {
  235. return update(Id, element);
  236. }
  237. }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement