首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于WebGet的WCF ResponseFormat

用于WebGet的WCF ResponseFormat
EN

Stack Overflow用户
提问于 2009-06-14 09:42:17
回答 4查看 31K关注 0票数 23

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

代码语言:javascript
复制
[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包装器。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-06-19 02:43:38

WebGetAttribute是微软提供的,我不认为你可以扩展WebMessageFormat。但是,您可以扩展使用WebGetAttributeWebHttpBinding。你可以添加你自己的属性,比如

代码语言:javascript
复制
[WebGet2(UriTemplate = "foo", ResponseFormat = WebMessageFormat2.PlainText)]
string Foo();

通常,在WCF中自定义消息布局称为自定义消息编码器/编码。微软提供了一个例子:Custom Message Encoder: Compression Encoder。此外,人们做的另一个常见扩展是扩展行为,以添加自定义错误处理,因此您可以在该方向上寻找一些示例。

票数 8
EN

Stack Overflow用户

发布于 2009-06-19 03:49:11

尝试使用

代码语言:javascript
复制
BodyStyle = WebMessageBodyStyle.Bare

然后从你的函数返回一个System.IO.Stream。

下面是我用来从数据库中返回图像的一些代码,但可以通过URL访问:

代码语言:javascript
复制
[OperationContract()]
[WebGet(UriTemplate = "Person/{personID}/Image", BodyStyle = WebMessageBodyStyle.Bare)]
System.IO.Stream GetImage(string personID);

实施:

代码语言:javascript
复制
public System.IO.Stream GetImage(string personID)
{
    // parse personID, call DB

    OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;

    if (image_not_found_in_DB)
    {
        context.StatusCode = System.Net.HttpStatusCode.Redirect;
        context.Headers.Add(System.Net.HttpResponseHeader.Location, url_of_a_default_image);
        return null;
    }

    // everything is OK, so send image

    context.Headers.Add(System.Net.HttpResponseHeader.CacheControl, "public");
    context.ContentType = "image/jpeg";
    context.LastModified = date_image_was_stored_in_database;
    context.StatusCode = System.Net.HttpStatusCode.OK;
    return new System.IO.MemoryStream(buffer_containing_jpeg_image_from_database);
}

在您的示例中,要返回原始字符串,请将ContentType设置为类似于“文本/纯文本”的内容,并以流的形式返回数据。据猜测,大概是这样的:

代码语言:javascript
复制
return new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(string_to_send));
票数 49
EN

Stack Overflow用户

发布于 2014-12-19 17:09:03

我像这样实现了这个属性,也许将来它会对某些人有所帮助:

代码语言:javascript
复制
[AttributeUsage(AttributeTargets.Method)]
public class WebGetText : Attribute, IOperationBehavior
{

    public void Validate(OperationDescription operationDescription)
    {
    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        dispatchOperation.Formatter = new Formatter(dispatchOperation.Formatter);
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
    }

    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {
    }
}

public class Formatter : IDispatchMessageFormatter
{
    IDispatchMessageFormatter form;

    public Formatter (IDispatchMessageFormatter form)
    {
         this.form = form;
    }

    public void DeserializeRequest(Message message, object[] parameters)
    {
        form.DeserializeRequest(message, parameters)
    }

    public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
    {
        IEnumerable<object> cl = (IEnumerable<object>)result;
        StringBuilder csvdata = new StringBuilder();


        foreach (object userVariableClass in cl) {
            Type type = userVariableClass.GetType();
            PropertyInfo[] fields = type.GetProperties();

            //            Dim header As String = String.Join(";", fields.Select(Function(f) f.Name + ": " + f.GetValue(userVariableClass, Nothing).ToString()).ToArray())
            //            csvdata.AppendLine("")
            //            csvdata.AppendLine(header)
            csvdata.AppendLine(ToCsvFields(";", fields, userVariableClass));
            csvdata.AppendLine("");
            csvdata.AppendLine("=====EOF=====");
            csvdata.AppendLine("");
        }
        Message msg = WebOperationContext.Current.CreateTextResponse(csvdata.ToString());
        return msg;
    }

    public static string ToCsvFields(string separator, PropertyInfo[] fields, object o)
    {
        StringBuilder linie = new StringBuilder();

        foreach (PropertyInfo f in fields) {
            if (linie.Length > 0) {
            }

            object x = f.GetValue(o, null);

            if (x != null) {
                linie.AppendLine(f.Name + ": " + x.ToString());
            } else {
                linie.AppendLine(f.Name + ": Nothing");
            }
        }

        return linie.ToString();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/992533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档