开发者

Bad request 400 on sending xml request in wcf rest

开发者 https://www.devze.com 2022-12-19 00:19 出处:网络
I am writing a sample application using wcf rest for authentication. Here is the snapshot of the code:

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.


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.


Kindly specify The Datacontract Namespace in DataContract Class

[DataContract(Namespace = "http://xxx.xxx.xxx/Service.svc")]

and follow same in Xml file


The Namespace in both client and server should match. Try to add namespace name as

[DataContract(Namespace = "http://sample.com")]
public class VUser

in the server contract. And then make sure the xml string has the xmlns value with the same namespace

"<VUser xmlns=\"http://sample.com" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">...</VUser>"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号