真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

ASP.NETWebAPI單元測試-WebAPI簡單介紹

如果你對Web API已比較了解則可以跳過本篇直接看單元測試部分。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供靖邊企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、成都網(wǎng)站制作、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為靖邊眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

創(chuàng)建一個叫 UnitTesingWebAPI的空白的解決方案,并包含下列項目:

  1. UnitTestingWebAPI.Domain:類庫(包含 Entity Models)

  2. UnitTestingWebAPI.Data:類庫(包含 Repositories)

  3. UnitTestingWebAPI.Services:類庫(包含 Services)

  4. UnitTestingWebAPI.API.Core:類庫(包含WebAPI組件,例如:Controllers, Filters, Massage Handlers)

  5. UnitTestingWebAPI.API:空的ASP.NET Web Application(Web程序去管控(host) WebAPI

  6. 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
 Articles { get; set; }     public Blog()     {         Articles = new HashSet
();     } }

Respository 層
為UnitTestingWebAPI.Data安裝Entity Framework,有兩種方式:

1:在UnitTestingWebAPI.Data上右鍵,點擊管理Nuget包,選擇對話窗口的左邊選擇在線包,找到EF進(jìn)行安裝

ASP.NET Web API 單元測試 - Web API 簡單介紹

2:命令行:

install-package EntityFramework

ASP.NET Web API 單元測試 - Web API 簡單介紹

注:一定要注意我用紅色圈住的地方。

添加下面的類:

Configurations/ArticleConfiguration.cs

public class ArticleConfiguration : EntityTypeConfiguration
{     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
{
    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 Blogs { get; set; }
        public DbSet
 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
{
    protected override void Seed(BloggerEntities context)
    {
        GetBlogs().ForEach(b => context.Blogs.Add(b));

        context.Commit();
    }

    public static List GetBlogs()
    {
        List _blogs = new List();

        // 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
 GetChsakellsArticles()     {         List
 _articles = new List
();         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
 GetDotNETGeeksArticles()     {         List
 _articles = new List
();         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
 GetAllArticles()     {         List
 _articles = new List
();         _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 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> where);
    // Get an entity by int id
    T GetById(int id);
    // Get an entity using delegate
    T Get(Expression> where);
    // Gets all entities of type T
    IEnumerable GetAll();
    // Gets entities using delegate
    IEnumerable GetMany(Expression> where);
}

Infrastructure/RepositoryBase.cs

public abstract class RepositoryBase where T : class
{
    #region Properties
    private BloggerEntities dataContext;
    private readonly IDbSet 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();
    }

    #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> where)
    {
        IEnumerable objects = dbSet.Where(where).AsEnumerable();
        foreach (T obj in objects)
            dbSet.Remove(obj);
    }

    public virtual T GetById(int id)
    {
        return dbSet.Find(id);
    }

    public virtual IEnumerable GetAll()
    {
        return dbSet.ToList();
    }

    public virtual IEnumerable GetMany(Expression> where)
    {
        return dbSet.Where(where).ToList();
    }

    public T Get(Expression> where)
    {
        return dbSet.Where(where).FirstOrDefault();
    }

    #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, 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 GetBlogByName(string blogName);
}

Repositories/ArticleRepository.cs

public class ArticleRepository : RepositoryBase
, 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 GetArticleByTitle(string articleTitle); }

Service 層

到UnitTestingWebAPI.Service項目上,添加對UnitTestingWebAPI.Domain, UnitTestingWebAPI.Data的引用,并添加下列文件:

ArticleService.cs

// operations you want to expose
public interface IArticleService
{
    IEnumerable
 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
 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 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 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層一樣):

  1. Entity Framework

  2. Microsoft.AspNet.WebApi.Core

  3. 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
 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 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;
    }
}

在有需要時添加下面的過濾器,它會反轉(zhuǎn)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
 _articles = objectContent.Value as List
;             if (_articles != null && _articles.Count > 0)             {                 _articles.Reverse();             }         }     } }

當(dāng)添加下面的媒體類型格式化器,可以返回一個用逗號分割來展示的文章列表:

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
);             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
;             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));             }         }     } }

添加下面兩個 消息 處理器,第一個負(fù)責(zé)Response中添加定制 header,第二個可以決定這個請求是否被接受:

MessageHandler/HeaderAppenderHandler.cs

public class HeaderAppenderHandler : DelegatingHandler
{
    async protected override Task 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 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();
            tsc.SetResult(response);
            return await tsc.Task;
        }
        else
        {
            return await base.SendAsync(request, cancellationToken);
        }
    }
}

添加下面被用于從Web 應(yīng)用程序中注冊Controller 的 DefaultAssembliesResolver

CustomAssembliesResolver.cs

public class CustomAssembliesResolver : DefaultAssembliesResolver
{
    public override ICollection GetAssemblies()
    {
        var baseAssemblies = base.GetAssemblies().ToList();
        var assemblies = new List(baseAssemblies) { typeof(BlogsController).Assembly };
        baseAssemblies.AddRange(assemblies);

        return baseAssemblies.Distinct().ToList();
    }
}

Asp.NET Web Application

添加 UnitTestingWebAPI.API Web應(yīng)用項目,并且添加引用 UnitTestingWebAPI.Core, UnitTestingWebAPI.Data 和 UnitTestingWebAPI.Service,同樣需要安裝下列組件包:

  1. Entity Framework

  2. Microsoft.AspNet.WebApi.WebHost

  3. Microsoft.AspNet.WebApi.Core

  4. Microsoft.AspNet.WebApi.Client

  5. Microsoft.AspNet.WebApi.Owin

  6. Microsoft.Owin.Host.SystemWeb

  7. Microsoft.Owin

  8. Autofac.WebApi2

在Global配置文件(如果沒有就新增一個)中配置初始化數(shù)據(jù)庫配置

Global.asax

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);

    // Init database
    System.Data.Entity.Database.SetInitializer(new BloggerInitializer());
}

同樣要記得添加一個相關(guān)的Connection String在Web.config文件中.


  

注冊外部Controller

在Web Application的根目錄創(chuàng)建一個 Owin Startup.cs 文件并且粘貼下面的代碼,在(autofac configration)需要的時候,這部分代碼會確保 UnitTestingWebAPI.API.Core(CustomAssembliesResolver) 項目正確使用WebApi Controller和注入合適的倉庫以及服務(wù).

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().As().InstancePerRequest();
        builder.RegisterType().As().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);
    }
}

在這個時候,你應(yīng)該可以啟動Web應(yīng)用程序并且使用下面的請求來獲取article或blogs(這里的端口可能不一致):

http://localhost:57414/api/Articles

http://localhost:57414/api/Blogs

同樣附上原文:chsakell's Blog

文章中的源碼: http://down.51cto.com/data/2243634

到此為止,WebAPI部分介紹的差不多了,有問題請留言.


文章題目:ASP.NETWebAPI單元測試-WebAPI簡單介紹
轉(zhuǎn)載注明:http://weahome.cn/article/jspjih.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部