zoukankan      html  css  js  c++  java
  • Silverlight_Rest_WCF系列之五:RestInvoker的使用

    在上篇文章中我们封装了Rest请求,下面我将做一些demo给大家演示RestInvoker怎么使用。

    首先是服务契约代码:

    这里注意下CreateByIdAndName方法,因为有两个参数,所以bodyStyle选择wrappedRequest.也就是对Request进行Wrapped的意思。

    Wrapped的效果就是Json的格式会不一致。

    View Code
    [ServiceContract]
        [ServiceKnownType(
    typeof(Product))]
        
    public interface IRestService
        {
            [OperationContract]
            [WebGet(UriTemplate 
    = "Products", BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat=WebMessageFormat.Json)]
            List
    <Product> Query();

            [OperationContract]
            [WebInvoke(UriTemplate 
    = "Products", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
            Product Create(Product product);

            [OperationContract]
            [WebInvoke(UriTemplate
    ="ProductsByIdAndName",Method="POST",BodyStyle=WebMessageBodyStyle.WrappedRequest, ResponseFormat=WebMessageFormat.Json)]
            Product CreateByIdAndName(Guid id, 
    string name);

            [OperationContract]
            [WebInvoke(UriTemplate 
    = "Products", Method = "PUT")]
            Product Update(Product product);

            [OperationContract]
            [WebInvoke(UriTemplate 
    = "Products", Method = "DELETE")]
            Product Delete(Product product);
        }

    服务类:

    View Code
    public class RestService : IRestService
        {
            
    public List<Product> Query()
            {
                
    return SampleData.Datas;
            }

            
    public Product Create(Product product)
            {
                
    return new Product() { ID = product.ID, Name = product.Name + "PostServer" };
            }

            
    public Product CreateByIdAndName(Guid id, string name)
            {
                
    return new Product() { ID = id, Name = name + "CreateByIdAndName" };
            }

            
    public Product Update(Product product)
            {
                
    return new Product() { ID = product.ID, Name = product.Name + "PutServer" };
            }

            
    public Product Delete(Product product)
            {
                
    return new Product() { ID = product.ID, Name = product.Name + "DeleteServer" };
            }
        }

    1:调用Get,Get对应的是Query方法。

    具体代码如下:

    RestInvoker.InvokeGet<Product[]>("http://localhost:18677/RestService.svc/Products",
                    (datas) 
    =>
                    {
                        
    this.Dispatcher.BeginInvoke(() => {
                            MessageBox.Show(datas[
    0].Name);
                        });
                    });

    2:调用Post,Post

    Product product = new Product() { ID = Guid.NewGuid(), Name = "555" };
                RestInvoker.InvokePost
    <Product, Product>("http://localhost:18677/RestService.svc/Products",
                    product, (resultProduct) 
    =>
                    {
                        
    this.Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show(resultProduct.Name);
                        });
                    });

    3:调用Post方法,对应的方法是:

    public Product CreateByIdAndName(Guid id, string name)
            {
                
    return new Product() { ID = id, Name = name + "CreateByIdAndName" };
            }

    因为RestInvoker支持匿名类和JsonObject,所以可以像这样的调用服务。

    var data = new { id = Guid.NewGuid(), name = "testIdName" };
                
    //JsonObject jo = new JsonObject();
                
    //jo["id"] = Guid.NewGuid();
                
    //jo["name"] = "testIdName";

                RestInvoker.InvokePost(
    "http://localhost:18677/RestService.svc/ProductsByIdAndName",data
                    , 
    new Action<string>((result) => 
                    {
                        
    this.Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show(result);
                        });
                    }));

    调用Put和Delete的方法和Post一致,区别是InvokePut,InvokeDelete.

    这里大家可以看到因为不支持跨线程,所以我们调用了this.Dispatcher.BeginInvoke.

    虽然解决了问题,但是很不优雅,下篇文章就会完善RestInvoker.让它支持跨线程访问。

  • 相关阅读:
    CDH6.2安装之离线方式
    impala
    Oracle
    性能调优之Mapping
    Informatica
    性能瓶颈之System
    性能瓶颈之Session
    本地Oracle客户端11g升级12c导致PowerCenter无法连接ODBC数据源
    性能瓶颈之Mapping
    性能瓶颈之Source
  • 原文地址:https://www.cnblogs.com/LoveJenny/p/2047103.html
Copyright © 2011-2022 走看看