c# - 为 WCF REST 服务生成示例数据?

标签 c# xml json wcf rest

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

网络服务接口(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);

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

最佳答案

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

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

这是一个 example来自 MSDN。

关于c# - 为 WCF REST 服务生成示例数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10904808/

相关文章:

c# - 保护应用程序和后台代理之间共享的等存储数据

c# - ADO.Net DataTables 有索引吗?

java - 接口(interface)和 jaxb

java - 如何使用响应格式根据参数而变化的改造?

c# - 如何在 C# 中转换为 XML 之前验证 JSON 字符串

c# - 如何在 C# .Net 4.6 WPF 应用程序中隐藏 ConnectionString

c# Azure无法设置blob层

java - 与 XML 配置相比,为什么 Java Config 在 spring boot 中更受欢迎?

php - 导入 XML 节点并删除命名空间

java - 如何使用 GSON 序列化/反序列化部分 JSON 对象?