简体   繁体   中英

WCF POST Method is not working

I have a WCF web service. I want to post some values into MS SQL database using IIS. GET method works. But i can not find where i have mistakes in my POST method. Does anyone help me about where are my mistakes... Thanks all. Here are my codes...

    [OperationContract] //Interface
    [WebInvoke(Method = "POST", UriTemplate = "AddName", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
   int AddName(uName name);

     [DataContract]
   public class uName // a class in Interface
    {
        string name;
        [DataMember]
        public string setGetname
        {
            get { return name; }
            set { name = value; }
        }
    }

public int AddName(uName name) //My POST method in service class
    {

        int state = 0;
        SqlConnection connection = new SqlConnection(this.conStr);
        SqlCommand cmd = new SqlCommand();
        try
        {

            if (connection.State == System.Data.ConnectionState.Closed)
            {
                connection.Open();

            }
            cmd = new SqlCommand("Insert into Table_1(kolon1) values(@name)", connection);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Parameters.AddWithValue("@name", name.setGetname);
            cmd.ExecuteReader();
            state = 1;

        }
        catch (Exception ex)
        {
            throw ex;

        }
        finally
        {
            connection.Close();
            cmd.Dispose();
        }


        return state;
    }

求助->我更改为“ BodyStyle = WebMessageBodyStyle.Bare”更改为“ BodyStyle = WebMessageBodyStyle.Wrapped”,它对我有用...谢谢...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM