WCF ResponseFormat For WebGet

六月ゝ 毕业季﹏ 提交于 2019-11-27 07:07:06

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

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.

Try using

BodyStyle = WebMessageBodyStyle.Bare

Then return a System.IO.Stream from your function.

Here's some code I use to return an image out of a database, but accessible via a URL:

[OperationContract()]
[WebGet(UriTemplate = "Person/{personID}/Image", BodyStyle = WebMessageBodyStyle.Bare)]
System.IO.Stream GetImage(string personID);

Implementation:

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

In your case, to return a raw string, set the ContentType to something like "text/plain" and return your data as a stream. At a guess, something like this:

return new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(string_to_send));

I implemented this attribute like this, maybe it will help someone in the future:

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

There is one way how to achieve this if you're dealing with HTTP, it's not exactly nice, but I thought I could mention it.

You can set the return type of your method to void and just output your raw string directly into the response.

[OperationContract]
[WebGet(UriTemplate = "foo")]
void Foo()
{
   HttpContext.Current.Response.Write("bar");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!