EasyText, EasyLicense 的作者, https://github.com/EasyHelper Good Good Study,Day Day Up.

 

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

posted @   LoveJenny  阅读(2244)  评论(2编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
EasyText, EasyLicense 的作者, https://github.com/EasyHelper Good Good Study,Day Day Up.

 

点击右上角即可分享
微信分享提示