Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从silverlight调用rest服务的delete/put方法_C#_Wcf_Silverlight_Rest - Fatal编程技术网

C# 如何从silverlight调用rest服务的delete/put方法

C# 如何从silverlight调用rest服务的delete/put方法,c#,wcf,silverlight,rest,C#,Wcf,Silverlight,Rest,我在rest服务中收到了很多关于post和get方法的文章,但我没有收到任何关于put/delete的好文章。我创建了一个rest服务,并尝试调用四个操作。获取和发布工作,但不放置/删除。我会在这里提供我的代码 在silverlight中调用rest服务的put和post方法 const string uridel = "http://localhost:50211/CustomerService.svc/deletecustomer/1"; const str

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

在silverlight中调用rest服务的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);
对于delete,我得到了一个服务器找不到的异常,对于put方法,我得到了一个错误,比如在这个请求中不支持指定的方法。是否有人可以建议错误在哪里..或者建议可以在silverlight中使用put和delete方法消费的好文章?

步骤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”