Thursday, July 18, 2013

RESTful WCF Service

CRUD operations using RESTful WCF Service
    [ServiceContract]
    public interface IBlogService
    {
        [OperationContract]
        [WebInvoke(Method="GET", BodyStyle=WebMessageBodyStyle.Bare, RequestFormat= WebMessageFormat.Json, UriTemplate = "Blog/{id}")]
        BlogPost GetBlogPost(string id);

        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, UriTemplate = "Blog/All")]
        List GetAllPosts();

        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, UriTemplate = "AddBlog/{id}")]
        BlogPost PostBlog(string id, BlogPost post);

        [OperationContract]
        [WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, UriTemplate = "UpdateBlog/{id}")]
        void PutBlog(string id, BlogPost post);

        [OperationContract]
        [WebInvoke(Method = "DELETE", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, UriTemplate = "DeleteBlog/{id}")]
        void DeleteBlog(string id);
    }
    [DataContract]
    public class BlogPost
    {
        [DataMember]
        public string Id { get; set; }
        [DataMember]
        public string Title { get; set; }
        [DataMember]
        public string Content { get; set; }
        [DataMember]
        public string Labels { get; set; }
    }
    
    public class BlogService : IBlogService
    {
        static List posts = new List  
                {              
                    new BlogPost{ Id = "1", Title= "Web Api Framework on .NET", Content = "services exposed over plain HTTP", Labels= "" },              
                    new BlogPost{ Id = "2", Title= "MVC4", Content= "Mobile Support", Labels= "" },              
                    new BlogPost{ Id = "3", Title = "KnockoutJS", Content= "Javascript based MVVM framework", Labels= ""}          
                };


        public BlogPost GetBlogPost(string id)
        {
            WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
            return posts.Find(m => m.Id == id);            
        }

        public List GetAllPosts()
        {
            WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
            return posts;
        }

        public BlogPost PostBlog(BlogPost post)
        {
            WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
            posts.Add(post);
            return post;

        }

        public void PutBlog(string id, BlogPost post)
        {
            var blog = posts.Find(m => m.Id == id);
            blog.Title = post.Title;
            blog.Content = post.Content;
        }

        public void DeleteBlog(string id)
        {
            var post = posts.Find(m => m.Id == id);
            posts.Remove(post);
        }
    }


    
      
        
      
    
    
      
        
          
          
          
                  

        
      
      
        
          
        
      
    
    
      
        
      
      

    
  
  
    
    
    
  


    
    
    
    
    


    
My Blogs
    Add Blog
    Title :
    Content:
    Delete Blog

    No comments:

    Post a Comment