I created a minuscule WCF service as a proof of concept. I eventually want to have a WCF service exposed very simply over the web for any consumer to call by URL with the ability to post data to some of the methods (non-.Net clients, so it should appear to the public as a simple web service). I use the webHttpBinding, enabled webHttp in my endpointBehavior, and added a few methods to the service with WebGetAttribute and one with WebInvokeAttribute for post. The WebGet stuff works without a hitch. I can simply browse to the url pattern defined in my UriTemplate from a web browser, pass parameters in from url args, return whatever data type (XElement, string, int, etc). I can also call this via WebClient.DownloadString from any other app type locally.
So, WebInvoke (Post) isn't quite behaving as well. Obviously browsing to the UriTemplate pattern isn't valid, because this issues a "GET" via the browser, so I get Service - Method Not Allowed. I'm ok with t开发者_如何学Chat. However, as soon as I write some simple WebClient.UploadString stuff to call the service via Post, I get errors.
At first I got 415 Method Not Allowed. I googled, enabled serviceDebug includeExceptionDetailInFaults in my serviceBehavior, and now the error is 400 Bad Request. I further googled, and found suggestions that I explicitly set the WebClient.Encoding to UTF8 and set my Content-type header to "application/xml; charset=utf-8" prior to the call. Same thing. I tried this with other mime types (text/plain, etc) and formatted the uploaded string all sorts of ways (key=value, key='value', value, 'value', \"value\", etc). I even tried UrlEncoding it. I even scrapped the whole WebClient and went the old HttpWebRequest/HttpWebResponse route, and I still get the same error.
At this point, I don't believe the issue is on the client side. I'm not very experienced with WCF, so I could very easily be doing something wrong there. For instance, I'm using a single service and a single endpoint. Is that cool for both get and post exposed service methods? Do they need slight differences in either serviceBehavior and/or endpointBehavior? I've seen a few examples online touting that WebInvoke over Http Post just works, so I'm pretty convinced that I'm just missing something simple on the service.
To test your WCF REST service, you should definitely get some help in the form of Fiddler, which allows you to easily create and send off POST requests to your URL. This would make sure you're really getting proper POST requests into your service.
Also, if you haven't already, check out the Pluralsight series of screencasts on WCF REST Starter Kit - this one on HTTP Plain XML (POX) Services in particular. Aaron Skonnard explains very nicely and very clearly how to do things and what problems to get around. Highly recommended!
Marc
精彩评论