Legacy Archive (2015): Originally written in Chinese on doubixie.com and translated into English for this edition. All archive posts · Home

Web API Dev Notes (1): Environment Setup

Original Chinese title: Webapi开发手记(一)—— 环境

Motivation

  1. Because of work—even though I really dislike .NET stuff—I have to build some things with it. Previously we used a heavy framework like WCF, which isn’t cross-platform either. Recently I tried MVC and found it pretty interesting, so I broke apart the old framework and rebuilt it as a Web API.
  2. This post mainly documents issues I ran into along the way; as for general concepts, a quick search will cover those.

Environment and Stack

  1. RESTful style—not much else to say there.
  2. EF Code First for the ORM layer, with SQL for persistence. Since part of the data lives in HBase, I’m using Node.js as a middleware layer for now, and may consider calling it directly later.
  3. For IoC I’m using ninject. I’ve seen people use Unity and Castle, so I might consider switching later.
  4. There’s also a simple EF code generator, which is an internal tool.

Package Management

  1. Coincidentally, this week’s episode of Kernel Panic talked about package management. My dev environment is VS2012 with .NET 4.5, and package management is of course Nuget. It really helps: when dragging class libraries over from other projects, just updating NuGet downloads all the required dependency packages.
  2. Watch out for MVC4 library versions: when I created the project with the default MVC4 template, it pulled in EF 5.0, whereas the current version is 6.1.2, and using it directly throws errors. Easy fix—just update it via NuGet.
  3. Routing and views are straightforward, and Bootstrap and jQuery come built in. Once you understand routing, coding is smooth—you can put together a simple example in an hour.

Deployment

  1. Since it’s basically a one-person project, right after finishing the demo I pushed it straight to production for testing. Make sure to check the .NET version on the server.

  2. Publishing is simple, whether via FTP or the file system.

  3. However, accessing it returned 403. No error message, and local debugging worked fine—it just said there was no directory to display, and enabling directory browsing still wouldn’t fetch any resources.

  4. After searching Baidu extensively with no luck, I turned to Google and Stack Overflow and found several potential fixes. In my view, any of these could be the cause:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer>
  1. Windows hotfix: if you’re on Windows Server 2008 R2 and haven’t patched the server in a long time, try this hotfix: Update for Windows Server 2008 R2 x64 Edition

    I spent nearly 3 hours troubleshooting before solving it with this—the server is on an intranet and had never been updated...

  2. Another possibility is the installation order of IIS and ASP.NET. I’m not sure which needs to go first, you just need to register ASP.NET. Link

Error Output

  • With the custonErrors configuration in production, detailed errors are hidden.

  • Plus when Web API fails, it only gives you a 500. After searching around, I found a workaround:

    返回来的xml只有一句话
    -<error>
        <message>发生错误。</message>
     <error>
  • Solution code:

    try
    {
        ...
    }
    catch (Exception e) {
    //在webapi中要想抛出异常必须这样抛出,否则之抛出一个默认500的异常
        var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
        {
            Content = new StringContent(e.ToString()),
            ReasonPhrase = "error"
        };
    throw new HttpResponseException(resp); 
    }

Dispatch and Content

  • I only have one public IP, so I need a dispatch server.
  • Currently considering Nginx + IIS, routing by domain name. I spent an afternoon configuring it and got stuck on Nginx listening on port 80—might have something to do with my ISP.

That’s about it.