WCF REST自托管返回400错误请求

时间:2013-07-29 07:03:03

标签: json wcf rest

我正在尝试构建一个自托管的WCF RESTful(Json)服务器。 按照一些分步教程,它会在fiddler中的请求http://192.168.1.250:18688/MyService/GetJSON之后返回错误400。 有人说在这种情况下,需要SVC文件的修改,但实际上在自托管应用程序中没有这样的文件。

如何解决?谢谢!

接口

namespace Contracts
{
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "Ping")]
        bool Ping();
        [OperationContract]
        Dude GetDude();
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetJSON")]//,BodyStyle=WebMessageBodyStyle.Bare)]
        string GetDudeJSON();
    }
}

代码

namespace Contracts
{
    [ServiceBehavior]   
    public class MyService:IMyService
    {
        public Dude Dummy { get; set; }
        public MyService()
        {
            Dummy = new Dude("Dude", 28);
        }


        public bool Ping()
        {
            return true;
        }

        public Dude GetDude()
        {
            return Dummy;
        }

       string IMyService.GetDudeJSON()
        {
            MemoryStream stream = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Dude));
            ser.WriteObject(stream,Dummy);
            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
          //  Console.WriteLine("Read:"+reader.ReadToEnd());
            return reader.ReadToEnd();
        }
    }
}

的app.config

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Contracts.MyService" behaviorConfiguration="MEXBehavior">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpBinding1" contract="Contracts.IMyService"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>        
            <add baseAddress="http://192.168.1.250:18688/MyService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <!-- A behavior definition for MEX -->
    <behaviors>
      <serviceBehaviors>

        <behavior name="MEXBehavior">
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceMetadata/>
        </behavior>        
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="httpBinding1"></binding>
      </basicHttpBinding>      
    </bindings>
  </system.serviceModel>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

CLI主机

static void Main(string[] args)
        {
            ServiceHost _host = new ServiceHost(typeof(Contracts.MyService));
            _host.Open();         
            ChannelFactory<Contracts.IMyService> channel = new ChannelFactory<Contracts.IMyService>(
                                                                            new BasicHttpBinding(),
                                                                            new EndpointAddress("http://192.168.1.250:18688/MyService"));
            Contracts.IMyService client = channel.CreateChannel();   
            while (true)
            {
                ;
            }
        }

1 个答案:

答案 0 :(得分:0)

basicHttpBinding用于基于SOAP的通信。对于REST,您必须使用webHttpBinding

将您的服务端点更改为:

<endpoint address="" binding="webHttpBinding" contract="Contracts.IMyService" behaviorConfiguration="MyRestBehavior" />

您必须注意到,我已经添加了端点行为配置参考。在<behaviors>部分中定义它:

<endpointBehaviors>
    <behavior name="MyRestBehavior">
        <webHttp />
    </behavior>
</endpointBehaviors>

执行此操作后,无需手动使用DataContractJsonSerializer。您可以将界面更改为:

Dude GetDudeJSON();

并实施到:

Dude IMyService.GetDudeJSON()
{
    return new Dude("Dude", 28);
}

您将自动收到JSON格式的对象。

另一方面 - 你的while (true) { ; }是非常糟糕的阻止方式 - 它会占用你的CPU周期。而只是使用一些阻止调用,如Console.ReadLine()