2011-10-13 30 views
1

我有WCF REST/JSON服务,我使用this模板创建它。在我的服务,我有一个方法如何从客户端应用程序调用WCF REST/JSON服务

[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客户端应用程序,我尝试调用这个方法:

​​

但没有发生

  • 我也尝试使用method = “POST” 在WebInvoke属性
  • 此外,我尝试使用HttpClient中没有“创建”的地址,然后在客户端中使用它。第一个参数

UPDATE

后,我尝试这个

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,服务运行良好,并且存在我的问题 - 如何增加请求消息的配额。我尝试哟使用,里面standardEndpoint节(web.config中)

readerQuotas的MaxArrayLength = “2147483647” maxBytesPerRead = “2147483647” MAXDEPTH = “2147483647” maxNameTableCharCount = “2147483647” maxStringContentLength = “2147483647”

但它不起作用

+0

你是什么意思?有没有错误?你有没有检查事件日志? –

+0

我的意思是,没有收到错误。 –

回答

0

您是否尝试过使用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来上班啊?

+0

我尝试使用Fiddler进行监控,它也收到错误的请求400.是的,简单的REST运行良好。 –

0

错误400错误的请求可能是由于许多可能性。尝试对您的服务启用跟踪。可以通过链接here完成。

此外,如果你有配置的配置文件请确保您有readerQuotas sepecified如下,如果你正在使用的WebHttpBinding:

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

如果您使用的是REST API与全球路线定义为您服务.asax并使用下面的标准端点使用:

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