2

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

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

 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 方法,我遇到了错误,例如此请求不支持指定的方法。任何人都可以建议错误在哪里..或者建议可以在silverlight中使用put和delete方法的好文章?

4

1 回答 1

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”。

于 2014-01-09T17:02:53.517 回答