WebGet 的 WCF 响应格式

发布于 2024-07-24 01:01:10 字数 586 浏览 6 评论 0原文

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

[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 包装器。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

儭儭莪哋寶赑 2024-07-31 01:01:10

尝试使用

BodyStyle = WebMessageBodyStyle.Bare

then 从您的函数返回 System.IO.Stream 。

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

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

实现:

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 设置为“text/plain”之类的内容并将数据作为流返回。 猜测是这样的:

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

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));
掩饰不了的爱 2024-07-31 01:01:10

WebGetAttribute 是由 Microsoft 提供,我认为您无法扩展 WebMessageFormat。 但是,您可以扩展 WebHttpBinding< /a> 使用 WebGetAttribute。 您可以添加自己的属性,例如

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

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

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.

白昼 2024-07-31 01:01:10

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

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

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();
    }
}
一杆小烟枪 2024-07-31 01:01:10

如果您正在处理 HTTP,那么有一种方法可以实现此目的,这不太好,但我想我可以提到它。

您可以将方法的返回类型设置为 void,然后将原始字符串直接输出到响应中。

[OperationContract]
[WebGet(UriTemplate = "foo")]
void Foo()
{
   HttpContext.Current.Response.Write("bar");
}

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文