REST and WCF connection

后端 未结 6 1020
执笔经年
执笔经年 2021-02-06 15:33

I am specifically looking for an example which use a) WCF & REST. After a long googling, though I got some but they are beyond my understanding.

Could some one pleas

6条回答
  •  -上瘾入骨i
    2021-02-06 16:10

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

    First you must 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);
    
    }
    

    You can see in the WebGet you define the final URL. So it depends on where you bind it, but say you bind the endpoint to www.example.com/rest

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

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

    The implemetion is just a simple class, I am going to assume you know how to implement an interface. If you need help just leave a comment.

    Now binding the endpoint. In the end it is not that hard, you can do it all in the config.

    
        
            
                
                    
                        
                    
                
    
                
    
            
        
        
            
                
                    
                    
                
            
    
            
                
                    
                
                
                    
                
            
        
        
            
                
                    
                
            
        
    
    

    Now the code to start the service and bind it. YOu can do it in anything, for example a console app.

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

    This should be enough to get started.

提交回复
热议问题