NSwagStudio for Swagger Api

本案例主要说明如何使用NSwag 工具使用桌面工具快速生成c# 客户端代码、快速的访问Web Api。

NSwagStudio 下载地址 比较强大、可以生成TypeScript、WebApi Controller、CSharp Client 

1、运行WebApi项目  URL http://yourserver/swagger 然后你将看到界面如下

    1.1 Web API 代码

    该代码使用的是Abp框架、如果不了解Abp框架的请到官网 http://www.aspnetboilerplate.com/ 了解

  •  实休代码
       public class UserInformation : Entity<int>, IHasCreationTime, IHasModificationTime
        {
            [StringLength(20)]
            public string UserName { get; set; }
            public int UserAge { get; set; }
            [StringLength(20)]
            public string UserAddress { get; set; }
    
            public UserInformation()
            {
                CreationTime = Clock.Now;
            }
            public DateTime CreationTime { get; set; }
            public DateTime? LastModificationTime { get; set; }
        }
    实体代码
  •   Application 应用
     [AutoMapTo(typeof(UserInformation))]
        public class CreateUserInformationDto : IHasCreationTime
        {
            public string UserName { get; set; }
            public int UserAge { get; set; }
    
            public string UserAddress { get; set; }
            public DateTime CreationTime { get; set; }
            public CreateUserInformationDto()
            {
                CreationTime = Clock.Now;
            }
        }
    
     public class DeleteUserInformationDto : IEntityDto
        {
            public int Id { get; set; }
        }
    
      public class GetAllUserInformationDto : PagedAndSortedResultRequestDto, ICustomValidate
        {
            public string UserName { get; set; }
            public void AddValidationErrors(CustomValidationContext context)
            {
                if (string.IsNullOrEmpty(UserName))
                {
                    context.Results.Add(new ValidationResult("用户名称关键字不能为空!"));
                }
            }
        }
    
      public class GetInputUserInformatinDto : IEntityDto
        {
            public int Id { get; set; }
        }
    
    [AutoMapTo(typeof(UserInformation))]
        public class UpdateUserInformationDto : CreateUserInformationDto, IEntityDto, IHasModificationTime
        {
    
            public UpdateUserInformationDto()
            {
                LastModificationTime = Clock.Now;
            }
    
            public DateTime? LastModificationTime { get; set; }
            public int Id { get; set; }
        }
    
     public class UserInformationDto : EntityDto, IHasCreationTime, IHasModificationTime
        {
    
            public string UserName { get; set; }
            public int UserAge { get; set; }
    
            public string UserAddress { get; set; }
    
            public UserInformationDto()
            {
                CreationTime = Clock.Now;
            }
            public DateTime CreationTime { get; set; }
            public DateTime? LastModificationTime { get; set; }
        }
    Dtos
      public interface IUserAppService :
            ICrudAppService<UserInformationDto, int,
            GetAllUserInformationDto,
            CreateUserInformationDto,
            UpdateUserInformationDto,
            GetInputUserInformatinDto,
            DeleteUserInformationDto>
        {
    
        }
    
    
       public class UserAppService :
            CrudAppService
            <UserInformation,
            UserInformationDto, int,
            GetAllUserInformationDto,
            CreateUserInformationDto,
            UpdateUserInformationDto,
            GetInputUserInformatinDto,
            DeleteUserInformationDto>,
            IUserAppService
        {
            private ICacheManager cacheManager;
            public UserAppService(IRepository<UserInformation, int> repository,ICacheManager cache) : base(repository)
            {
                cacheManager = cache;
            }
    
               protected override IQueryable<UserInformation> CreateFilteredQuery(GetAllUserInformationDto input)
               {
                   return base.CreateFilteredQuery(input).Where(o=>o.UserName.Contains(input.UserName));
               }
           }
    Application Service

  2、生成客户端代码 并且访问Web API

   2.1 安装工具和创建测试客户端项目

  • 安装Install NSwagStudio
  • 创建一个新的c#客户端项目
  • 将所需的程序集依赖项添加到库项目中

   2.2 生成代码

  • 启动 NSwagStudio 然后选择 Swagger Specification
  • 在Load Swagger Specification from URL: http://yourserver/swagger/v1/swagger.json  前期条件是服务必须期启动
  • 选择一个右边的选项卡 如:"CSharpClient"、然后点击“ Generate Outputs”
  • 复制生所的代码到你的客户端项目中
  • 同时也可以设置、如项目命名空间、以及配置输出文件路经、生成DTO的一些配置

     

  3、保存脚本

  • 保存文件.nswag 把当前的一些配置保存

          

 4、 客户端代码实现

 2                 UserClient app = new UserClient();
 3                 var data = app.GetAllAsync(new GetAllUserInformationDto()
 4                 {
 5                     MaxResultCount = 1,
 6                     SkipCount = 1,
 7                     Sorting = "desc",
 8                     UserName = "luyong"
 9                 });
10 
11                 //新增数据
12                var app2= app.CreateAsync(new CreateUserInformationDto()
13                 {
14                     CreationTime = DateTime.Now,
15                     UserAddress = "china",
16                     UserName = "fadf333",
17                     UserAge = 10
18                 });
19 
20                 dataGridView1.DataSource = data.Result.Items;

 

posted @ 2016-10-20 10:01  在 水 一 方  阅读(6684)  评论(6编辑  收藏  举报