2011-03-21 110 views
11

寻找一个WCF 4 REST服务一些指导其是基于WCF REST模板40(CS)在VS2010扩展。我花了几天的时间试图让这个玩家工作,审查其他职位,而当我接近时,我似乎无法越过终点线。经过多次挫折之后,它终于到达了服务并发布(使用fiddler请求构建器),但方法参数是空的,但它在请求生成器中正确设置。我猜测这可能是配置问题,但随着截止日期的到来,我没有时间进行更多的研究。 FWIW,在调试时,jsonstring变量为空。自我承认是一种noob问题,因为这是第一次通过REST为我,任何帮助将不胜感激!WCF REST服务的JSON数据后

在此先感谢。

的web.config

<system.web> 
    '<compilation debug="true" targetFramework="4.0" /> 
</system.web> 

<system.webServer> 
<modules runAllManagedModulesForAllRequests="true"> 
    <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
</modules> 
</system.webServer> 

<system.serviceModel> 
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
<standardEndpoints> 
    <webHttpEndpoint> 
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> 
    </webHttpEndpoint> 
</standardEndpoints> 
</system.serviceModel> 

的global.asax.cs

public class Global : HttpApplication 
    { 
     void Application_Start(object sender, EventArgs e) 
     { 
     RegisterRoutes(); 
     } 

     private void RegisterRoutes() 
     { 
     RouteTable.Routes.Add(new ServiceRoute("Scoring", new WebServiceHostFactory(), typeof(ScoringSvc))); 
     } 
    } 

服务代码

[ServiceContract] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
public class ScoringSvc 
{ 
    [OperationContract] 
    [WebInvoke 
     (Method = "POST", 
     BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     RequestFormat=WebMessageFormat.Json, 
     ResponseFormat=WebMessageFormat.Json)] 
    public string BOB(string jsonstring) 
    { 
     return "Received: " + jsonstring; 
    } 
} 

的Fiddler请求头

Host: localhost 
Content-Length: 20 
Content-Type: application/json; charset=UTF-8 

请求主体

{"Name":"Frank"} 

原料从小提琴手响应

HTTP/1.1 200 OK 
Cache-Control: private 
Content-Length: 12 
Content-Type: application/json; charset=utf-8 
Server: Microsoft-IIS/7.5 
X-AspNet-Version: 4.0.30319 
X-Powered-By: ASP.NET 
Date: Mon, 21 Mar 2011 21:31:14 GMT 

"Received: " 
+0

忘了提及,解决方案使用IIS 7作为web服务器而不是apnet调试服务器。 – Grogh 2011-03-21 22:05:25

回答

20

偶然发现这个链接WCF + REST: Where is the request data?,看到格伦的响应流传递给方法,然后撕裂,除了有一个StreamReader成一个字符串来获取表单POST数据。

修改原型服务代码如下

[OperationContract] 
[WebInvoke 
    (UriTemplate="/BOB", 
    Method = "POST", 
    BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
public string BOB (Stream streamdata) 
{ 
    StreamReader reader = new StreamReader(streamdata); 
    string res = reader.ReadToEnd(); 
    reader.Close(); 
    reader.Dispose(); 
    return "Received: " + res; 
} 

而且似乎这样的伎俩,充分JSON数组在流中传递,读取到本地字符串,然后我可以使用JSON攻击它.net将序列化到/从字典传递到业务逻辑。不是很漂亮,但功能。

+0

发现调用“关闭”和/或“处置”会引起响应,导致服务故障“消息已处理” – 2017-04-14 12:47:39

-1

你有没有试过[WebGet(UriTemplate = ..]属性,而不是帖子看看是否能够正常工作?下面是一个例子 - http://blogs.msdn.com/b/kaevans/archive/2007/09/04/creating-a-json-service-with-webget-and-wcf-3-5.aspx

+0

我确实有一个定义的webget方法,如预期的那样返回,目前注释清理代码以专注于帖子。我有一个webinvoke方法的地方,并发现了一个帖子在stackoverflow(无法找回它现在),说不包括。在删除之前,我根本没有经历过,当时收到404,删除后我现在得到200个响应,但没有数据。如果我将它作为根(“”)或将端点放在 – Grogh 2011-03-22 02:44:27

+0

中,您似乎没有关系使用什么操作系统和版本的IIS或IIS Express或Web开发服务器?应用程序池设置了什么框架? 404错误可能是由于此处相关的问题http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/2ec269e3-c1ff-4d9b-9ff3-d530f1599047 – sep15ms 2011-03-22 02:52:03

+0

2008 r2 sp1 IIS 7与.Net 4通过aspnet_regiis.exe注册。任何指向IIS 7中添加引用的dll的指针?也试图在安装了IIS的win 7 SP1上,但我认为这是IIS 7.5 ...虽然仍然没有果汁。 – Grogh 2011-03-22 03:21:30

1

您是否尝试过进入{“jsonstring”:“弗兰克”}请求体(内提琴手的请求生成器)?

+0

我认为这是真正的答案。当包装时,json被反序列化为方法参数的名称。 – 2016-03-03 15:22:53

2

我用这一个工程:

[WebInvoke(ResponseFormat = WebMessageFormat.Json, 
      RequestFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.WrappedRequest, 
      Method = "POST", 
      UriTemplate = "setExpositions?shelfId={shelfId}")] 
[OperationContract] 
public bool SetExpositions(int shelfId, List<WcfExposition> expositions) 
{ 
} 

其中shelfId在GET传递,并博览会是在邮件正文中的JSON数据传递。

+8

您可以向我们展示一些适用于此操作的示例JSON数据吗? – 2013-03-05 17:01:34

0

我认为在BodyStyle = WebMessageBodyStyle.WrappedRequest中可能会出现问题,我认为虽然文档完全不清楚,但希望元素可以用方法名称包装。

设置解包并将请求正文设置为'{"Name":"Frank"}'(注意围绕它的单引号。实际上发布的是包含JSON的字符串,我不知道为什么要这样做。它让我想起了http://thedailywtf.com/Articles/XMLd-XML.aspx他们在哪里把XML放在他们的XML。您正在将JSON放入您的JSON中。