2

Here is my Interface code

      [OperationContract]
       [WebGet(RequestFormat = WebMessageFormat.Json,
         ResponseFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare,
          UriTemplate = "/Insert?customer={customer}")]
       void InsertDetailsData(CustomerClass customer);

My Class method is like

      public void InsertDetailsData(CustomerClass cust)
      {
        SqlConnection scon = new SqlConnection(myConnection);
        scon.Open();
        cust.CustomerName = "adsd";
        cust.Address = "asdasd";
        cust.FirstName = "asd";
        cust.LastName = "asda";
        string query = "Insert into TblCustomer(CustomerName,FirstName,LastName,Address)";
        query += " values('" + cust.CustomerName + "','" + cust.FirstName + "','" + cust.LastName + "','" + cust.Address + "')";

        SqlCommand cmd = new SqlCommand(query, scon);
        cmd.ExecuteNonQuery();
        scon.Close();

    }

But I am getting error like

Operation 'InsertDetailsData' in contract 'ICustomerService' has a query variable named 'customer' of type 'CustomerService.CustomerClass', but type 'CustomerService.CustomerClass' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.

So tried with

  public class MyQueryStringConverter : QueryStringConverter
    {
        public override bool CanConvert(Type type)
        {
            return (type == typeof(CustomerClass)) || base.CanConvert(type);
        }
        public override object ConvertStringToValue(string parameter, Type parameterType)
        {
            if (parameterType == typeof(CustomerClass))
            {
                string[] parts = parameter.Split(',');
                return new CustomerClass { FirstName = parts[0], CustomerName = parts[1], LastName = parts[2], Address = parts[3] };
            }
            else
            {
                return base.ConvertStringToValue(parameter, parameterType);
            }
        }
    }
    public class MyWebHttpBehavior : WebHttpBehavior
    {
        protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
        {
            return new MyQueryStringConverter();
        }
    }

Still I am facing same error.

Sheeba
  • 25
  • 2
  • 18
  • This link will help you. [passing class as parameter][1] [1]: http://stackoverflow.com/questions/6783264/passing-a-class-as-parameter-in-restful-wcf-service – Rajat_RJT Feb 19 '15 at 04:34
  • For inserting data via REST you are supposed to use PUT attribute rather than GET as per REST principals. Can you post on how you raw request looks like? – Rajesh Apr 27 '15 at 10:49

1 Answers1

0

This request is only send simple types, uritemplates make a mapped parametres in method.

[WebGet(UriTemplate = "Method/{Par1}/{par2}")]

if you want to pass class must be use POST method WebInvoke.

This msdn description

https://msdn.microsoft.com/tr-tr/library/system.servicemodel.web.webinvokeattribute%28v=vs.110%29.aspx