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.让它支持跨线程访问。

你可能感兴趣的:(silverlight)