REST and WCF mix

I am specifically looking for an example that uses a) WCF and REST. After long searches, although I received some, they are not in my understanding.

Can someone please give me a very simple example, such as "Hallow World" or "Summation of 2 numbers", which will give me a clear idea of ​​how to write a server, as well as how to consume the same from the end of the client.

Also, if there is any good link that explains this example in plain language, kindly tell me that.

thanks

+4
source share
6 answers

REST in WCF is not that difficult once you figure it out.

You must first define your interface.

Here is an example.

[ServiceContract] public interface IRESTExample { [WebGet(UriTemplate = "interaction/queue?s={site}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] [OperationContract] string QueueInteraction(string site); [WebGet(UriTemplate = "interaction/cancel?id={interactionId}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] [OperationContract] string CancelInteraction(string interactionId); [WebGet(UriTemplate = "queue/state?s={site}&q={queue}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] [OperationContract] string QueueState(string site, string queue); } 

In WebGet, you can define the final URL. So it depends on where you connect it, but say that you are binding the endpoint to www.example.com/rest

QueueInteraciton will be www.example.com/rest/interaction/queue?s=SomeSite

If {stie} or {parameterName} is replaced with the parameter name.

Execution is just a class, I'm going to assume that you know how to implement an interface. If you need help just leave a comment.

Now snap the endpoint. In the end, it is not so difficult, you can do it all in the config.

 <system.serviceModel> <services> <service name="Stackoverflow.Example.Service.RestExample" behaviorConfiguration="MyServiceTypeBehaviors"> <host> <baseAddresses> <add baseAddress="http://localhost:2136/RestExample"/> </baseAddresses> </host> <endpoint address="rest" binding="webHttpBinding" behaviorConfiguration="xmlBehavior" contract="Stackoverflow.Example.Service.IRESTExample" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="MyServiceTypeBehaviors" > <!-- Add the following element to your service behavior configuration. --> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="jsonBehavior"> <webHttp/> </behavior> <behavior name="xmlBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name = "NoSecurity"> <security mode = "None" /> </binding> </basicHttpBinding> </bindings> </system.serviceModel> 

Now enter the code to start the service and link it. You can do this in everything, for example, in a console application.

 RestExample exampleService = new RestExample(); host = new ServiceHost(exampleService); host.Open(); 

That should be enough to get started.

+4
source

msdn article Introduction to RESTful services with WCF with sample code in the msdn code gallery . Also check out the code article

+2
source

David Basarab's answer is correct, but there is a much simpler way to do this without any manual connection. Especially if you are used to the classic ASMX web services and don't have much experience with WCF, the following method is easy to foul.

  • In a Visual Studio 2010 web project, add a link to System.ServiceModel.Web .
  • Select "add new item" to your project. The template you want is located in the "Web" and is called "AJAX-enabled WCF Service". Do not choose vanilla "WCF Service"! If you need this, you must do all the web.config wiring yourself, as David Basarab talked about, and this is a pain. The AJAX-enabled WCF service does all of this for you. Name your service as you want.
  • Open the .svc file. In the [ServiceContract] attribute in your class, fill in the namespace string parameter with whatever you want.
  • You will get a DoWork () sample and a bunch of comments telling you what to do in the file. The trick to getting a RESTful API is adding the [WebGet()] attributes to your web methods. Add it to DoWork () and check all the functions for you.

So, to call the DoWork () method, you would fall into this in your browser: http: //localhost/MyAjaxEnabledService.svc/DoWork

Now add the new HelloWorld () method, which shows some parameters and output.

VB:

 <OperationContract()> <WebGet(ResponseFormat:=WebMessageFormat.Xml)> Public Function HelloWorld(ByVal name As String) As String WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml" Return String.Format("Hello, {0}!", If(String.IsNullOrWhiteSpace(name), "world", name)) End Function 

FROM#:

 [OperationContract()] [WebGet(ResponseFormat=WebMessageFormat.Xml)] public string HelloWorld(String name) { WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; Return String.Format("Hello, {0}!", String.IsNullOrWhiteSpace(name) ? "world" : name); } 

Now you can visit:

http: //localhost/MyAjaxEnabledService.svc/HelloWorld? name = MattMc3

There's a lot of crappy and confusing WCF documentation, especially for those who crave the simplicity of the old .ASMX style. Hope this helps someone get started with WCF. There is much more you can do with it than the old ASMX style, but it is difficult to overclock and not discourage using MS for their poor help when switching from ASMX. You can learn more about the fast and dirty RESTful WCF services here .

+2
source

If you really want to do ReST, use a web framework that will lead you to the right path. See OpenRasta .

It is impossible to make WCF to perform REST, it is just very difficult to learn how to Rest using a framework that will often bother you and lead you in the wrong direction.

+1
source

At Microsoft Web Developer, you can use the WCF REST Service online template. He will create a project for you with the correct web.config and global.asax files.

0
source

You can create a WCF REST web service by configuring the endpoint to use webHttpBinding , as shown in this in-depth tutorial:

http://www.west-wind.com/weblog/posts/310747.aspx

There's also an open source web services infrastructure that makes it easy to create XML and JSON REST web services without requiring additional configuration.

Edit: Added a link to a good article that describes the spirit of REST:

http://tomayko.com/writings/rest-to-my-wife

Link to a blog comment explaining common REST misconceptions:

http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

-1
source

Source: https://habr.com/ru/post/1301523/


All Articles