REST / SOAP endpoints for a WCF service

前端 未结 6 1586
囚心锁ツ
囚心锁ツ 2020-11-22 03:05

I have a WCF service and I want to expose it as both a RESTfull service and as a SOAP service. Anyone has done something like this before?

6条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 04:03

    You can expose the service in two different endpoints. the SOAP one can use the binding that support SOAP e.g. basicHttpBinding, the RESTful one can use the webHttpBinding. I assume your REST service will be in JSON, in that case, you need to configure the two endpoints with the following behaviour configuration

    
      
        
      
    
    

    An example of endpoint configuration in your scenario is

    
      
        
        
      
    
    

    so, the service will be available at

    • http://www.example.com/soap
    • http://www.example.com/json

    Apply [WebGet] to the operation contract to make it RESTful. e.g.

    public interface ITestService
    {
       [OperationContract]
       [WebGet]
       string HelloWorld(string text)
    }
    

    Note, if the REST service is not in JSON, parameters of the operations can not contain complex type.

    Reply to the post for SOAP and RESTful POX(XML)

    For plain old XML as return format, this is an example that would work both for SOAP and XML.

    [ServiceContract(Namespace = "http://test")]
    public interface ITestService
    {
        [OperationContract]
        [WebGet(UriTemplate = "accounts/{id}")]
        Account[] GetAccount(string id);
    }
    

    POX behavior for REST Plain Old XML

    
      
    
    

    Endpoints

    
      
        
        
      
    
    

    Service will be available at

    • http://www.example.com/soap
    • http://www.example.com/xml

    REST request try it in browser,

    http://www.example.com/xml/accounts/A123

    SOAP request client endpoint configuration for SOAP service after adding the service reference,

      
        
      
    

    in C#

    TestServiceClient client = new TestServiceClient();
    client.GetAccount("A123");
    

    Another way of doing it is to expose two different service contract and each one with specific configuration. This may generate some duplicates at code level, however at the end of the day, you want to make it working.

提交回复
热议问题