R – Bad request 400 on sending xml request in wcf rest

restwcf

I am writing a sample application using wcf rest for authentication. Here is the snapshot of the code:

service Interface:

[ServiceContract]
public interface IAuthenticate
{
    [OperationContract]
    [WebInvoke(BodyStyle=WebMessageBodyStyle.Bare, 
Method = "POST", UriTemplate = "/VUser",RequestFormat= WebMessageFormat.Xml ), ]
    string CreateUser(VUser user);
}

Datacontract class:

[DataContract]
public class VUser
{
    public VUser()
    {
    }

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

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

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

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

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

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

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

    [DataMember]
    public int CountryCode { get; set; }
}

Service class:

public class Authenticate : IAuthenticate
{

    #region IAuthenticate members
    public string CreateUser(Vuser user)
    {
        //processing xml for response

    }
    #endregion IAuthenticate
}

client code:

       Uri baseAddress = new Uri("http://localhost:8000");

        using (WebServiceHost host = new WebServiceHost(typeof(Authenticate), baseAddress))
        {
            host.Open();
            Console.WriteLine("Press any key to terminate");
            Console.ReadLine();
            host.Close();

        }

Now I am using fiddler to send the request after host.open() and send the the request has shown:

post http://localhost:8000/Vuser/

User-Agent: Fiddler
Host: localhost:8000
content-length: 233
content-type: text/xml

and in request body :

sandy
r
sunil
sunil.r
919900101948
winter
male
01

but it is returning me HTTP/1.1 400 Bad Request. My question is am I passing the vuser class correctly to the create user method or is there any other way to send the vuser.

Please help me.

Best Answer

It could be a problem with serialization.

Serialization uses the default consrtuctor, without parameters.

In C# the compiler will automatically create a default constructor, except if you create a constructor with a parameter.

The Authenticate class is missing a default constructor, you will therefore have probelms sending it over WCF.

Related Topic