简体   繁体   中英

What is the best way to version a WCF Web API?

I currently have a WCF web API that I have split into two versions. The first version runs at api.mysite.com. The second is currently not published to production.

I would like a way to publish the second API such that requests to the first version are non-interrupted. My ideas would be to add a x-api-version header and internally route the request to the designated API. If there is no header, then default to version 1. I considered adding /v1 or /v2 to the beginning of the path to delimit the version such that a request to v1 or v2 might look like:

http://api.mysite.com/v1/authentication/login
http://api.mysite.com/v2/auth/login

The only caveat is that requests without the version must work and default to version 1 (or whatever version I specify).

Although this sounds good (to me at least), I'm not sure on what the recommended way of implementing this would be. I know that I could always do some sort of reverse proxy but, I'm hoping that my solutions can be a programatic one. The less configuration that is required on the part of the server, the better. If anyone has any ideas or links to blogs/tutorials, that would be fantastic!

Thanks in advance!

微软在这里有一篇关于使用WCF进行版本控制的文章

Ok, I enjoyed the answers that I have received so far (thank you both) but, it didn't quite solve my problem given the constraints and goals that I have with my API. So, I wanted to detail the solution that I have found and plan to use.

To start, I am versioning my API via the URI. This is to mean that various versions of the API will look like:

http://api.mysite.com/authentication/login
http://api.mysite.com/v1/authentication/login
http://api.mysite.com/v2/auth/login
http://api.mysite.com/v3/auth/letmeinplease
... you get the point ...

The important thing to note here is that if I do not include a version number, then I default to version 1. This will be my current setup however, it could just as easily default to the latest version, latest stable, etc.

Here we go. I created a folder in which the application will live ( wwroot/api ). Inside that folder I created folders for all of the versions: v1, v2, etc. Now, in IIS (7.5 for me) I created a new project which had an application root of wwroot/api/v1 . Then I added each version folder (including v1) as a sub-application. This allows me to version the API via the URI (as seen above) however, there is a caveat.

Web.config inheritance can really be a pain. So, I made sure to disable it for all of my api versions. A reference on how to do this can be found here . With that exception, everything works like a charm! :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM