【问题标题】:WCF REST Self-Hosted 400 Bad RequestWCF REST 自托管 400 错误请求
【发布时间】:2013-03-29 14:55:34
【问题描述】:

我在使用自托管 WCF REST 服务时遇到问题。

当我尝试通过浏览器或 Fiddler 发出 GET 时,我收到 400 Bad Request。跟踪正在报告 XmlException 的内部异常“消息的正文无法读取,因为它是空的。”

我在 app.config 中没有任何配置(我需要任何配置吗?)。我试过把WebServiceHost改成ServiceHost,返回WSDL,但是操作还是返回400。

我在这里错过了什么?

// Add Reference to System.ServiceModel and System.ServiceModel.Web
using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;

namespace WCFRESTTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var baseAddress = new Uri("http://localhost:8000/");
            var host = new WebServiceHost(typeof(RestService), baseAddress);

            try
            {
                host.AddServiceEndpoint(typeof(IRestService), new WSHttpBinding(), "RestService");

                var smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);

                host.Open();
                Console.WriteLine("Service Running.  Press any key to stop.");
                Console.ReadKey();
            }
            catch(CommunicationException ce)
            {
                host.Abort();
                throw;
            }
        }
    }

    [ServiceContract]
    public interface IRestService
    {
        [OperationContract]
        [WebGet(UriTemplate = "Test")]
        bool Test();
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class RestService : IRestService
    {
        public bool Test()
        {
            Debug.WriteLine("Test Called.");
            return true;
        }
    }
}

【问题讨论】:

标签: wcf rest self-hosting


【解决方案1】:

当您使用WebServiceHost 时,您通常不需要添加服务端点 - 它会添加一个具有使其成为“Web HTTP”(又名 REST)端点所需的所有行为的端点(即,不使用 SOAP,您可以使用 Fiddler 等工具轻松使用,这似乎是您想要的)。还有Web HTTP endpoints aren't exposed in the WSDL,所以你也不需要添加ServiceMetadataBehavior

现在解释为什么它不起作用 - 向 http://localhost:8000/Test 发送 GET 请求应该可以工作 - 在下面的代码中它可以工作。尝试运行此代码,并使用 Fiddler 发送您之前发送的请求,以查看差异。这应该指出你有什么问题。

public class StackOverflow_15705744
{
    [ServiceContract]
    public interface IRestService
    {
        [OperationContract]
        [WebGet(UriTemplate = "Test")]
        bool Test();
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class RestService : IRestService
    {
        public bool Test()
        {
            Debug.WriteLine("Test Called.");
            return true;
        }
    }

    public static void Test()
    {
        var baseAddress = new Uri("http://localhost:8000/");
        var host = new WebServiceHost(typeof(RestService), baseAddress);

        // host.AddServiceEndpoint(typeof(IRestService), new WSHttpBinding(), "RestService");

        // var smb = new ServiceMetadataBehavior();
        // smb.HttpGetEnabled = true;
        // host.Description.Behaviors.Add(smb);

        host.Open();

        WebClient c = new WebClient();
        Console.WriteLine(c.DownloadString(baseAddress.ToString().TrimEnd('/') + "/Test"));

        Console.WriteLine("Service Running.  Press any key to stop.");
        Console.ReadKey();
    }
}

【讨论】:

  • 我发誓我试过这个,但显然没有。那么 WebServiceHost 有什么不同呢?通过基地址将“RestService”添加回路径的推荐方法是什么?
  • 默认情况下WebServiceHost 将在基地址上添加端点。所以如果你想让你的操作响应http://localhost:8000/RestService/Test,那么你可以在基地址中添加RestService
猜你喜欢
  • 1970-01-01
  • 2012-01-14
  • 2011-09-29
  • 2020-04-20
  • 2013-09-02
  • 2014-01-12
  • 1970-01-01
  • 2014-09-11
  • 2013-06-22
相关资源
最近更新 更多