Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // IService1.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 MojWebSerwis2
- {
- // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- [WebGet(UriTemplate = "/books")]
- List<Book> getAll();
- [OperationContract]
- [WebGet(UriTemplate = "/books/{id}", ResponseFormat = WebMessageFormat.Xml)]
- Book getById(string Id);
- [OperationContract]
- [WebInvoke(UriTemplate = "/books/{id}", Method = "PUT", RequestFormat = WebMessageFormat.Xml)]
- string update(string Id, Book element);
- [OperationContract]
- [WebGet(UriTemplate = "/books/year/{id}", ResponseFormat = WebMessageFormat.Xml)]
- List<Book> getByYear(string id);
- [OperationContract]
- [WebInvoke(UriTemplate = "/books/add/{id}", Method = "PUT", RequestFormat = WebMessageFormat.Xml)]
- string add(string Id, Book element);
- [OperationContract]
- [WebInvoke(UriTemplate = "/books/delete/{id}", Method = "GET", RequestFormat = WebMessageFormat.Xml)]
- string delete(string Id);
- [OperationContract]
- [WebGet(UriTemplate = "/json/books", ResponseFormat = WebMessageFormat.Json)]
- List<Book> getJsonAll();
- [OperationContract]
- [WebGet(UriTemplate = "/json/books/{id}", ResponseFormat = WebMessageFormat.Json)]
- Book getJsonById(string Id);
- [OperationContract]
- [WebInvoke(UriTemplate = "/json/books/{id}", Method = "PUT", RequestFormat = WebMessageFormat.Json)]
- string updateJson(string Id, Book element);
- [OperationContract]
- [WebGet(UriTemplate = "/json/books/year/{id}", ResponseFormat = WebMessageFormat.Json)]
- List<Book> getByYearJson(string id);
- [OperationContract]
- [WebInvoke(UriTemplate = "/json/books/add/{id}", Method = "PUT", RequestFormat = WebMessageFormat.Json)]
- string addJson(string Id, Book element);
- [OperationContract]
- [WebInvoke(UriTemplate = "/json/books/delete/{id}", Method = "GET", RequestFormat = WebMessageFormat.Json)]
- string deleteJson(string Id);
- }
- // Use a data contract as illustrated in the sample below to add composite types to service operations.
- [DataContract]
- public class Book
- {
- string bookId;
- string author;
- string title;
- string publisher;
- int yearOfPublishing;
- [DataMember]
- public string BookIdValue
- {
- get { return bookId; }
- set { bookId = value; }
- }
- [DataMember]
- public string AuthorValue
- {
- get { return author; }
- set { author = value; }
- }
- [DataMember]
- public string TitleValue
- {
- get { return title; }
- set { title = value; }
- }
- [DataMember]
- public string PublisherValue
- {
- get { return publisher; }
- set { publisher = value; }
- }
- [DataMember]
- public int YearOfPublishingValue
- {
- get { return yearOfPublishing; }
- set { yearOfPublishing = value; }
- }
- public Book(string id, string author, string title, string publisher, int year)
- {
- this.bookId = id;
- this.author = author;
- this.title = title;
- this.publisher = publisher;
- this.yearOfPublishing = year;
- }
- }
- }
- // Service1.svc.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 MojWebSerwis2
- {
- // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
- // 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.
- [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
- public class Service1 : IService1
- {
- private static List<Book> books;
- public Service1()
- {
- books = new List<Book>
- {
- new Book("ABC1", "J.K. Rowling", "Harry Potter i Komnata Tajemnic", "WYDEX", 2001)
- ,new Book("ABC2", "A. A. Milne", "Kubuś Puchatek", "Helion", 1979)
- ,new Book("ABC3", "Henryk Sienkiewicz", "Quo Vadis", "MojaLektura", 2011)
- };
- }
- public string add(string Id, Book element)
- {
- if (books.Find(book => book.BookIdValue == element.BookIdValue) != null)
- {
- return "Ksiazka o tym id juz istnieje!";
- }
- books.Add(element);
- return "Dodano.";
- }
- public string addJson(string Id, Book element)
- {
- return add(Id, element);
- }
- public string delete(string Id)
- {
- var found = books.Find(book => book.BookIdValue == Id);
- if (found == null)
- {
- return "Ksiazka o tym id nie istnieje!";
- }
- books.Remove(found);
- return "Usunieto ksiazke.";
- }
- public string deleteJson(string Id)
- {
- return delete(Id);
- }
- public List<Book> getAll()
- {
- return books;
- }
- public Book getById(string Id)
- {
- return books.Find(book => book.BookIdValue == Id);
- }
- public List<Book> getByYear(string year)
- {
- int yearInt = int.Parse(year);
- return books.Where(book => book.YearOfPublishingValue == yearInt).ToList();
- }
- public List<Book> getByYearJson(string year)
- {
- return getByYear(year);
- }
- public List<Book> getJsonAll()
- {
- return getAll();
- }
- public Book getJsonById(string Id)
- {
- return getById(Id);
- }
- public string update(string Id, Book element)
- {
- if (element == null)
- {
- throw new ArgumentNullException("Blad update");
- }
- int idx = books.FindIndex(b => b.BookIdValue == element.BookIdValue);
- if (idx == -1)
- return "Nie mozna zaktualizowac elementu o id=" + Id;
- books.RemoveAt(idx);
- books.Add(element);
- return "Zaktualizowano element o id=" + Id;
- }
- public string updateJson(string Id, Book element)
- {
- return update(Id, element);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement