WCF POST方法无效

时间:2018-03-14 14:03:53

标签: c# json wcf fiddler

我有一个WCF网络服务。我想使用IIS将一些值发布到MS SQL数据库中。 GET方法有效。但我找不到我在POST方法中出错的地方。有谁帮助我,我的错误在哪里...谢谢大家。这是我的代码......

    [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;
    }

1 个答案:

答案 0 :(得分:0)

SOLVED->我将“BodyStyle = WebMessageBodyStyle.Bare”改为“BodyStyle = WebMessageBodyStyle.Wrapped”,它对我有用......谢谢大家...