WebGet 的 WCF 响应格式 [英] WCF ResponseFormat For WebGet

查看:27
本文介绍了WebGet 的 WCF 响应格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WCF 为 ServiceContract 的 WebGet 注释中的 ResponseFormat 属性提供了两个选项.

WCF offers two options for ResponseFormat attribute in WebGet annotation in ServiceContract.

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(UriTemplate = "greet/{value}", BodyStyle = WebMessageBodyStyle.Bare)]
    string GetData(string value);

    [OperationContract]
    [WebGet(UriTemplate = "foo", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    string Foo();

ResponseFormat 的选项是 WebMessageFormat.Json 和 WebMessageFormat.Xml.是否可以编写我自己的 Web 消息格式?我希望当客户端调用 foo() 方法时他得到原始字符串 - 没有 json 或 xml 包装器.

The options for ResponseFormat are WebMessageFormat.Json and WebMessageFormat.Xml. Is it possible to write my own web message format? I would like that when client calls foo() method he gets raw string - without json or xml wrappers.

推荐答案

WebGetAttribute 由 Microsoft 提供,我认为您无法扩展 WebMessageFormat.但是,您可能可以扩展 WebHttpBinding 使用 WebGetAttribute.您可以添加自己的属性,如

WebGetAttribute is shipped by Microsoft, and I don't think you can extend WebMessageFormat. However you could probably extend the WebHttpBinding that uses WebGetAttribute. You could add your own attribute like

[WebGet2(UriTemplate = "foo", ResponseFormat = WebMessageFormat2.PlainText)]
string Foo();

通常,在 WCF 中自定义消息布局称为自定义消息编码器/编码.Microsoft 提供了一个示例:自定义消息编码器:压缩编码器.人们做的另一个常见扩展是扩展行为以添加自定义错误处理,因此您可以在该方向寻找一些示例.

In general, customizing the message layout in WCF is called custom message encoder/encoding. Microsoft provides an example: Custom Message Encoder: Compression Encoder. Also another common extension people do is to extend behavior to add custom error handling, so you could look for some example in that direction.

这篇关于WebGet 的 WCF 响应格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆