您好,登錄后才能下訂單哦!
如果你對Web API已比較了解則可以跳過本篇直接看單元測試部分。
創建一個叫 UnitTesingWebAPI的空白的解決方案,并包含下列項目:
UnitTestingWebAPI.Domain:類庫(包含 Entity Models)
UnitTestingWebAPI.Data:類庫(包含 Repositories)
UnitTestingWebAPI.Services:類庫(包含 Services)
UnitTestingWebAPI.API.Core:類庫(包含WebAPI組件,例如:Controllers, Filters, Massage Handlers)
UnitTestingWebAPI.API:空的ASP.NET Web Application(Web程序去管控(host) WebAPI
UnitTestingWebAPI.Tests:類庫(包含單元測試)
Domain 層:
Articles.cs
public class Article { public int ID { get; set; } public string Title { get; set; } public string Contents { get; set; } public string Author { get; set; } public string URL { get; set; } public DateTime DateCreated { get; set; } public DateTime DateEdited { get; set; } public int BlogID { get; set; } public virtual Blog Blog { get; set; } public Article() { } }
Blog.cs
public class Blog { public int ID { get; set; } public string Name { get; set; } public string URL { get; set; } public string Owner { get; set; } public DateTime DateCreated { get; set; } public virtual ICollection<Article> Articles { get; set; } public Blog() { Articles = new HashSet<Article>(); } }
Respository 層:
為UnitTestingWebAPI.Data安裝Entity Framework,有兩種方式:
1:在UnitTestingWebAPI.Data上右鍵,點擊管理Nuget包,選擇對話窗口的左邊選擇在線包,找到EF進行安裝
2:命令行:
install-package EntityFramework
注:一定要注意我用紅色圈住的地方。
添加下面的類:
Configurations/ArticleConfiguration.cs
public class ArticleConfiguration : EntityTypeConfiguration<Article> { public ArticleConfiguration() { ToTable("Article"); Property(a => a.Title).IsRequired().HasMaxLength(100); Property(a => a.Contents).IsRequired(); Property(a => a.Author).IsRequired().HasMaxLength(50); Property(a => a.URL).IsRequired().HasMaxLength(200); Property(a => a.DateCreated).HasColumnType("datetime2"); Property(a => a.DateEdited).HasColumnType("datetime2"); } }
Configurations/BlogConfiguration.cs
public class BlogConfiguration : EntityTypeConfiguration<Blog> { public BlogConfiguration() { ToTable("Blog"); Property(b => b.Name).IsRequired().HasMaxLength(100); Property(b => b.URL).IsRequired().HasMaxLength(200); Property(b => b.Owner).IsRequired().HasMaxLength(50); Property(b => b.DateCreated).HasColumnType("datetime2"); } }
Configurations/BloggerEntities
public class BloggerEntities : DbContext { public BloggerEntities() : base("BloggerEntities") { Configuration.ProxyCreationEnabled = false; } public DbSet<Blog> Blogs { get; set; } public DbSet<Article> Articles { get; set; } public virtual void Commit() { base.SaveChanges(); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new ArticleConfiguration()); modelBuilder.Configurations.Add(new BlogConfiguration()); } }
Configurations/BloggerInitializer
public class BloggerInitializer : DropCreateDatabaseIfModelChanges<BloggerEntities> { protected override void Seed(BloggerEntities context) { GetBlogs().ForEach(b => context.Blogs.Add(b)); context.Commit(); } public static List<Blog> GetBlogs() { List<Blog> _blogs = new List<Blog>(); // Add two Blogs Blog _chsakellsBlog = new Blog() { Name = "chsakell's Blog", URL = "https://chsakell.com/", Owner = "Chris Sakellarios", Articles = GetChsakellsArticles() }; Blog _dotNetCodeGeeks = new Blog() { Name = "DotNETCodeGeeks", URL = "dotnetcodegeeks", Owner = ".NET Code Geeks", Articles = GetDotNETGeeksArticles() }; _blogs.Add(_chsakellsBlog); _blogs.Add(_dotNetCodeGeeks); return _blogs; } public static List<Article> GetChsakellsArticles() { List<Article> _articles = new List<Article>(); Article _oData = new Article() { Author = "Chris S.", Title = "ASP.NET Web API feat. OData", URL = "https://chsakell.com/2015/04/04/asp-net-web-api-feat-odata/", Contents = @"OData is an open standard protocol allowing the creation and consumption of queryable and interoperable RESTful APIs. It was initiated by Microsoft and it’s mostly known to .NET Developers from WCF Data Services. There are many other server platforms supporting OData services such as Node.js, PHP, Java and SQL Server Reporting Services. More over, Web API also supports OData and this post will show you how to integrate those two.." }; Article _wcfCustomSecurity = new Article() { Author = "Chris S.", Title = "Secure WCF Services with custom encrypted tokens", URL = "https://chsakell.com/2014/12/13/secure-wcf-services-with-custom-encrypted-tokens/", Contents = @"Windows Communication Foundation framework comes with a lot of options out of the box, concerning the security logic you will apply to your services. Different bindings can be used for certain kind and levels of security. Even the BasicHttpBinding binding supports some types of security. There are some times though where you cannot or don’t want to use WCF security available options and hence, you need to develop your own authentication logic accoarding to your business needs." }; _articles.Add(_oData); _articles.Add(_wcfCustomSecurity); return _articles; } public static List<Article> GetDotNETGeeksArticles() { List<Article> _articles = new List<Article>(); Article _angularFeatWebAPI = new Article() { Author = "Gordon Beeming", Title = "AngularJS feat. Web API", URL = "http://www.dotnetcodegeeks.com/2015/05/angularjs-feat-web-api.html", Contents = @"Developing Web applications using AngularJS and Web API can be quite amuzing. You can pick this architecture in case you have in mind a web application with limitted page refreshes or post backs to the server while each application’s View is based on partial data retrieved from it." }; _articles.Add(_angularFeatWebAPI); return _articles; } public static List<Article> GetAllArticles() { List<Article> _articles = new List<Article>(); _articles.AddRange(GetChsakellsArticles()); _articles.AddRange(GetDotNETGeeksArticles()); return _articles; } }
Infrastructure/Disposable.cs
public class Disposable : IDisposable { private bool isDisposed; ~Disposable() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!isDisposed && disposing) { DisposeCore(); } isDisposed = true; } // Ovveride this to dispose custom objects protected virtual void DisposeCore() { } }
Infrastructure/IDbFactory.cs
public interface IDbFactory : IDisposable { BloggerEntities Init(); }
Infrastructure/DbFactory.cs
public class DbFactory : Disposable, IDbFactory { BloggerEntities dbContext; public BloggerEntities Init() { return dbContext ?? (dbContext = new BloggerEntities()); } protected override void DisposeCore() { if (dbContext != null) dbContext.Dispose(); } }
Infrastrure/IRepository.cs
public interface IRepository<T> where T : class { // Marks an entity as new void Add(T entity); // Marks an entity as modified void Update(T entity); // Marks an entity to be removed void Delete(T entity); void Delete(Expression<Func<T, bool>> where); // Get an entity by int id T GetById(int id); // Get an entity using delegate T Get(Expression<Func<T, bool>> where); // Gets all entities of type T IEnumerable<T> GetAll(); // Gets entities using delegate IEnumerable<T> GetMany(Expression<Func<T, bool>> where); }
Infrastructure/RepositoryBase.cs
public abstract class RepositoryBase<T> where T : class { #region Properties private BloggerEntities dataContext; private readonly IDbSet<T> dbSet; protected IDbFactory DbFactory { get; private set; } protected BloggerEntities DbContext { get { return dataContext ?? (dataContext = DbFactory.Init()); } } #endregion protected RepositoryBase(IDbFactory dbFactory) { DbFactory = dbFactory; dbSet = DbContext.Set<T>(); } #region Implementation public virtual void Add(T entity) { dbSet.Add(entity); } public virtual void Update(T entity) { dbSet.Attach(entity); dataContext.Entry(entity).State = EntityState.Modified; } public virtual void Delete(T entity) { dbSet.Remove(entity); } public virtual void Delete(Expression<Func<T, bool>> where) { IEnumerable<T> objects = dbSet.Where<T>(where).AsEnumerable(); foreach (T obj in objects) dbSet.Remove(obj); } public virtual T GetById(int id) { return dbSet.Find(id); } public virtual IEnumerable<T> GetAll() { return dbSet.ToList(); } public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where) { return dbSet.Where(where).ToList(); } public T Get(Expression<Func<T, bool>> where) { return dbSet.Where(where).FirstOrDefault<T>(); } #endregion }
Infrastrure/IUnitOfWork.cs
public interface IUnitOfWork { void Commit(); }
Infrastrure/UnitOfWork.cs
public class UnitOfWork : IUnitOfWork { private readonly IDbFactory dbFactory; private BloggerEntities dbContext; public UnitOfWork(IDbFactory dbFactory) { this.dbFactory = dbFactory; } public BloggerEntities DbContext { get { return dbContext ?? (dbContext = dbFactory.Init()); } } public void Commit() { DbContext.Commit(); } }
Infrastructure/BlogRepository.cs
public class BlogRepository : RepositoryBase<Blog>, IBlogRepository { public BlogRepository(IDbFactory dbFactory) : base(dbFactory) { } public Blog GetBlogByName(string blogName) { var _blog = this.DbContext.Blogs.Where(b => b.Name == blogName).FirstOrDefault(); return _blog; } } public interface IBlogRepository : IRepository<Blog> { Blog GetBlogByName(string blogName); }
Repositories/ArticleRepository.cs
public class ArticleRepository : RepositoryBase<Article>, IArticleRepository { public ArticleRepository(IDbFactory dbFactory) : base(dbFactory) { } public Article GetArticleByTitle(string articleTitle) { var _article = this.DbContext.Articles.Where(b => b.Title == articleTitle).FirstOrDefault(); return _article; } } public interface IArticleRepository : IRepository<Article> { Article GetArticleByTitle(string articleTitle); }
Service 層
到UnitTestingWebAPI.Service項目上,添加對UnitTestingWebAPI.Domain, UnitTestingWebAPI.Data的引用,并添加下列文件:
ArticleService.cs
// operations you want to expose public interface IArticleService { IEnumerable<Article> GetArticles(string name = null); Article GetArticle(int id); Article GetArticle(string name); void CreateArticle(Article article); void UpdateArticle(Article article); void DeleteArticle(Article article); void SaveArticle(); } public class ArticleService : IArticleService { private readonly IArticleRepository articlesRepository; private readonly IUnitOfWork unitOfWork; public ArticleService(IArticleRepository articlesRepository, IUnitOfWork unitOfWork) { this.articlesRepository = articlesRepository; this.unitOfWork = unitOfWork; } #region IArticleService Members public IEnumerable<Article> GetArticles(string title = null) { if (string.IsNullOrEmpty(title)) return articlesRepository.GetAll(); else return articlesRepository.GetAll().Where(c => c.Title.ToLower().Contains(title.ToLower())); } public Article GetArticle(int id) { var article = articlesRepository.GetById(id); return article; } public Article GetArticle(string title) { var article = articlesRepository.GetArticleByTitle(title); return article; } public void CreateArticle(Article article) { articlesRepository.Add(article); } public void UpdateArticle(Article article) { articlesRepository.Update(article); } public void DeleteArticle(Article article) { articlesRepository.Delete(article); } public void SaveArticle() { unitOfWork.Commit(); } #endregion }
BlogService.cs
// operations you want to expose public interface IBlogService { IEnumerable<Blog> GetBlogs(string name = null); Blog GetBlog(int id); Blog GetBlog(string name); void CreateBlog(Blog blog); void UpdateBlog(Blog blog); void SaveBlog(); void DeleteBlog(Blog blog); } public class BlogService : IBlogService { private readonly IBlogRepository blogsRepository; private readonly IUnitOfWork unitOfWork; public BlogService(IBlogRepository blogsRepository, IUnitOfWork unitOfWork) { this.blogsRepository = blogsRepository; this.unitOfWork = unitOfWork; } #region IBlogService Members public IEnumerable<Blog> GetBlogs(string name = null) { if (string.IsNullOrEmpty(name)) return blogsRepository.GetAll(); else return blogsRepository.GetAll().Where(c => c.Name == name); } public Blog GetBlog(int id) { var blog = blogsRepository.GetById(id); return blog; } public Blog GetBlog(string name) { var blog = blogsRepository.GetBlogByName(name); return blog; } public void CreateBlog(Blog blog) { blogsRepository.Add(blog); } public void UpdateBlog(Blog blog) { blogsRepository.Update(blog); } public void DeleteBlog(Blog blog) { blogsRepository.Delete(blog); } public void SaveBlog() { unitOfWork.Commit(); } #endregion }
Web API Core 組件
在UnitTestingWebAPI.API.Core 上添加 UnitTestingWebAPI.API.Domain 和UnitTestingWebAPI.Service 項目,并安裝下面的包(方法和前面Resporities層一樣):
Entity Framework
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.Client
添加下面的Web API Controller到 Controller 文件夾中:
Controllers/ArticlesController.cs
public class ArticlesController : ApiController { private IArticleService _articleService; public ArticlesController(IArticleService articleService) { _articleService = articleService; } // GET: api/Articles public IEnumerable<Article> GetArticles() { return _articleService.GetArticles(); } // GET: api/Articles/5 [ResponseType(typeof(Article))] public IHttpActionResult GetArticle(int id) { Article article = _articleService.GetArticle(id); if (article == null) { return NotFound(); } return Ok(article); } // PUT: api/Articles/5 [ResponseType(typeof(void))] public IHttpActionResult PutArticle(int id, Article article) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != article.ID) { return BadRequest(); } _articleService.UpdateArticle(article); try { _articleService.SaveArticle(); } catch (DbUpdateConcurrencyException) { if (!ArticleExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); } // POST: api/Articles [ResponseType(typeof(Article))] public IHttpActionResult PostArticle(Article article) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _articleService.CreateArticle(article); return CreatedAtRoute("DefaultApi", new { id = article.ID }, article); } // DELETE: api/Articles/5 [ResponseType(typeof(Article))] public IHttpActionResult DeleteArticle(int id) { Article article = _articleService.GetArticle(id); if (article == null) { return NotFound(); } _articleService.DeleteArticle(article); return Ok(article); } private bool ArticleExists(int id) { return _articleService.GetArticle(id) != null; } }
Controllers/BlogsController.cs
public class BlogsController : ApiController { private IBlogService _blogService; public BlogsController(IBlogService blogService) { _blogService = blogService; } // GET: api/Blogs public IEnumerable<Blog> GetBlogs() { return _blogService.GetBlogs(); } // GET: api/Blogs/5 [ResponseType(typeof(Blog))] public IHttpActionResult GetBlog(int id) { Blog blog = _blogService.GetBlog(id); if (blog == null) { return NotFound(); } return Ok(blog); } // PUT: api/Blogs/5 [ResponseType(typeof(void))] public IHttpActionResult PutBlog(int id, Blog blog) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != blog.ID) { return BadRequest(); } _blogService.UpdateBlog(blog); try { _blogService.SaveBlog(); } catch (DbUpdateConcurrencyException) { if (!BlogExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); } // POST: api/Blogs [ResponseType(typeof(Blog))] public IHttpActionResult PostBlog(Blog blog) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _blogService.CreateBlog(blog); return CreatedAtRoute("DefaultApi", new { id = blog.ID }, blog); } // DELETE: api/Blogs/5 [ResponseType(typeof(Blog))] public IHttpActionResult DeleteBlog(int id) { Blog blog = _blogService.GetBlog(id); if (blog == null) { return NotFound(); } _blogService.DeleteBlog(blog); return Ok(blog); } private bool BlogExists(int id) { return _blogService.GetBlog(id) != null; } }
在有需要時添加下面的過濾器,它會反轉Articles list的順序:
Filters/ArticlesReversedFilter.cs
public class ArticlesReversedFilter : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { var objectContent = actionExecutedContext.Response.Content as ObjectContent; if (objectContent != null) { List<Article> _articles = objectContent.Value as List<Article>; if (_articles != null && _articles.Count > 0) { _articles.Reverse(); } } } }
當添加下面的媒體類型格式化器,可以返回一個用逗號分割來展示的文章列表:
MediaTypeFormatters/ArticleFormatter.cs
public class ArticleFormatter : BufferedMediaTypeFormatter { public ArticleFormatter() { SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/article")); } public override bool CanReadType(Type type) { return false; } public override bool CanWriteType(Type type) { //for single article object if (type == typeof(Article)) return true; else { // for multiple article objects Type _type = typeof(IEnumerable<Article>); return _type.IsAssignableFrom(type); } } public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content) { using (StreamWriter writer = new StreamWriter(writeStream)) { var articles = value as IEnumerable<Article>; if (articles != null) { foreach (var article in articles) { writer.Write(String.Format("[{0},\"{1}\",\"{2}\",\"{3}\",\"{4}\"]", article.ID, article.Title, article.Author, article.URL, article.Contents)); } } else { var _article = value as Article; if (_article == null) { throw new InvalidOperationException("Cannot serialize type"); } writer.Write(String.Format("[{0},\"{1}\",\"{2}\",\"{3}\",\"{4}\"]", _article.ID, _article.Title, _article.Author, _article.URL, _article.Contents)); } } } }
添加下面兩個 消息 處理器,第一個負責Response中添加定制 header,第二個可以決定這個請求是否被接受:
MessageHandler/HeaderAppenderHandler.cs
public class HeaderAppenderHandler : DelegatingHandler { async protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { HttpResponseMessage response = await base.SendAsync(request, cancellationToken); response.Headers.Add("X-WebAPI-Header", "Web API Unit testing in chsakell's blog."); return response; } }
HeaderAppenderHandler/EndRequestHandler.cs
public class EndRequestHandler : DelegatingHandler { async protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { if (request.RequestUri.AbsoluteUri.Contains("test")) { var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("Unit testing message handlers!") }; var tsc = new TaskCompletionSource<HttpResponseMessage>(); tsc.SetResult(response); return await tsc.Task; } else { return await base.SendAsync(request, cancellationToken); } } }
添加下面被用于從Web 應用程序中注冊Controller 的 DefaultAssembliesResolver
CustomAssembliesResolver.cs
public class CustomAssembliesResolver : DefaultAssembliesResolver { public override ICollection<Assembly> GetAssemblies() { var baseAssemblies = base.GetAssemblies().ToList(); var assemblies = new List<Assembly>(baseAssemblies) { typeof(BlogsController).Assembly }; baseAssemblies.AddRange(assemblies); return baseAssemblies.Distinct().ToList(); } }
Asp.NET Web Application
添加 UnitTestingWebAPI.API Web應用項目,并且添加引用 UnitTestingWebAPI.Core, UnitTestingWebAPI.Data 和 UnitTestingWebAPI.Service,同樣需要安裝下列組件包:
Entity Framework
Microsoft.AspNet.WebApi.WebHost
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Owin
Microsoft.Owin.Host.SystemWeb
Microsoft.Owin
Autofac.WebApi2
在Global配置文件(如果沒有就新增一個)中配置初始化數據庫配置
Global.asax
protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); // Init database System.Data.Entity.Database.SetInitializer(new BloggerInitializer()); }
同樣要記得添加一個相關的Connection String在Web.config文件中.
<connectionStrings> <add name="BloggerDbConnStr" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=BloggerDB;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
注冊外部Controller
在Web Application的根目錄創建一個 Owin Startup.cs 文件并且粘貼下面的代碼,在(autofac configration)需要的時候,這部分代碼會確保 UnitTestingWebAPI.API.Core(CustomAssembliesResolver) 項目正確使用WebApi Controller和注入合適的倉庫以及服務.
Startup.cs
public class Startup { public void Configuration(IAppBuilder appBuilder) { var config = new HttpConfiguration(); config.Services.Replace(typeof(IAssembliesResolver), new CustomAssembliesResolver()); config.Formatters.Add(new ArticleFormatter()); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // Autofac configuration var builder = new ContainerBuilder(); builder.RegisterApiControllers(typeof(BlogsController).Assembly); builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest(); builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerRequest(); //Repositories builder.RegisterAssemblyTypes(typeof(BlogRepository).Assembly) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces().InstancePerRequest(); // Services builder.RegisterAssemblyTypes(typeof(ArticleService).Assembly) .Where(t => t.Name.EndsWith("Service")) .AsImplementedInterfaces().InstancePerRequest(); IContainer container = builder.Build(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); appBuilder.UseWebApi(config); } }
在這個時候,你應該可以啟動Web應用程序并且使用下面的請求來獲取article或blogs(這里的端口可能不一致):
http://localhost:57414/api/Articles
http://localhost:57414/api/Blogs
同樣附上原文:chsakell's Blog
文章中的源碼: http://down.51cto.com/data/2243634
到此為止,WebAPI部分介紹的差不多了,有問題請留言.
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。