Dave on Facebook

Dave's profile on LinkedIn
DavesWeb visitors really liked:
Country Place Cookbook
Tomato Sauce Recipe
Bloody AwfulBelow AverageNothing SpecialGoodPretty Damn Good
My Status
Fabulous Snow!
Bloody AwfulBelow AverageNothing SpecialGoodPretty Damn Good
My Status
Thankful for Signs of Spring!
Bloody AwfulBelow AverageNothing SpecialGoodPretty Damn Good
Country Place Cookbook
Grandmother Snare's Iced Tea
Bloody AwfulBelow AverageNothing SpecialGoodPretty Damn Good
Gallery
Can Am Eyes
Bloody AwfulBelow AverageNothing SpecialGoodPretty Damn Good
Top Viewed items on DavesWeb:
My Status
Balsamic Vinaigarette Watermelon - 38,091 views
My Status
Cool car - 26,530 views
Gallery
Can Am Eyes - 25,968 views

No Live Blogging, Sorry - 16,716 views
My Status
Ads Sneaked Onto DavesWeb Part 2 of 2 - 16,252 views
Recently Viewed Items on DavesWeb:
TechQuest
Create a Simple RESTful Service using Visual Studio
TechQuest
Prettifying Code Snippets on Web Pages
TechQuest
Create a Simple RESTful Service using Visual Studio
TechQuest
TDD with MVC4 Code First EF6 Application
Muse
Don't Eat The Blue Food
TechQuest: Welcome to TechQuest.
Create a Simple RESTful Service using Visual Studio
RESTful services are REALLY simple to consume. But they don't have to be complicated to build. This tutorial will help you create a VERY simple REST service that you can build with the FREE Visual Studio 2015 Community Edition and deploy with a FREE Azure AD account.
  1. Launch Visual Studio and create a new WCF Service Application. I called mine SimpleRESTService.
  2. Delete the files IService1.cs and Service1.src. We will replace them with similar files of our own.
  3. Right click on your project and select Add, New Item. In the Add New Item dialog, under Web, select WCF Service. Name the new file something like MyService.svc. Note that this creates two files: IMyService.cs, which defines the interface for the service, and MyService.svc, which implements the IMyService interface.
    Uploaded Image
  4. Now we will write code to set up RESTful service methods. Open IMyService.cs and replace the code with this.
    using System.ServiceModel;
    using System.ServiceModel.Web;
    
    namespace SimpleRESTService
    {
        [ServiceContract]
        public interface IMyService
        {
            [OperationContract]
            [WebInvoke(Method = "GET",
                ResponseFormat = WebMessageFormat.Xml,
                BodyStyle = WebMessageBodyStyle.Wrapped,
                UriTemplate = "xml/{id}")]
            string XmlData(string id);
    
            [OperationContract]
            [WebInvoke(Method = "GET",
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Wrapped,
                UriTemplate = "json/{id}")]
            string JsonData(string id);
        }
    }
    
  5. Open MyService.svc.cs and replace the existing code with this:
    namespace SimpleRESTService
    {
        public class MyService : IMyService
        {
            public string XmlData(string id)
            {
                return "You requested id " + id;
            }
    
            public string JsonData(string id)
            {
                return "You requested id " + id;
            }
        }
    }
  6. Now open the web.config file and replace the code with this:
    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5.2" />
        <httpRuntime targetFramework="4.5.2"/>
      </system.web>
      <system.serviceModel>
        <services>
          <service name="SimpleRESTService.MyService" behaviorConfiguration="ServiceBehaviour">
            <endpoint address="" binding ="webHttpBinding" contract="SimpleRESTService.IMyService" behaviorConfiguration="web"></endpoint>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="ServiceBehaviour">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="web">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    
  7. That's it! We are ready to test! In Solution Explorer, select the solution, then press F5. This will launch a web browser and open your site. Don't freak yet, because we need to add a little bit to the Url.
  8. In the Url bar, add MyService.svc and that will bring up the WCF service home page. But that's not really what we want. We want to access the REST service.
    Uploaded Image
  9. In the Url bar, append /xml/12345 so the full Url is something like http://localhost:8448/MyService.svc/xml/12345 and you will see the Xml returned by the REST service!
    <?xml version="1.0"?>
    -<XmlDataResponse xmlns="http://tempuri.org/">
    
    <XmlDataResult>You requested id 12345</XmlDataResult>
    
    </XmlDataResponse>
    
  10. Replace the xml in the Url with json and you will see the same thing with the result returned in json. Your browser may ask you whether you want to Open or Save the result.

    Uploaded Image

That's it! You can customize and extend from there! It is so much easier than WCF of Web services that anytime I need to spin up a new service I will try to do it in REST first.
1,038 views.
No users have rated this item yet.