简体   繁体   中英

WCF Service Returns 400 error when using WebGet

I don't know much about WCF. But I have a very basic service that I'm trying to execute. My service code looks like this:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(IncludeExceptionDetailInFaults = false)]
[ServiceContract]
public class MyService
{
    [OperationContract]
    [WebGet(UriTemplate = "/IsValidRequest")]
    public bool IsValidRequest()
    {
        return true;
    }
}

Like I said, a very basic service. When I enter "http://localhost:[port]/MyService.svc" into my browser, I see the service description page. However, "IsValidRequest" is not listed like I thought it would be (maybe this only happens with .asmx's). Either way, when I enter "http://localhost:[port]/MyService.svc/IsValidRequest" into my browser, nothing is returned. In Fiddler, I see that I get an HTTP 400 error. However, there is nothing that gives me any inkling as to what the problem could be.

Can someone help me out and point me in the right direction? Thanks!

Use the following config (change the namespaces to match your code)

<services>
  <service name="WcfService1.MyService" behaviorConfiguration="GetBehavior">
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="WebBehavior"  contract="WcfService1.MyService">

    </endpoint>

  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="GetBehavior">

      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebBehavior">
      <webHttp />          
    </behavior>
  </endpointBehaviors>

</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" aspNetCompatibilityEnabled="true"/>

Your .svc file should look like:

<%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.MyService" CodeBehind="MyService.svc.cs" %>

在此处输入图片说明

Looking at the WSDL for the message is irrelevant because as you have said in a response to a comment you want this to be a REST based service and WSDL is a SOAP construct. On that basis you should remove a <serviceMetadata> behavior if you have one as this is about SOAP metadata.

To diagnose problems like this you should turn on tracing in WCF (I have a short screencast here that shows you how to do it). This should highlight what the problem is in processing the message

To wire up the REST plumbing without adding a section in the config for your service add the following to your config file under the system.serviceModel section

<protocolMapping>
  <add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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