简体   繁体   English

WCF JSON HTTP 400错误请求

[英]WCF JSON HTTP 400 bad request

I'm trying a WCF Web service with JSON post method: 我正在尝试使用JSON post方法进行WCF Web服务:

Service1.svc 服务1.svc

  [AspNetCompatibilityRequirements(
       RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {


        public string GetData(string id)
        {
           // return string.Format("You entered: {0}", value);
            return "you entered " + id;
        }

IService1.cs IService1.cs

[ServiceContract]
public interface IService1
{
     [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "data/{id}")]
    [OperationContract]
    string GetData(string id);

web.config web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <!--<compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>-->
    <compilation/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

Then I hosted .svc on IIS and tested JSON with below on IE http://localhost/WCFService/Service1.svc/data/10 然后我将.svc托管在IIS上,并在IE http://localhost/WCFService/Service1.svc/data/10上使用以下方法测试了JSON

But getting 但是得到

http 400 bad request HTTP 400错误请求

Here the WCF Test Client, calling service method: 这里是WCF测试客户端,调用服务方法: 在此处输入图片说明 I found a solution on Stackoverlow. 我在Stackoverlow上找到了解决方案。 C# 4.0 WCF REST JSON - HTTP GET CODE 400 Bad Request C#4.0 WCF REST JSON-HTTP GET CODE 400错误请求

tried with below web.config file: but still its not working (http 400 bad request) 尝试使用以下web.config文件:但仍然无法正常工作(HTTP 400错误请求)

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding"/>
    </protocolMapping>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>

        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>  
 </configuration>

After lot of searching I found a solution at configuring WCF with <services> tag 经过大量搜索,我发现了使用<services>标签配置WCF的解决方案

Here is the web.config file, that is working: 这是正在运行的web.config文件:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <!--<compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>-->
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfServiceApp.Service1">
        <endpoint address="../Service1.svc" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/>
      </customHeaders>
    </httpProtocol>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

I believe below part did the trick: 我相信下面的部分可以解决问题:

WcfServiveApp is my namespace and Service1 is class and IService1 is interface. WcfServiveApp是我的命名空间,而Service1是类,而IService1是接口。

<services>
  <service name="WcfServiceApp.Service1">
    <endpoint address="../Service1.svc" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/>
  </service>
</services>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM