本案例主要說明如何使用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; } }
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)); } }
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;