Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. .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 mojwebserwis
  12. {
  13. // UWAGA: możesz użyć polecenia „Zmień nazwę” w menu „Refaktoryzuj”, aby zmienić nazwę interfejsu „IService1” w kodzie i pliku konfiguracji.
  14. [ServiceContract]
  15. public interface ilibrary
  16. {
  17.  
  18. [OperationContract]
  19. [WebGet(UriTemplate = "/baza")]
  20. List<book> getAll();
  21.  
  22. [OperationContract]
  23. [WebGet(UriTemplate = "/json/baza")]
  24. List<book> getJsonAll();
  25.  
  26. [OperationContract]
  27. [WebGet(UriTemplate = "/baza/{id}",
  28. ResponseFormat = WebMessageFormat.Xml)]
  29. book getByID(string ID);
  30.  
  31. [OperationContract]
  32. [WebGet(UriTemplate = "/json/baza/{id}",
  33. ResponseFormat = WebMessageFormat.Json)]
  34. book getJsonByID(string ID);
  35.  
  36. [OperationContract]
  37. [WebInvoke(UriTemplate = "/baza/{id}",
  38. Method = "PUT",
  39. RequestFormat = WebMessageFormat.Xml)]
  40. string update(string ID, book element);
  41.  
  42. [OperationContract]
  43. [WebInvoke(UriTemplate = "/json/baza/{id}",
  44. Method = "PUT",
  45. RequestFormat = WebMessageFormat.Json)]
  46. string updateJson(string ID, book element);
  47.  
  48. [OperationContract]
  49. [WebInvoke(UriTemplate = "/baza",
  50. Method = "POST",
  51. RequestFormat = WebMessageFormat.Xml)]
  52. string create(book element);
  53.  
  54. [OperationContract]
  55. [WebInvoke(UriTemplate = "/json/baza",
  56. Method = "POST",
  57. RequestFormat = WebMessageFormat.Json)]
  58. string createJson(book element);
  59.  
  60. [OperationContract]
  61. [WebInvoke(UriTemplate = "/baza",
  62. Method = "DELETE",
  63. ResponseFormat = WebMessageFormat.Xml)]
  64. string delete(int ID);
  65.  
  66. [OperationContract]
  67. [WebInvoke(UriTemplate = "/json/baza",
  68. Method = "DELETE",
  69. ResponseFormat = WebMessageFormat.Json)]
  70. string deleteJson(int ID);
  71.  
  72. }
  73.  
  74.  
  75. // Użyj kontraktu danych, jak pokazano w poniższym przykładzie, aby dodać typy złożone do operacji usługi.
  76. [DataContract]
  77. public class book
  78. {
  79. [DataMember]
  80. public int id;
  81. [DataMember]
  82. public string title;
  83. [DataMember]
  84. public string author;
  85. [DataMember]
  86. public double price;
  87. [DataMember]
  88. public bool inlibrary;
  89.  
  90. public book(int id, string title, string author, double price, bool inlibrary)
  91. {
  92. this.id = id;
  93. this.title = title;
  94. this.author = author;
  95. this.price = price;
  96. this.inlibrary = inlibrary;
  97. }
  98.  
  99.  
  100. }
  101. }
  102. WEBCONFIG---------------------------------------------------------------------------------------------------
  103. <?xml version="1.0"?>
  104. <configuration>
  105. <appSettings>
  106. <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  107. </appSettings>
  108. <!--
  109. For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
  110.  
  111. The following attributes can be set on the <httpRuntime> tag.
  112. <system.Web>
  113. <httpRuntime targetFramework="4.6.1" />
  114. </system.Web>
  115. -->
  116. <system.web>
  117. <compilation debug="true" targetFramework="4.6.1"/>
  118. <httpRuntime targetFramework="4.6.1"/>
  119. </system.web>
  120. <system.serviceModel>
  121. <services>
  122. <service name="mojwebserwis.library" behaviorConfiguration="">
  123. <endpoint address="" binding="webHttpBinding" contract="mojwebserwis.ilibrary" behaviorConfiguration="mojRESTEndpointBehavior"/>
  124. </service>
  125. </services>
  126. <behaviors>
  127. <endpointBehaviors>
  128. <behavior name="mojRESTEndpointBehavior">
  129. <webHttp helpEnabled="true"/>
  130. </behavior>
  131. </endpointBehaviors>
  132. <serviceBehaviors>
  133. <behavior>
  134. <!-- Aby zapobiec ujawnieniu informacji o metadanych, ustaw dla poniższych elementów wartość false przed wdrożeniem -->
  135. <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
  136. <!-- Aby uzyskać szczegółowe informacje o wyjątku dotyczące błędów na potrzeby debugowania, ustaw dla poniższego elementu wartość true. Ustaw wartość false przed wdrożeniem, aby zapobiec ujawnieniu informacji o wyjątku -->
  137. <serviceDebug includeExceptionDetailInFaults="false"/>
  138. </behavior>
  139. </serviceBehaviors>
  140. </behaviors>
  141. <protocolMapping>
  142. <add binding="basicHttpsBinding" scheme="https"/>
  143. </protocolMapping>
  144. <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  145. </system.serviceModel>
  146. <system.webServer>
  147. <modules runAllManagedModulesForAllRequests="true"/>
  148. <!--
  149. Aby przeglądać katalog główny aplikacji internetowej w trakcie debugowania, ustaw dla poniższego elementu wartość true.
  150. Ustaw wartość false przed wdrożeniem, aby zapobiec ujawnieniu informacji o folderze aplikacji internetowej.
  151. -->
  152. <directoryBrowse enabled="true"/>
  153. </system.webServer>
  154. </configuration>
  155.  
  156.  
  157. SERVICE1.SVC -------------------------------------------------------------------------------------------------------------------
  158. using System;
  159. using System.Collections.Generic;
  160. using System.Linq;
  161. using System.Runtime.Serialization;
  162. using System.ServiceModel;
  163. using System.ServiceModel.Web;
  164. using System.Text;
  165.  
  166.  
  167.  
  168.  
  169. namespace mojwebserwis
  170. {
  171. // UWAGA: możesz użyć polecenia „Zmień nazwę” w menu „Refaktoryzuj”, aby zmienić nazwę klasy „Service1” w kodzie, usłudze i pliku konfiguracji.
  172. // UWAGA: aby uruchomić klienta testowego WCF w celu przetestowania tej usługi, wybierz plik Service1.svc lub Service1.svc.cs w eksploratorze rozwiązań i rozpocznij debugowanie.
  173. [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
  174.  
  175. public class library : ilibrary
  176. {
  177. private static List<book> books = new List<book>();
  178. public library()
  179. {
  180. books.Add(new book(1, "a", "a", 10, true));
  181. }
  182.  
  183. public string create(book element)
  184. {
  185. books.Add(element);
  186. return "Book has been added";
  187. }
  188.  
  189. public string createJson(book element)
  190. {
  191. books.Add(element);
  192. return "Book has been added";
  193. }
  194.  
  195. public string delete(int ID)
  196. {
  197. if (books.Count < ID || ID <= 0) return "Wrong ID";
  198. else
  199. {
  200. books.RemoveAt(ID - 1);
  201. return "Book has been deleted";
  202. }
  203. }
  204.  
  205. public string deleteJson(int ID)
  206. {
  207. if (books.Count < ID || ID <= 0) return "Wrong ID";
  208. else
  209. {
  210. books.RemoveAt(ID - 1);
  211. return "Book has been deleted";
  212. }
  213. }
  214.  
  215. public List<book> getAll()
  216. {
  217. return books;
  218. }
  219.  
  220. public book getByID(string ID)
  221. {
  222. int intID = int.Parse(ID);
  223. return books.Find(b => b.id == intID);
  224. }
  225.  
  226. public List<book> getJsonAll()
  227. {
  228. return books;
  229. }
  230.  
  231. public book getJsonByID(string ID)
  232. {
  233. int intID = int.Parse(ID);
  234. return books.Find(b => b.id == intID);
  235. }
  236.  
  237. public string update(string ID, book element)
  238. {
  239. if (element == null)
  240. throw new ArgumentNullException("Error");
  241. int idx = books.FindIndex(b => b.id == element.id);
  242. if (idx == -1)
  243. return "Unable to update this element";
  244. books.RemoveAt(idx);
  245. books.Add(element);
  246. return "Succesful update";
  247. }
  248.  
  249. public string updateJson(string ID, book element)
  250. {
  251. if (element == null)
  252. throw new ArgumentNullException("Error");
  253. int idx = books.FindIndex(b => b.id == element.id);
  254. if (idx == -1)
  255. return "Unable to update this element";
  256. books.RemoveAt(idx);
  257. books.Add(element);
  258. return "Succesful update";
  259. }
  260.  
  261.  
  262.  
  263. }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement