从具有webHttpBinding的服务中获取乱码而不是Hello World [英] Getting Gibberish instead of Hello World from a service with webHttpBinding

查看:132
本文介绍了从具有webHttpBinding的服务中获取乱码而不是Hello World的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的示例,应该返回"Hello World"字符串.但是,浏览器显示类似SGVsbG8gV29ybGQ=的内容.从oldskul样式的服务返回纯文本的正确方法是什么?

Here is a trivial example that is supposed to return "Hello World" string. However, a browser displays something like SGVsbG8gV29ybGQ=. Which is the right way to return plain text from an oldskul-style service?

请知道:

  1. 我无法返回字符串:三个Unicode字符将被自动添加,并且旧版HTTP客户端将无法进行互操作.

  1. I can't return a string: three Unicode characters will be automatically prepended and the legacy HTTP client won't be able to interoperate.

我可以返回Message,但是仍然必须保持解析功能以提取data变量.不允许在同一方法签名中混合Messageint类型的AFAIK.

I could return a Message, but still must keep parsing functionality to extract the data variable. Mixing Message and int types in the same method signature is not allowed AFAIK.


[ServiceContract(SessionMode=SessionMode.NotAllowed)] 
public interface IHello 
{
  [WebGet(UriTemplate = "manager?data={data}")]
  [OperationContract]
  Byte[] DoIt(int data);
}

public class Hello : IHello { public Byte[] DoIt(int data) { return Encoding.ASCII.GetBytes("HelloWorld"); } }

public class Hello : IHello { public Byte[] DoIt(int data) { return Encoding.ASCII.GetBytes("HelloWorld"); } }

更新:这不是乱码,但响应编码正确.但是,格式不是我希望收到的格式.我已经弄清楚(如下面的先生们所建议的那样),对传输层消息传递格式的最终​​控制是通过Message类获得的.但是,如果我确实使用一个-我就失去了解析请求(UriTemplate属性)的可能性.因此,很高兴知道如何将MessageUriRequest集成.

UPDATE: It's not gibberish, but correctly encoded response. However, the format is not what I'd expect to receive. I've figured out (as gentlemen below suggest) that the ultimate control of the transport layer messaging format is gained with Message class. However, if I do use one - I loose the possibility of parsing the request (UriTemplate attribute). Hence, it would be great to know how to integrate the Message with UriRequest.

P.S..如果无法实现干净"的集成,那么最优雅的解决方法是什么?请问有什么可以在我的实现中借鉴和使用的幕后代码吗?

P.S. If the "clean" integration is not possible - than what's the most elegant workaround? Is there any code that is done behind the wire curtain that I can borrow and use in my implementation, please?

推荐答案

在Googled中找到了正确的整合方式,发现人们像我一样被困住了(如果我错了,请纠正我).到目前为止,以下变通办法对我而言还行得通:

Googled for a proper way to integrate and found that people are stuck just as I am (please correct me if I am wrong on that). So far the following workaround works fine for me:

  public class Hello : IHello
  {
        [WebGet(UriTemplate = "manager")]
        [OperationContract]
        Message DoIt();
  }

  public class Hello : IHello
  {
        public Message DoIt()
        {
          var webContext = WebOperationContext.Current;
          var request = webContext.IncomingRequest;
          var queryParameters = request.UriTemplateMatch.QueryParameters;
          var data = queryParameters["data"];

          var result = new StringBuilder(@"
              <?xml version='1.0'?>
              ...
          ");

          var response = webContext.CreateTextResponse(result.ToString(), "application/xml", Encoding.ASCII);

          return response;
        }
  }

如果有更好的选择-知道这一点将是完美的.

If there is a better alternative - it would be perfect to know about it.

这篇关于从具有webHttpBinding的服务中获取乱码而不是Hello World的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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