【问题标题】:WCF REST can not parse Request BodyWCF REST 无法解析请求正文
【发布时间】:2014-06-10 23:13:36
【问题描述】:

我在使用 WCF REST 解析请求正文时遇到了困难。 我的问题:

  • POST 方法 https://{Service}/X/Y? Z= 0000000
  • 正文请求:{"id":"id"}

我的代码:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
                BodyStyle = WebMessageBodyStyle.Bare,
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                UriTemplate = "/X/{y}?Z={z}")
                ]
    string GetRequest(string y, string z, Request request);
}
public class Service : IService
{
    public string GetRequest(string y, string z, Request request)
    {
        //Do sth
    }


[DataContract]
public class Request
{
    [DataMember]
    [JsonProperty("id")]
    public String  Id { get; set; }
}

我遇到的问题是 y 和 z 有数据并且它们是正确的,但是 请求中的 id 为空。我希望是“id”。

我在互联网上进行了很多搜索,发现在这种情况下不容易遵循的 Stream 解决方案。 我想知道是否有人有一个聪明的主意来做到这一点。

【问题讨论】:

  • 奇怪...类似的东西对我有用,但我什至没有设置 BodyStyle、RequestFormat 和 ResponseFormat。
  • 更奇怪的是,您不需要它们并且它可以工作。你有什么特别的参考或什么可以称为特别的吗?
  • 你能告诉我你的 web.config 怎么样吗?

标签: wcf wcf-rest


【解决方案1】:

看看这个版本

服务

namespace ServiceTest
{
    internal class Program
    {
        private static WebServiceHost _service;

        private static void Main()
        {
            _service = new WebServiceHost(typeof (Service));
            Console.WriteLine("The service has started");
            _service.Open();
            Console.ReadKey();
        }
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public sealed class Service : IService
    {
        public string GetRequest(string y, string z, Request request)
        {
            Console.WriteLine("Y: {0}, Z: {1}, request: {2}", y, z, request);
            return "Ok";
        }
    }

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json, UriTemplate = "/X/{y}?Z={z}")]
        string GetRequest(string y, string z, Request request);
    }

    [DataContract]
    public sealed class Request
    {
        [DataMember]
        public string Id { get; set; }

        public override string ToString()
        {
            return string.Format("Id: {0}", Id);
        }
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <system.serviceModel>
        <services>
            <service name="ServiceTest.Service">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:9090/webhost" />
                    </baseAddresses>
                </host>
                <endpoint binding="webHttpBinding" contract="ServiceTest.IService" />
            </service>
        </services>
    </system.serviceModel>

    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

客户

internal class Program
{
    private static void Main(string[] args)
    {
        var client = new HttpClient();
        var request = new Request {Id = "Nelibur"};
        var result = client.PostAsync("http://localhost:9090/webhost/X/Y?Z=2000", CreateContent(request)).Result;
        Console.WriteLine(result.Content.ReadAsStringAsync().Result);
        Console.ReadKey();
    }

    private static StringContent CreateContent<T>(T value)
    {
        using (var stream = new MemoryStream())
        {
            var serializer = new DataContractJsonSerializer(typeof (T));
            serializer.WriteObject(stream, value);
            string content = Encoding.UTF8.GetString(stream.ToArray());
            return new StringContent(content, Encoding.UTF8, "application/json");
        }
    }
}

原始请求:

POST http://localhost:9090/webhost/X/Y?Z=2000 HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: localhost:9090
Content-Length: 16
Expect: 100-continue
Connection: Keep-Alive

{"Id":"Nelibur"}

您可以尝试使用请求,例如Fiddler

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-09
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 2021-01-16
    • 1970-01-01
    • 2023-02-03
    相关资源
    最近更新 更多