WCF 400 Bad Request

2019-06-24 08:46发布

I created a simple function

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

Definition,

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

From browser using Javascript/Jquery, http://localhost/service1.svc tells me I have created a service and all other info.. Looks fine.
I'm trying to call this using http://localhost/service1.svc/Start

I get a 400 bad request for this call. I hope I'm not doing something totally wrong here. I should be able to access WCF service from browser right? I tried looking a lot before I thought of posting. But I'm unable to get this basic thing working is frustrating me.

EDIT & UPDATE Now I'm at this stage. The service page is telling me that the metadata service is disabled and is asking me to insert the following text

   <serviceMetadata httpGetEnabled="true" />

I inserted the text - but still it shows the same text!! This is getting too confusing now..

2条回答
家丑人穷心不美
2楼-- · 2019-06-24 09:18

Works for me. I created WCF Rest Service.

I use URL which looks like http://localhost:8080/Service1/Start

Here is the code:

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");
        }
    }
}
查看更多
戒情不戒烟
3楼-- · 2019-06-24 09:36

Try to change POST with GET and restart the request

查看更多
登录 后发表回答