【问题标题】:Generate sample data for WCF REST services?为 WCF REST 服务生成示例数据?
【发布时间】:2012-06-09 21:55:29
【问题描述】:

是否有某种方法可以基于 WCF REST 接口生成示例 XML/JSON?大多数情况下,使用我们的 Web 服务的设备将消息反序列化为它的相关对象。但是有时这是不可能的,因此我需要向开发人员发送他们需要提供给服务的实际 XML/JSON 以及输出的样子。有没有一种简单的方法来生成这些信息,即使它使用数据类型的默认值?

webservice接口示例:

    [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);

在上面的示例中,我需要查看 ResultOfAction、LoginRequest 和 RequestOneTimePIN 序列化 XML。有没有生成此类信息的简单方法?

【问题讨论】:

    标签: c# xml json wcf rest


    【解决方案1】:

    在配置中设置helpEnabled="true" 属性时,WCF 4.0 将根据您从服务方法调用返回的格式生成示例数据:

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

    这是来自 MSDN 的 example

    【讨论】:

    • 没办法!这真是令人印象深刻!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多