How to produce XML output using WCF service?

前端 未结 2 2042
误落风尘
误落风尘 2021-01-03 05:13

I\'ve set up the following interface.

[ServiceContract]
public interface IService1
{
  [OperationContract]
  String Ping();
}

Its implement

2条回答
  •  [愿得一人]
    2021-01-03 05:55

    I finally got totally PO and went off to business full contact. This is what I've produced - it works on my machine and I hope it's not a local phenomenon. :)

    IRestService.cs - the declaration, what your code promises to a contacting client

    [ServiceContract]
    public interface IRestService
    {
      [OperationContract]
      [WebInvoke(Method = "GET", 
        ResponseFormat = WebMessageFormat.Xml, 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        UriTemplate = "xml/{id}")]
      String XmlData(String id);
    
      [OperationContract]
      [WebInvoke(Method = "GET", 
        ResponseFormat = WebMessageFormat.Json, 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        UriTemplate = "json/{id}")]
      String JsonData(String id);
    }
    

    RestService.svc.cs - the implementation, what your code actually does to the client

    public class RestService : IRestService
    {
      public String XmlData(String id)
      {
        return "Requested XML of id " + id;
      }
    
      public String JsonData(String id)
      {
        return "Requested JSON of id " + id;
      }
    }
    

    Web.config - the configuration, what your code is handled as on the way to the client

    
    
      
        
      
    
      
        
          ...
        
        
        
      
    
    
    

    services - contents of the tag describing the service's nature

    
      
    
    

    behaviors - contents of the tag describing the behavior of the service and the end-point

    
      
        
        
      
    
    
    
      
        
      
    
    

    Index.html - the executor, what your code can be called as

    
      
        
        
      
      
        ...
      
    
    

    script - contents of the tag describing the executable in JavaScript

    window.onload = function () {
      document.getElementById("xhr").onclick = function () {
        var xhr = new XMLHttpRequest();
        xhr.onload = function () { alert(xhr.responseText); }
        xhr.open("GET", "RestService.svc/xml/Viltersten");
        xhr.send();
      }
    }
    

    style - contents of the tag describing the appearance

    .clickable
    {
      text-decoration: underline;
      color: #0000ff;
    }
    

    body - contents of the tag describing the markup structure

    • XML output here
    • JSON output here
    • XHR output here

    Everything is stored in a project called DemoRest. I created my own files for declaration and implementation of the service, removing the default ones. The directives of using as well as the XML version declaration are omitted for spacial reasons.

    Now the response can be retrieved using the following URL.

    localhost:12345/RestService.svc/xml/Konrad
    localhost:12345/RestService.svc/json/Viltersten
    
    1. Does anybody else get it to work too?
    2. Any suggestions on improvement or clarification?

提交回复
热议问题