Web API Dev Notes (1): Environment Setup
Original Chinese title: Webapi开发手记(一)—— 环境
Motivation
- 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.
- This post mainly documents issues I ran into along the way; as for general concepts, a quick search will cover those.
Environment and Stack
- RESTful style—not much else to say there.
- 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.
- For IoC I’m using ninject. I’ve seen people use Unity and Castle, so I might consider switching later.
- There’s also a simple EF code generator, which is an internal tool.
Package Management
- 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.
- 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.
- 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
-
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.
-
Publishing is simple, whether via FTP or the file system.
-
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. -
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>
-
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... -
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.
起因
- 工作的缘故,虽然很不喜欢.net的东西,但是不得不去做一些。之前一种使用wcf这样很重的框架,又不跨平台,最近接触了MVC,还是很有趣的,然后就把之前整套框架都拆解出来做成webapi。
- 主要还是记录一些会遇到的问题,至于什么概念什么的,百度一下都会有的。
环境和定义
- RESTFUL风格。这个就没啥可说的
- ORM层用的EF code First 持久层用SQL。因为有一部分数据存在了hbase之中,先使用nodejs做一个中间层,之后会考虑直接用调用。
- IOC 用的ninject,看到有人用Unity和Castle。之后会考虑更换。
- 当然还有一个简单的ef代码生成器。属于内部资源。
库管理
- 刚好这周听内核恐慌讲的就是库管理的事情。我的开发环境是VS2012, .Net的版本是4.5。管理当然是Nuget。确实很有好处,有一些类库从别的项目拖过来的,直接更新下nuget就能把所有依赖的包都下载完全。
- 还是要注意MVC4的库版本,我创建项目的时候用的是MVC4的默认版本,他引入的ef是5.0的版本,而目前是6.1.2 直接使用会报错。很好解决,使用nuget直接更新就好了。
- 路由和view都好懂,还内置了boostrap和jq。看懂路由之后之后写起来就很方便,一个简单的例子一个小时就能弄完。
发布
-
因为整个项目其实就是一个人。所以在写完demo之后我立马拖到生产环境上测试了。注意service的.net版本。
-
发布很简单,不管是ftp还是文件系统。
-
但是访问的时候确实
403。也不报错,本地调试无问题,显示没有可显示目录,如果把目录列表打开,怎无法获得任何资源。 -
经过大量的百度无果,然后找了google,stackoverflow。有如下几种解决方案。个人觉得都是可存在的可能
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
-
window hotfix,如果你是Windows Server 2008 R2 并且很久没给服务器打补丁了,试试这个hotfixUpdate for Windows Server 2008 R2 x64 Edition
我查了将近3个小时就是用这个解决的,因为服务器在内网。从来没有生过级。。。 -
还有一种就是iis 和 asp.net 安装顺序的问题,至于那个在前,我也确实母鸡,只是要注册一下asp.net就行。链接
错误输出
-
custonErrors 这个配置在上了生产环境之后就看不到详细错误了。
-
而且webapi错了,就只会给你一个500.找了一圈找了一个解决方案
返回来的xml只有一句话 -<error> <message>发生错误。</message> <error> -
解决代码:
try { ... } catch (Exception e) { //在webapi中要想抛出异常必须这样抛出,否则之抛出一个默认500的异常 var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(e.ToString()), ReasonPhrase = "error" }; throw new HttpResponseException(resp); }
分发和内容
- 我只有一个对公的ip,所以需要一个分发服务器。
- 目前考虑使用nginx+iis,通过域名来区分。但是配置了一下午,卡在nginx监听80端口,可能和自己的网络供应商有关。