2011-09-20 262 views
4

我创建使用Javascript/jQuery的一个简单的函数WCF 400错误的请求

 [OperationContract] 
     [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)] 
     string Start(); 

定义,

 public String Start() 
     { 
      JavaScriptSerializer serializer = new JavaScriptSerializer(); 
      return serializer.Serialize("Check"); 
     } 

从浏览器, http://localhost/service1.svc告诉我,我已经创建了一个服务和其他所有信息..看起来很好。
我试图把这种使用 http://localhost/service1.svc/Start

我得到这个呼叫的400错误的请求。我希望我在这里做的事情没有完全错误。我应该能够从浏览器访问WCF服务吗? 在我想要发布之前,我尝试了很多。但我无法得到这个基本的东西令我感到沮丧。

编辑& UPDATE 现在我在这个阶段。该服务页面告诉我,元数据服务被禁用,并要求我插入下面的文本

<serviceMetadata httpGetEnabled="true" /> 

我插入的文本 - 但它毕竟显示了相同的文字!现在,这是越来越太混乱..

+0

你有与状态代码的任何有效载荷?如果否,请尝试打开将错误消息发送回客户端的选项。它在ServiceBehaviors/Behavior元素下是。 –

+0

它设置为true。但我没有得到任何有效载荷。只是状态码。 – Tom

+0

@ a.dimo是对的,你指定你的方法只能用POST方法调用。当您使用浏览器请求时,它会发送GET请求。 –

回答

1

尝试使用GET改变后并重新启动要求

+0

Nopes。这没有帮助。我已经尝试过每一种组合 - 开始,开始(int id),GET和POST – Tom

+0

你为什么认为将POST改为GET会有所帮助? – Tom

1

为我工作。我创建了WCF Rest服务。

我使用的URL看起来像http://localhost:8080/Service1/Start

下面是代码:

using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
using System.Web.Script.Serialization; 

namespace WcfRestService1 
{ 
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page 
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want 
    // a single instance of the service to process all calls. 
    [ServiceContract] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    // NOTE: If the service is renamed, remember to update the global.asax.cs file 
    public class Service1 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
     public string Start() 
     { 
      JavaScriptSerializer serializer = new JavaScriptSerializer(); 
      return serializer.Serialize("Check"); 
     } 
    } 
} 
+0

嗯..我无法确定在我的代码中出了什么问题。 – Tom

+0

你创建了什么样的项目?只需WCF服务应用程序(在VS中的WCF选项卡下)或WCF REST服务应用程序(在Web选项卡下)。 –

+0

从WCF - > WCF服务应用程序>我做了一些更多的调查,这里是我发现.. 无法导入WSDL:portType的详细 – Tom