【问题标题】:How to call WCF REST/JSON Service from client application如何从客户端应用程序调用 WCF REST/JSON 服务
【发布时间】:2011-10-13 17:16:08
【问题描述】:

我有 WCF REST/JSON 服务,我使用 this 模板创建它。在我的服务中,我有一个方法

 [WebInvoke(UriTemplate = "Create", Method = "*",RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)]
    public void Create(PictureData pictureData)
    {
        var context = new EFDBContext();
        context.PictureData.Add(pictureData);
        context.SaveChanges();
    }

PictureData 这是我的实体数据,我尝试通过 EF 将其保存在 DB 中。

在我的 WPF 客户端应用程序中,我尝试调用此方法:

using (var client = new HttpClient("http://localhost:8080/ScreenPictureService/Create"))
        {
            var dataContract = HttpContentExtensions.CreateJsonDataContract(pictureData);
            client.Post("", dataContract);
        }

但什么也没发生

  • 我也尝试在 WebInvoke 属性中使用 Method="POST"
  • 另外我尝试在 HttpClient 中使用没有“Create”的地址,然后在 client.Post 中使用它在第一个参数中

更新

我试了之后

var dataContract = HttpContentExtensions.CreateJsonDataContract(pictureData, typeof (PictureData));
        var client = new HttpClient();
        using(var response = client.Post("http://localhost:8080/ScreenPictureService/Create", dataContract))
        {
            response.EnsureStatusIs(HttpStatusCode.OK);
        }

我收到了错误的请求 400

更新 2 我发现了我的问题:

  • 我使用JSON.NET 序列化我的对象,当我收到字节数组时它会转换为base64 格式,但我的服务需要字节数组 - 它解决了使用字节列表。

  • 第二个问题 - 我尝试以高清晰度接收我的桌面截图,并且我有相同的响应(错误请求 400),如果我将图片分辨率更改为 800x600,服务运行良好,并且有我的问题 - 如何增加请求消息的配额。我尝试使用,在标准端点部分(web.config)内

readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"

但它不起作用

【问题讨论】:

  • 什么也没发生是什么意思?有什么错误吗?你检查过事件日志吗?
  • 我的意思是,没有收到任何错误。

标签: c# wcf json rest


【解决方案1】:

您是否尝试过使用 Fiddler 之类的工具监控确切的请求/响应?也许您的帖子与您期望的不一样?

WCF 服务是否知道接受 REST?如果您不使用WCF.WebApi,通常需要配置可怕的 wcf 绑定,例如:

<service name="MyWcfServiceWebRole.xyz.IAbcService">
    <endpoint address="" behaviorConfiguration="webby" binding="webHttpBinding" bindingConfiguration="RestBinding" contract="MyWcfServiceWebRole.xyz.IAbcService" />
</service>

<behaviors>
   <endpointBehaviors>
      <behavior name="webby">
         <webHttp />
      </behavior>
   </endpointBehaviors>
</behaviors>

简单的 REST Get 是否有效?

【讨论】:

  • 我尝试使用 Fiddler 进行监控,它也收到错误请求 400。是的,一个简单的 REST 效果很好。
【解决方案2】:

我建议Hammock,它很简单而且效果很好。

【讨论】:

    【解决方案3】:

    错误 400 Bad request 可能有多种原因。尝试在您的服务上启用跟踪。可以通过链接here来完成。

    另外,如果你有一个配置文件,如果你使用 webHttpBinding,请确保你的 readerQuotas 如下所示:

    <webHttpBinding>
        <binding name="RestBinding">
          <readerQuotas maxStringContentLength="5242880" maxArrayLength="16384"
            maxBytesPerRead="4096" />
          <security mode="None">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    

    如果您使用 REST API 并通过 global.asax 中的路由定义您的服务并使用标准端点,请使用以下:

    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true">
        <readerQuotas maxStringContentLength="5242880" maxArrayLength="4194304"
                maxBytesPerRead="4194304" />
    </standardEndpoint>
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多