wcf rest collection - uri template

Last post 06-14-2009 8:51 PM by Krusty13000. 4 replies.

Sort Posts:

  • wcf rest collection - uri template

    06-10-2009, 11:52 PM
    • Member
      6 point Member
    • Krusty13000
    • Member since 04-05-2008, 2:42 AM
    • Posts 33

     Hello,

    can anyone tell me where in the rest starter kit 2 how I modify the uri template to accept two parameters? so the uri will be for the get ...

    http://localhost:25000/Service.svc/5/3

    where 5 will be the employee id and 3 the department id.

    I am not sure where to modify the template anymore?

     

    Thanks

     

  • Re: wcf rest collection - uri template

    06-11-2009, 1:50 AM

    The kit uses the default ServiceContract "ICollectionService<ITem>" where you modify your OperationContract. But it is compiled into Microsoft.ServiceModel.Web.dll. So you can not modify the default service contract.

    You just need to add another service contract in your project, and put in your operation contract including the template. Don't forget to use this service contract by making the service inherit to it.

    Allen Wang

  • Re: wcf rest collection - uri template

    06-11-2009, 5:58 PM
    • Member
      6 point Member
    • Krusty13000
    • Member since 04-05-2008, 2:42 AM
    • Posts 33

     Allen,

    do you know where there is an example of this?

     

    Thanks

    Krusty.

  • Re: wcf rest collection - uri template

    06-12-2009, 8:19 AM
    Answer

      Sorry, I have not seen an example using self-defined templates except the defaults in Starter Kit 2. If you want to use your own UriTemplates, I think you can try in this way.

    1. delete the default svc file and related code file.

    2. add a new WCF Service item (3 files will be added, here suppose they are IService.cs, Service.svc and Service.svc.cs)

    3. edit the content is the three files.

       IService.cs 

    using System.ServiceModel;
    using System.ServiceModel.Web;

    namespace CollectionService
    {
    [ServiceContract]
    public interface IEmployeeService
    {
    [OperationContract]
    [WebGet(UriTemplate = "{id}")]
    string GetEmployee(string id);

    [OperationContract]
    [WebGet(UriTemplate = "{id}/{departmentId}")]
    string AddEmployee(string id, string departmentId);
    }
    }

        Service.svc.cs 

    using System;

    namespace CollectionService
    {
    public class Service :IEmployeeService
    {
    public string GetEmployee(string id)
    {
    return String.Format("U R requesting employee '{0}'", id);
    }

    public string AddEmployee(string id, string departmentId)
    {
    return String.Format("U R requesting employee '{0}' department '{1}'"
                                                    , id, departmentId);
    }
    }
    }   

        Service. svc  ( Add the WebServiceHostFactory) 

    <%@ ServiceHost Language="C#" Debug="true" Service="CollectionService.Service" 
    CodeBehind="Service.svc.cs" 

    Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>  

     

    4. At last, delete the tag  and its content in the config file.

    5. View Service.svc in browser , and add "/ddd" or "/ddd/eee" to the end of the URI you will see:


      <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">U R requesting employee 'ddd'</string>
     
       and
     
      <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">U R requesting employee 'ddd' department 'eee'</string>
     

     Besides, you can also install the Starter Kit Preview 1, because it automatically adds a 'service.base.svc.cs' file, in which you can modify the UriTemplates .

     

     

     

  • Re: wcf rest collection - uri template

    06-14-2009, 8:51 PM
    • Member
      6 point Member
    • Krusty13000
    • Member since 04-05-2008, 2:42 AM
    • Posts 33

     Thank you so much Allen - it worked.

Page 1 of 1 (5 items)