none
Convert template-created VS2010 WCF service to WCF REST service RRS feed

  • Question

  • Below are the exact steps to create a simple WCF service application in VS 2010. From this, I simply want to know what I need to add to create a REST service that is accessible from the web browser.  If possible, please provide exact syntax, don't say "add a service endpoint..." because I understand the concept; it's just that all of my attempts have not worked.  The error messages give no help.

    Open VS2010
    New Project
    select "WCF Service Application"
    Allow use name "WcfService1"
    In Solution Explorer, select "Service1.svc", right-click, choose "Set As Start Page"
    Hit F5.
    Test harness appears - in left tree, double-click "GetData()"
    Type "23" into the top right-hand side grid for field name = "value"; click the "Invoke" button.
    See the result appear in the Response (bottom) grid. "You entered 23"

    This proves VS built the WCF basic service correctly, and the test harness works.

    Now attempt modification of project to provide REST service for the "GetData" service.
    open IIS Manager.  Expand "Sites", and then expand "Default Web Site"
    Right-click "Default Web Site", select "Add Application"
    In the Alias field, type "WcfService1"
    Navigate via the ... button to the directory where VS placed your "WcfService1" folder for the project you created above.
    Complete the application creation process.

    Open Windows Explorer, navigate to the same WcfService1 folder.  Give "everyone" full rights to the folder (because I don't feel like figuring out right now what the security should REALLY be - probably aspnet or something like that....)

    Open IE.  Navigate to "http://localhost/wcfservice1/service1.svc"
    Hopefully you see the offer of a WSDL page in the browser.

    Now, what do I need to change in the project so that I can enter this in the browser, and see the reply similarly to the test project?

    http://localhost/wcfservice1/service1.svc/23

    and I should see in the browser via XML or JSON - I don't care - : "You entered 23"

    Friday, March 25, 2011 4:57 PM

Answers

  • Larry,

    Here is an easier way to take the WcfService1 project and host it in IIS

    • Right click WcfService1 in the solution explorer and select Properties
    • Click the Web tab
    • Under Servers select Use Local IIS Web Server and specify the URL you want to use (or use the default)
    • Click Create Virtual Directory
    • Build the solution

    To convert the WcfService1 to a WCF REST (WCF HTTP) service do the following:

    • In IService1.cs replace [OperationContract] on the GetData method with [WebGet]
      [ServiceContract]
      public interface IService1
      {
        [WebGet]
        string GetData(int value);
    
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    
        // TODO: Add your service operations here
      }
    
    
    • In Service1.svc add Factory="System.ServiceModel.Activation.WebServiceHostFactory" to the end of the ServiceHost directive
    <%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
    • Add the following configuration within <system.ServiceModel> in the web.config file:
          <standardEndpoints>
            <webHttpEndpoint>
              <!-- the "" standard endpoint is used by WebServiceHost for auto creating a web endpoint. -->
              <standardEndpoint name="" helpEnabled="true"/>
            </webHttpEndpoint>
          </standardEndpoints>
    <configuration>
    
     <system.web>
      <compilation debug="true" targetFramework="4.0" />
     </system.web>
     <system.serviceModel>
      <standardEndpoints>
       <webHttpEndpoint>
        <!-- the "" standard endpoint is used by WebServiceHost for auto creating a web endpoint. -->
        <standardEndpoint name="" helpEnabled="true"/>
       </webHttpEndpoint>
      </standardEndpoints>
      <behaviors>
       <serviceBehaviors>
        <behavior>
         <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
         <serviceMetadata httpGetEnabled="true"/>
         <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
         <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
       </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
     </system.serviceModel>
     <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
     </system.webServer>
     
    </configuration>
    

    <?xml version="1.0"?>
    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">You entered: 23</string>

    I know this isn't the exact URI you requested but it's close. By default WCF HTTP (REST) uses the operation name in the URL to route the call to the appropriate operation. You can change how this is done by specifying a UriTemplate within the [WebGet] attribute. For more information see http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx for information about the WebGet attribute and http://msdn.microsoft.com/en-us/library/bb675245.aspx for information about UriTemplates.

    Also see the complete WCF Web HTTP Programming Model docs: http://msdn.microsoft.com/en-us/library/bb412169.aspx

    We are currently working on an updated Web API Programming Model that provides first class HTTP support. You can download the beta from http://wcf.codeplex.com. This is a beta, but it will give you a good idea of where we are going in terms of HTTP and REST support.

    If you have any questions, please let me know.

    Thanks,

    Michael Green
    WCF Documentation Team

    • Marked as answer by Yi-Lun Luo Friday, April 1, 2011 9:02 AM
    Friday, March 25, 2011 10:48 PM

All replies

  • Larry,

    Here is an easier way to take the WcfService1 project and host it in IIS

    • Right click WcfService1 in the solution explorer and select Properties
    • Click the Web tab
    • Under Servers select Use Local IIS Web Server and specify the URL you want to use (or use the default)
    • Click Create Virtual Directory
    • Build the solution

    To convert the WcfService1 to a WCF REST (WCF HTTP) service do the following:

    • In IService1.cs replace [OperationContract] on the GetData method with [WebGet]
      [ServiceContract]
      public interface IService1
      {
        [WebGet]
        string GetData(int value);
    
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    
        // TODO: Add your service operations here
      }
    
    
    • In Service1.svc add Factory="System.ServiceModel.Activation.WebServiceHostFactory" to the end of the ServiceHost directive
    <%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
    • Add the following configuration within <system.ServiceModel> in the web.config file:
          <standardEndpoints>
            <webHttpEndpoint>
              <!-- the "" standard endpoint is used by WebServiceHost for auto creating a web endpoint. -->
              <standardEndpoint name="" helpEnabled="true"/>
            </webHttpEndpoint>
          </standardEndpoints>
    <configuration>
    
     <system.web>
      <compilation debug="true" targetFramework="4.0" />
     </system.web>
     <system.serviceModel>
      <standardEndpoints>
       <webHttpEndpoint>
        <!-- the "" standard endpoint is used by WebServiceHost for auto creating a web endpoint. -->
        <standardEndpoint name="" helpEnabled="true"/>
       </webHttpEndpoint>
      </standardEndpoints>
      <behaviors>
       <serviceBehaviors>
        <behavior>
         <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
         <serviceMetadata httpGetEnabled="true"/>
         <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
         <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
       </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
     </system.serviceModel>
     <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
     </system.webServer>
     
    </configuration>
    

    <?xml version="1.0"?>
    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">You entered: 23</string>

    I know this isn't the exact URI you requested but it's close. By default WCF HTTP (REST) uses the operation name in the URL to route the call to the appropriate operation. You can change how this is done by specifying a UriTemplate within the [WebGet] attribute. For more information see http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx for information about the WebGet attribute and http://msdn.microsoft.com/en-us/library/bb675245.aspx for information about UriTemplates.

    Also see the complete WCF Web HTTP Programming Model docs: http://msdn.microsoft.com/en-us/library/bb412169.aspx

    We are currently working on an updated Web API Programming Model that provides first class HTTP support. You can download the beta from http://wcf.codeplex.com. This is a beta, but it will give you a good idea of where we are going in terms of HTTP and REST support.

    If you have any questions, please let me know.

    Thanks,

    Michael Green
    WCF Documentation Team

    • Marked as answer by Yi-Lun Luo Friday, April 1, 2011 9:02 AM
    Friday, March 25, 2011 10:48 PM
  • Michael,

    Tried following your example step by step.  Builds, but when I try to access the service I get error page:

    HTTP Error 500.0 - Internal Server Error

    Calling LoadLibraryEx on ISAPI filter "C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll" failed

    I'm going to be forced to switch gears so I can't dig deeper into the error for a few weeks, but thought I'd let you know there may be other steps required. Or perhaps something has changed since March 2011.

    Monday, January 9, 2012 8:01 PM