Tuesday, October 18, 2011

Create a POST to a REST WCF Service with Fiddler



How to POST to REST WCF web services with Fiddler

CREATE RESTful WCF Service API Using POST: Step By Step Guide

After making a set of REST web services with WCF I tested them by making requests with a browser. 
This is fine for GET requests however you will need some HTTP building tool such as Fiddler to test a POST, PUT or DELETE REST web service.

To begin with I have a WCF REST web service that intends for a RequestData object to be POSTed:

        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Xml,
            RequestFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "auth")]
       ResponseData Auth(RequestData rData);


The definition of RequestData is very simple:



  public class RequestData
    {
        [DataMember]
        public string details { get; set; }
    }


The definition of ResponseData is quite simple:



public class ResponseData
    {
        [DataMember]
        public string Name { get; set; }


        [DataMember]
        public string Age { get; set; }


        [DataMember]
        public string Exp { get; set; }


        [DataMember]
        public string Technology { get; set; }
    }


The function that gets RequestData and returns ResponseData : 


    public ResponseData Auth(RequestData rData)
        {
            var data = rData.details.Split('|');            
           var response = new ResponseData
                               {
                                   Name = data[0],
                                   Age = data[1],
                                   Exp = data[2],
                                   Technology = data[3]
                               };

            return response;
        }


Note this namespace is very important as it will need to be specified in the XML sent in the request. 
Personally I set my DataContract with no namespace to save having to write it out in the XML every time.

Now open up Fiddler and lets get testing:

Create a POST to your REST service in Fiddler
Go to the Request Builder tab in Fiddler.
Set the verb to POST
Set the URL to the one of your service
Type "Content-Type: application/xml" into the Request Headers. 
If you did give your DataContract a namespace other than blank as I did you will need to specify it in the XML of your Request Body. 
Add your XML to the Request Body like : 





The response : 




Comsuming WCF Services With Android

Create a Simple WCF service against Pubs Database using EF, LINQ



PersonPassport4.asmx - Simple webservice : INSERT UPDATE DELETE



RESTful ASP.NET WCF with SQLite without LINQ: 

  [ServiceContract]

    public interface IService1
    {

    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "Notes/{ID}")]
        [OperationContract]
        Note GetNote(string ID);

   [DataContract]
    public class Note
    {
         [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Category { get; set; }
        [DataMember]
        public string Subject { get; set; }
        [DataMember]
        public string NoteText { get; set; }
    }

      public Note GetNote(string ID)
        {
            string Sql = "SELECT * FROM NOTE WHERE ID=@ID";
            SQLiteCommand cmd = new SQLiteCommand(Sql, conn);
             cmd.Parameters.AddWithValue("@ID", int.Parse(ID));
            SQLiteDataAdapter da = new SQLiteDataAdapter(Sql, conn);
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
             da.Fill(dt);
            Note note=null;
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                note = new Note();
                note.ID = Convert.ToInt32(row["ID"]);
                note.Category = (string)row["Category"];
                note.Subject = (string)row["Subject"];
                note.NoteText = (string)row["NoteText"];
            }
    
            WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
             return note;
        }

Sunday, October 16, 2011

svc-Integrated bad module ManagedPipelineHandler


HTTP Error 500.21 - Internal Server Error

Handler "svc-Integrated" has a bad module "ManagedPipelineHandler" in its module list






Solution : 
run aspnet_regiis.exe -i in VS 2010 command prompt.



Visual Studio 2010 -> Tools -> command prompt :



Wednesday, October 12, 2011

WCF SOAP REST WsHttpBinding webHttpBinding


BasicHttpBinding vs wsHttpBinding vs WebHttpBinding





  • webHttpBinding is the REST-style binding, where you basically just hit a URL and get back a truckload of XML or JSON from the web service
  • basicHttpBinding and wsHttpBinding are two SOAP-based bindings which is quite different from REST. SOAP has the advantage of having WSDL and XSD to describe the service, its methods, and the data being passed around in great detail (REST doesn't have anything like that - yet). On the other hand, you can't just browse to a wsHttpBinding endpoint with your browser and look at XML - you have to use a SOAP client, e.g. the WcfTestClient or your own app
So your first decision must be: REST vs. SOAP (or you can expose both types of endpoints from your service - that's possible, too).
Then, between basicHttpBinding and wsHttpBinding, there differences are as follows:

Tuesday, October 11, 2011

Drop the Soap : WCF , REST , .NET4 , LINQ


Drop the Soap: WCF, REST, URIs in .NET 4


This post will cover REST in .NET 4 and Visual Web Developer 2010.

Getting Started

To get started we’ll create a basic WCF Rest Service Application
using the new on-line templates option in VVD 2010:



Dude Where’s my .Svc File?

The WCF REST template shows us the new way we can simply build services.  Before we talk about what’s there, let’s look at what is not there:
  • The .Svc File
  • An Interface Contract
  • Dozens of lines of configuration that you have to change to make your service work
REST in .NET 4 is greatly simplified and leverages the Web Routing capabilities used in ASP.NET MVC and other parts of the web frameworks.  With REST in .NET 4 you use a global.asax to set the route to your service using the new ServiceRoute class.  From there, the WCF runtime handles dispatching service calls to the methods based on the Uri Templates.






~ ~ ~ 
wcf 4 linq  :



WCF and LINQ to SQL


WCF and LINQ to SQL (Part 2 (Creates))


WCF and LINQ to SQL (Part 3 (Deletes))


WCF and LINQ to SQL (Part 4 – Updates)






Simple 5 steps to expose WCF services using REST style

http://bitcast-g.bitgravity.com/iplayerhd/data/1/55936c11.mp4





The advantages of using JSON enabled WCF service is that the data is communicated in plain text like stream, it does not require any data/message parsing. For exposing a WCF service as JSON enabled WCF service, we need to apply the [WebGet] attribute on the method of the ServiceContract with ResponseFormat as JSON. The binding used here is WebHttpBinding which is also used for REST enable WCF services. While exposing WCF as JSON enable service, the EndPointBehavior for the WCF service is WebHttp which generates JSON response from the WCF service.


IService1.cs : 



using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;


namespace WCF_JSONService
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        // [WebGet(UriTemplate = "/Products",ResponseFormat=WebMessageFormat.Json)]
        [WebGet(UriTemplate = "/Products", ResponseFormat = WebMessageFormat.Xml)]
        List GetAllProducts(); 
    }




    [DataContract]
    public class Product
    {
        [DataMember]
        public int ProdId { get; set; }
        [DataMember]
        public string PropName { get; set; }
        [DataMember]
        public int Quantity { get; set; }
        [DataMember]
        public int Price { get; set; }
    }
}


Service1.svc : 

using System.Collections.Generic;

namespace WCF_JSONService
{
    public class Service1 : IService1
    {
        public List GetAllProducts()
        {
            return new List
            {
                 new Product() {ProdId=101,PropName="Laptop",Quantity=200,Price=45000},
                 new Product() {ProdId=102,PropName="Desktop",Quantity=300,Price=35000},
            };
        }
    }
}

Wednesday, October 5, 2011

JavaFX 2.0 Java EE 7


JavaFX 2.0, Java 9 head JavaOne keynote


The forthcoming Java EE (Enterprise Edition) 7 release, meanwhile, has a cloud computing bent and would enable pruning of capabilities such as JAX-RX (Java API for Restful Web Services) by platform vendors if they so choose.


JavaFX, a lagging rival to Adobe Flash and Microsoft Silverlight for the consumer Web and mobile devices, is now looking to carve out a niche in Java shops and enterprise client applications. With version 2.0, JavaFX becomes a set of Java libraries and APIs.