用于WebGet的WCF ResponseFormat [英] WCF ResponseFormat For WebGet

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

问题描述

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.是否可以编写自己的网络消息格式?我希望当客户端调用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 ResponseFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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