none
help on endpoint for WCF REST service RRS feed

  • Question

  • WCF configuration always confused me.  I have WCF REST service. The person who developed this added own service host (at least that’s how it looks from the class name) however ServiceContract & ServiceBehavior attributes are configured on the top of ServiceHost class.

    Below are the classes.

    I wanted to know how the endpoint element should be configured in config file? What should be the service name, address and contract 

    namespace Forms.DocumentServiceGateway
    {
        [ServiceContract]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
        public class GatewayServiceHost
        {
            private IGatewayService _service;
    
            public GatewayServiceHost()
            {
            }
    
            public GatewayServiceHost(IGatewayService service)
            {
                _service = service;
            }
    
            [Description("Preview a template")]
            [WebGet(UriTemplate = "PreviewTemplate?entityID={entityID}&templateID={templateID}")]
            public Stream PreviewTemplate(int entityID, int templateID)
            {
                var response = _service.PreviewTemplate(entityID, templateID);
                return BuildStream(response);
            }
    }

    namespace Forms.DocumentServiceGateway
    {   
        
        public interface IGatewayService
        {        
            Response PreviewTemplate(int entityID, int templateID);
        }
    }

    namespace Forms.DocumentServiceGateway
    {
        public class GatewayService : IGatewayService
        {
            private IHelper _helper;
    
            public GatewayService(IHelper helper)
            {
                _helper = helper;
            }
    
            public Response PreviewTemplate(int entityID, int templateID)
            {
                return _helper.PreviewTemplate(entityID, templateID);
            }
        }
    }



    • Edited by lax4u Monday, May 20, 2013 4:28 PM
    Monday, May 20, 2013 4:27 PM

Answers

All replies

  • Hi,

    >>The person who developed this added own service host (at least that’s how it looks from the class name) however ServiceContract & ServiceBehavior attributes are configured on the top of ServiceHost class.

    It is hard to understand the structure of the services simply from the code snippets. It not seem a custom Service Host, but it should implement ServiceHost class. You may check how it be used in the project.

    #Custom Service Host

    http://msdn.microsoft.com/en-us/library/aa395224.aspx

    Also you can take a look at a blog below which give an example self-hosting WCF REST service.

    #Self Hosted WCF REST Service or Hosting WCF REST Service in Console Application

    http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/self-hosted-wcf-rest-service-or-hosting-wcf-rest-service-in-console-application/

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 21, 2013 6:20 AM
    Moderator
  • Well i was not looking for how to develop custom host. My question was  just because the class name above is "GatewayServiceHost" i dont think it is ServiceHost. In-fact the class has ServiceContract attribute, that means its a service contract , but not servicehost. Ideally we create interface and apply [ServiceContract] attribute on the interface. Note that I cannot change code

    So my endpoint look like below, note the contract and service name, is that correct?

    <services>
          <service name="DocumentServiceGateway.GatewayServiceHost">
            <clear/>
            <endpoint address=""
                      contract="Forms.DocumentServiceGateway.GatewayServiceHost"
                      binding="webHttpBinding"/>
          </service>
        </services>

    I thought service name should be namespace + class name. However if I set the service name as “Forms.DocumentServiceGateway.GatewayServiceHost” and then I try to browse the service, I get error “endpoint not found”


    • Edited by lax4u Tuesday, May 21, 2013 2:38 PM
    Tuesday, May 21, 2013 2:37 PM
  • Hi,

    Yes, the service name should be namespace+ class name, try "Forms.DocumentServiceGateway.GatewayService", the contract should be "Forms.DocumentServiceGateway.IGatewayService", I am not sure about the "GatewayServiceHost", but you can also try set the service name as "Forms.DocumentServiceGateway.GatewayServiceHost".

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 23, 2013 8:31 AM
    Moderator
    • If i set service name to "Forms.DocumentServiceGateway.GatewayService" then i get enpoint not found error.
    • I cannot set contract to "Forms.DocumentServiceGateway.IGatewayService" because IGatewayService does not have [ServiceContract] attribute. The service contract attribute is on GatewayServiceHost clas, and this class is NOT derived from IGatewayService
    Thursday, May 23, 2013 8:08 PM
  • Hi,

    Since I do not know the structure of the project, but one thing you need note, the contract need be configured properly. Refer a document below for the endpoint properties, a contract that identifies the operations available.

    #Endpoints: Addresses, Bindings, and Contracts

    http://msdn.microsoft.com/en-us/library/ms733107.aspx

    Hope this helps.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 28, 2013 9:31 AM
    Moderator