简体   繁体   中英

Generate sample data for WCF REST services?

Is there some way to generate sample XML/JSON based on a WCF REST interface? Most of the time the devices that consume our web services deserialize the message into it's relevant object. However some times that is not possible and as such I need to send developers the actual XML/JSON that they need to give to the services and what the output looks like. Is there an easy way to generate this information, even if it it uses the datatypes default values?

An example of a webservice interface:

    [OperationContract]
    [WebGet(UriTemplate = "Test", ResponseFormat = WebMessageFormat.Xml)]
    ResultOfAction Test();

    // used to login
    [OperationContract]
    [WebInvoke(UriTemplate = "Login?", Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    ResultOfAction Login(LoginRequest request);

    // register a client + forgot password
    [OperationContract]
    [WebInvoke(UriTemplate = "RequestOTP?", Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    ResultOfAction RequestOTP(RequestOneTimePIN requestOneTimePin);

In the example above, I would need to see the ResultOfAction, LoginRequest and RequestOneTimePIN serialzed XML. Is there a simple way of generating such info?

When the helpEnabled="true" attribute is set in the config, WCF 4.0 will generate sample data based on the format you are returning from the service method call:

<behaviors>  
    <endpointBehaviors>
        <behavior name="webHttpBehavior">
            <webHttp helpEnabled="true"/>
        </behavior>
    </endpointBehaviors> 
</behaviors>

Here's an example from MSDN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM