WCF Rest和Soap服务-(415)无法处理消息,因为内容类型';应用程序/json;字符集=utf-8';

WCF Rest和Soap服务-(415)无法处理消息,因为内容类型';应用程序/json;字符集=utf-8';,rest,wcf,Rest,Wcf,无法处理消息,因为内容类型为“application/json;charset=utf-8“不是预期的类型”text/xml;字符集=utf-8' 我有一个支持Soap的WCF服务,该服务按预期工作,但当我尝试将其作为Restful服务使用时,我收到了上面的错误消息。我花了无数个小时研究错误,尝试了所有建议,但都没有成功 这是我的Web.config` <?xml version="1.0"?> <configuration> <configS

无法处理消息,因为内容类型为“application/json;charset=utf-8“不是预期的类型”text/xml;字符集=utf-8'

我有一个支持Soap的WCF服务,该服务按预期工作,但当我尝试将其作为Restful服务使用时,我收到了上面的错误消息。我花了无数个小时研究错误,尝试了所有建议,但都没有成功

这是我的Web.config`

<?xml version="1.0"?>
<configuration>
 <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="Test_Service.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>

 </configSections> 


<system.web>
    <compilation strict="false" explicit="true" targetFramework="4.6" debug="true"/>
    <customErrors mode="Off"/>
    <pages controlRenderingCompatibilityVersion="4.0"/>
  </system.web>

  <system.serviceModel>

<services>
  <service name="Test_Service.Test_Service">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="secureHttpBinding" contract="Test_Service.ITest_Service"/>
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
    <endpoint address="REST" binding ="webHttpBinding" contract ="Test_Service.ITest_Rest_Service" behaviorConfiguration ="RESTBehaviour" bindingConfiguration ="webBinding"/>
  </service>
</services>

<bindings>
  <basicHttpBinding>
    <binding name="secureHttpBinding">
      <security mode="Transport">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
  </basicHttpBinding>
  <webHttpBinding >
    <binding name ="webBinding">
      <security mode ="Transport">
        <transport clientCredentialType ="None"/>
      </security>
    </binding>
  </webHttpBinding>
</bindings>

<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled ="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name ="RESTBehaviour">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>   

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>


</system.serviceModel>

  <system.webServer>
    <directoryBrowse enabled="true"/>
    <validation validateIntegratedModeConfiguration="false"/>
  </system.webServer>



 <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>  
</configuration>`

有没有关于如何修复此错误的建议?

在我尝试之后,我认为问题可能在于服务接口的实现。您可以提供实现该服务的特定代码吗?公共函数GetRestTestData(sData)作为字符串实现ITest_Rest_服务。GetRestTestData Dim oOutput作为对象oOutput=New,带有{.Return=“Hello World”}返回新的JavaScriptSerializer().Serialize(oOutput)结束函数
<ServiceContract()>
Public Interface ITest_Service

<OperationContract()>
Function GetData(ByVal TestInfo As TestInputData) As TestOutputData

End Interface

    <ServiceContract()>
Public Interface ITest_Rest_Service
    <OperationContract()>
    <WebInvoke(Method:="Post", UriTemplate:="GetRestTestData", BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)>
    Function GetRestTestData(sData As String) As String
End Interface
Dim oData As Object = New With {
                        Key .Client = "123",
                        Key .DeviceType = "Computer"             
                      }

        sData = (New JavaScriptSerializer().Serialize(oData))
        Dim sURL As String = "https://Test.com/Test_Service.svc/GetRestTestData"
        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create(New Uri(sURL)), HttpWebRequest)
        postReq.Method = "Post"
        postReq.ContentType = "application/json; charset=utf-8"
        postReq.Accept = "application/json"            
        Dim byteData As Byte() = Encoding.UTF8.GetBytes(sData)
        postReq.ContentLength = byteData.Length

        Dim postreqstream As Stream = postReq.GetRequestStream()
        postreqstream.Write(byteData, 0, byteData.Length)
        postreqstream.Close()

        Dim postresponse As HttpWebResponse

        postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)