WCF 400 错误请求

发布于 2024-12-05 18:21:19 字数 950 浏览 2 评论 0原文

我创建了一个简单的函数

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

定义,

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

使用 Javascript/Jquery 从浏览器, http://localhost/service1.svc 告诉我我已经创建了一个服务和所有其他信息..看起来不错。
我正在尝试使用它来调用 http://localhost/service1.svc/Start

我收到此呼叫的 400 错误请求。我希望我在这里没有做完全错误的事情。我应该能够从浏览器访问 WCF 服务,对吧? 在我想到发帖之前,我尝试了很多。但我无法让这个基本的事情发挥作用,这让我很沮丧。

编辑&更新 现在我正处于这个阶段。服务页面告诉我元数据服务已禁用,并要求我插入以下文本

   <serviceMetadata httpGetEnabled="true" />

我插入了文本 - 但它仍然显示相同的文本!现在这变得太混乱了..

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-12-12 18:21:19

尝试将 POST 改为 GET 并重新启动请求

Try to change POST with GET and restart the request

柠檬 2024-12-12 18:21:19

对我有用。我创建了 WCF 休息服务。

我使用的 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");
        }
    }
}

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");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文