REST / SOAP endpoints for a WCF service

前端 未结 6 1539
囚心锁ツ
囚心锁ツ 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:05

    This post has already a very good answer by "Community wiki" and I also recommend to look at Rick Strahl's Web Blog, there are many good posts about WCF Rest like this.

    I used both to get this kind of MyService-service... Then I can use the REST-interface from jQuery or SOAP from Java.

    This is from my Web.Config:

    
     
      
       
       
       
      
     
     
      
       
        
        
       
      
      
       
        
       
      
     
    
    

    And this is my service-class (.svc-codebehind, no interfaces required):

        ///  MyService documentation here ;) 
    [ServiceContract(Name = "MyService", Namespace = "http://myservice/", SessionMode = SessionMode.NotAllowed)]
    //[ServiceKnownType(typeof (IList))]
    [ServiceBehavior(Name = "MyService", Namespace = "http://myservice/")]
    public class MyService
    {
        [OperationContract(Name = "MyResource1")]
        [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "MyXmlResource/{key}")]
        public string MyResource1(string key)
        {
            return "Test: " + key;
        }
    
        [OperationContract(Name = "MyResource2")]
        [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource/{key}")]
        public string MyResource2(string key)
        {
            return "Test: " + key;
        }
    }
    

    Actually I use only Json or Xml but those both are here for a demo purpose. Those are GET-requests to get data. To insert data I would use method with attributes:

    [OperationContract(Name = "MyResourceSave")]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource")]
    public string MyResourceSave(string thing){
        //...
    

提交回复
热议问题