I have a WCF service and I want to expose it as both a RESTfull service and as a SOAP service. Anyone has done something like this before?
相关问题
- Design RESTful service with multiple ids
- Axios OPTIONS instead of POST Request. Express Res
- Plain (non-HTML) error pages in REST api
- Laravel 5.1 MethodNotAllowedHttpException on store
- How to make a .svc file write to asp.net Trace.axd
相关文章
- WCF发布Windows服务 POST方式报错 GET方式没有问题 应该怎么解决?
- XCopy or MOVE do not work when a WCF Service runs
- Cannot use org.jvnet.jax-ws-commons.jaxws-maven-pl
- Got ActiveRecord::AssociationTypeMismatch on model
- Multiple parameters in AngularJS $resource GET
- Global Exception Handling in Jersey & Spring?
- Could not find default endpoint element that refer
- The 'DbProviderFactories' section can only
We must define the behavior configuration to REST endpoint
and also to a service
After the behaviors, next step is the bindings. For example basicHttpBinding to SOAP endpoint and webHttpBinding to REST.
Finally we must define the 2 endpoint in the service definition. Attention for the address="" of endpoint, where to REST service is not necessary nothing.
In Interface of the service we define the operation with its attributes.
Joining all parties, this will be our WCF system.serviceModel definition.
To test the both endpoint, we can use WCFClient to SOAP and PostMan to REST.
This post has already a very good answer by "Community wiki" and I also recommend to look at Rick Strahl's Web Blog, there are many good posts about WCF Rest like this.
I used both to get this kind of MyService-service... Then I can use the REST-interface from jQuery or SOAP from Java.
This is from my Web.Config:
And this is my service-class (.svc-codebehind, no interfaces required):
Actually I use only Json or Xml but those both are here for a demo purpose. Those are GET-requests to get data. To insert data I would use method with attributes:
You can expose the service in two different endpoints. the SOAP one can use the binding that support SOAP e.g. basicHttpBinding, the RESTful one can use the webHttpBinding. I assume your REST service will be in JSON, in that case, you need to configure the two endpoints with the following behaviour configuration
An example of endpoint configuration in your scenario is
so, the service will be available at
Apply [WebGet] to the operation contract to make it RESTful. e.g.
Note, if the REST service is not in JSON, parameters of the operations can not contain complex type.
Reply to the post for SOAP and RESTful POX(XML)
For plain old XML as return format, this is an example that would work both for SOAP and XML.
POX behavior for REST Plain Old XML
Endpoints
Service will be available at
REST request try it in browser,
SOAP request client endpoint configuration for SOAP service after adding the service reference,
in C#
Another way of doing it is to expose two different service contract and each one with specific configuration. This may generate some duplicates at code level, however at the end of the day, you want to make it working.
MSDN seems to have an article for this now:
https://msdn.microsoft.com/en-us/library/bb412196(v=vs.110).aspx
Intro:
If you only want to develop a single web service and have it hosted on many different endpoints (i.e. SOAP + REST, with XML, JSON, CSV, HTML outputes). You should also consider using ServiceStack which I've built for exactly this purpose where every service you develop is automatically available on on both SOAP and REST endpoints out-of-the-box without any configuration required.
The Hello World example shows how to create a simple with service with just (no config required):
No other configuration is required, and this service is immediately available with REST in:
It also comes in-built with a friendly HTML output (when called with a HTTP client that has Accept:text/html e.g a browser) so you're able to better visualize the output of your services.
Handling different REST verbs are also as trivial, here's a complete REST-service CRUD app in 1 page of C# (less than it would take to configure WCF ;):
This is what i did to make it work. Make sure you put
webHttp automaticFormatSelectionEnabled="true" inside endpoint behaviour.
Inside service model
EndPoint Behaviour