Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .cs ----------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- namespace mojwebserwis
- {
- // UWAGA: możesz użyć polecenia „Zmień nazwę” w menu „Refaktoryzuj”, aby zmienić nazwę interfejsu „IService1” w kodzie i pliku konfiguracji.
- [ServiceContract]
- public interface ilibrary
- {
- [OperationContract]
- [WebGet(UriTemplate = "/baza")]
- List<book> getAll();
- [OperationContract]
- [WebGet(UriTemplate = "/json/baza")]
- List<book> getJsonAll();
- [OperationContract]
- [WebGet(UriTemplate = "/baza/{id}",
- ResponseFormat = WebMessageFormat.Xml)]
- book getByID(string ID);
- [OperationContract]
- [WebGet(UriTemplate = "/json/baza/{id}",
- ResponseFormat = WebMessageFormat.Json)]
- book getJsonByID(string ID);
- [OperationContract]
- [WebInvoke(UriTemplate = "/baza/{id}",
- Method = "PUT",
- RequestFormat = WebMessageFormat.Xml)]
- string update(string ID, book element);
- [OperationContract]
- [WebInvoke(UriTemplate = "/json/baza/{id}",
- Method = "PUT",
- RequestFormat = WebMessageFormat.Json)]
- string updateJson(string ID, book element);
- [OperationContract]
- [WebInvoke(UriTemplate = "/baza",
- Method = "POST",
- RequestFormat = WebMessageFormat.Xml)]
- string create(book element);
- [OperationContract]
- [WebInvoke(UriTemplate = "/json/baza",
- Method = "POST",
- RequestFormat = WebMessageFormat.Json)]
- string createJson(book element);
- [OperationContract]
- [WebInvoke(UriTemplate = "/baza",
- Method = "DELETE",
- ResponseFormat = WebMessageFormat.Xml)]
- string delete(int ID);
- [OperationContract]
- [WebInvoke(UriTemplate = "/json/baza",
- Method = "DELETE",
- ResponseFormat = WebMessageFormat.Json)]
- string deleteJson(int ID);
- }
- // Użyj kontraktu danych, jak pokazano w poniższym przykładzie, aby dodać typy złożone do operacji usługi.
- [DataContract]
- public class book
- {
- [DataMember]
- public int id;
- [DataMember]
- public string title;
- [DataMember]
- public string author;
- [DataMember]
- public double price;
- [DataMember]
- public bool inlibrary;
- public book(int id, string title, string author, double price, bool inlibrary)
- {
- this.id = id;
- this.title = title;
- this.author = author;
- this.price = price;
- this.inlibrary = inlibrary;
- }
- }
- }
- WEBCONFIG---------------------------------------------------------------------------------------------------
- <?xml version="1.0"?>
- <configuration>
- <appSettings>
- <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
- </appSettings>
- <!--
- For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
- The following attributes can be set on the <httpRuntime> tag.
- <system.Web>
- <httpRuntime targetFramework="4.6.1" />
- </system.Web>
- -->
- <system.web>
- <compilation debug="true" targetFramework="4.6.1"/>
- <httpRuntime targetFramework="4.6.1"/>
- </system.web>
- <system.serviceModel>
- <services>
- <service name="mojwebserwis.library" behaviorConfiguration="">
- <endpoint address="" binding="webHttpBinding" contract="mojwebserwis.ilibrary" behaviorConfiguration="mojRESTEndpointBehavior"/>
- </service>
- </services>
- <behaviors>
- <endpointBehaviors>
- <behavior name="mojRESTEndpointBehavior">
- <webHttp helpEnabled="true"/>
- </behavior>
- </endpointBehaviors>
- <serviceBehaviors>
- <behavior>
- <!-- Aby zapobiec ujawnieniu informacji o metadanych, ustaw dla poniższych elementów wartość false przed wdrożeniem -->
- <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
- <!-- 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 -->
- <serviceDebug includeExceptionDetailInFaults="false"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <protocolMapping>
- <add binding="basicHttpsBinding" scheme="https"/>
- </protocolMapping>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
- </system.serviceModel>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- <!--
- Aby przeglądać katalog główny aplikacji internetowej w trakcie debugowania, ustaw dla poniższego elementu wartość true.
- Ustaw wartość false przed wdrożeniem, aby zapobiec ujawnieniu informacji o folderze aplikacji internetowej.
- -->
- <directoryBrowse enabled="true"/>
- </system.webServer>
- </configuration>
- SERVICE1.SVC -------------------------------------------------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- namespace mojwebserwis
- {
- // UWAGA: możesz użyć polecenia „Zmień nazwę” w menu „Refaktoryzuj”, aby zmienić nazwę klasy „Service1” w kodzie, usłudze i pliku konfiguracji.
- // 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.
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
- public class library : ilibrary
- {
- private static List<book> books = new List<book>();
- public library()
- {
- books.Add(new book(1, "a", "a", 10, true));
- }
- public string create(book element)
- {
- books.Add(element);
- return "Book has been added";
- }
- public string createJson(book element)
- {
- books.Add(element);
- return "Book has been added";
- }
- public string delete(int ID)
- {
- if (books.Count < ID || ID <= 0) return "Wrong ID";
- else
- {
- books.RemoveAt(ID - 1);
- return "Book has been deleted";
- }
- }
- public string deleteJson(int ID)
- {
- if (books.Count < ID || ID <= 0) return "Wrong ID";
- else
- {
- books.RemoveAt(ID - 1);
- return "Book has been deleted";
- }
- }
- public List<book> getAll()
- {
- return books;
- }
- public book getByID(string ID)
- {
- int intID = int.Parse(ID);
- return books.Find(b => b.id == intID);
- }
- public List<book> getJsonAll()
- {
- return books;
- }
- public book getJsonByID(string ID)
- {
- int intID = int.Parse(ID);
- return books.Find(b => b.id == intID);
- }
- public string update(string ID, book element)
- {
- if (element == null)
- throw new ArgumentNullException("Error");
- int idx = books.FindIndex(b => b.id == element.id);
- if (idx == -1)
- return "Unable to update this element";
- books.RemoveAt(idx);
- books.Add(element);
- return "Succesful update";
- }
- public string updateJson(string ID, book element)
- {
- if (element == null)
- throw new ArgumentNullException("Error");
- int idx = books.FindIndex(b => b.id == element.id);
- if (idx == -1)
- return "Unable to update this element";
- books.RemoveAt(idx);
- books.Add(element);
- return "Succesful update";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement