如何调用delete / put方法从silverlight中休息服务

时间:2013-02-25 13:22:38

标签: c# wcf silverlight rest

我在休息服务中收到很多关于post和get方法的文章但我没有得到任何好的put / delete。我创建了一个休息服务并试图调用四个操作。获取和发布工作但不删除/删除。我会在这里提供我的代码;

在silverlight put和post方法中调用休息服务

 const string uridel = 
              "http://localhost:50211/CustomerService.svc/deletecustomer/1";
 const string uriput = 
              "http://localhost:50211/CustomerService.svc/modifycustomer/1";

client.DownloadStringCompleted += (s, ev) =>//delete method
        {
            XDocument xml = XDocument.Parse(ev.Result);

            var Customer = from results in xml.Descendants
                                               ("CustomerResponse")
                           select new CustomerResponse
                           {
                               CustomerId = Int32.Parse(results.Descendants
                                               ("CustomerId").First().Value),
                           };


            int id = Customer.Select(w => w.CustomerId).FirstOrDefault();
            MessageBox.Show("result is :" + id);
        };
        client.DownloadStringAsync(new Uri(uridel), "DELETE");


 CustomerResponse cusres = new CustomerResponse();//put method
        cusres.CustomerName = textBox1.Text;
        cusres.CustomerPh = textBox2.Text;
        DataContractSerializer dataContractSerializer = 
                          new DataContractSerializer(typeof(CustomerResponse));
        MemoryStream memoryStream = new MemoryStream();
        dataContractSerializer.WriteObject(memoryStream, cusres);
        string xmlData = Encoding.UTF8.GetString(memoryStream.ToArray(), 0, 
                                                (int)memoryStream.Length);

        client.UploadStringCompleted += (s, ev) =>
        {
            XDocument xml = XDocument.Parse(ev.Result);

            var Customer = from results in xml.Descendants("CustomerResponse")
                           select new CustomerResponse
                           {

                               CustomerId = Int32.Parse(results.Descendants
                                               ("CustomerId").First().Value),
                           };

            int id = Customer.Select(w => w.CustomerId).FirstOrDefault();
            MessageBox.Show("result is :" + id);
            textBox1.Text = "";
            textBox2.Text = "";
        };
        client.Headers[HttpRequestHeader.ContentType] = "application/xml";
        client.UploadStringAsync(new Uri(uriput), "PUT", xmlData);

在wcf服务中

    [OperationContract]
    [WebInvoke(Method = "DELETE",
               RequestFormat = WebMessageFormat.Xml,
               ResponseFormat = WebMessageFormat.Xml,
               BodyStyle = WebMessageBodyStyle.Bare,
               UriTemplate = "deletecustomer/{id}")]
    CustomerResponse DeleteCustomer(string id);


 [OperationContract]
    [WebInvoke(Method = "PUT",
               RequestFormat = WebMessageFormat.Xml,
               ResponseFormat = WebMessageFormat.Xml,
               BodyStyle = WebMessageBodyStyle.Bare,
               UriTemplate = "modifycustomer/{id}")]
    CustomerResponse ModifyCustomer(string id,CustomerResponse cusres);

对于删除,我得到一个异常,即找不到服务器,而对于put方法,我得到的错误就像指定的方法在这个请求中不支持。任何人都可以建议错误在哪里..或者建议可以使用put和delete方法在Silverlight中使用的好文章?

1 个答案:

答案 0 :(得分:1)

第1步: 在Silverlight应用程序中,检查是否在App()构造函数的App.xaml.cs中添加了以下行。

HttpWebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
HttpWebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);

第2步:

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
   <policy>
     <allow-from http-request-headers="*" http-methods="*">       
       <domain uri="*"/>
     </allow-from>
    <grant-to>
      <resource path="/" include-subpaths="true"/>
    </grant-to>
   </policy>
 </cross-domain-access>
</access-policy>

将xml保存为服务主机项目下的“clientaccesspolicy.xml”。