简体   繁体   English

使用WebGet时WCF服务返回400错误

[英]WCF Service Returns 400 error when using WebGet

I don't know much about WCF. 我对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. 当我在浏览器中输入“ http:// localhost:[port] /MyService.svc”时,我看到了服务描述页面。 However, "IsValidRequest" is not listed like I thought it would be (maybe this only happens with .asmx's). 但是,“ IsValidRequest”没有像我想的那样列出(也许仅在.asmx上发生)。 Either way, when I enter "http://localhost:[port]/MyService.svc/IsValidRequest" into my browser, nothing is returned. 无论哪种方式,当我在浏览器中输入“ http:// localhost:[port] /MyService.svc/IsValidRequest”时,都不会返回任何内容。 In Fiddler, I see that I get an HTTP 400 error. 在Fiddler中,我看到出现HTTP 400错误。 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: 您的.svc文件应如下所示:

<%@ 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. 查看消息的WSDL是无关紧要的,因为正如您在对评论的回复中所说,您希望它是基于REST的服务,而WSDL是SOAP构造。 On that basis you should remove a <serviceMetadata> behavior if you have one as this is about SOAP metadata. 在此基础上,如果您有<serviceMetadata>行为,则应该删除它,因为这与SOAP元数据有关。

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). 为了诊断这样的问题,您应该打开WCF中的跟踪功能( 这里有一个简短的截屏视频,向您展示了如何做到)。 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 要连接REST管道而不在服务的配置中添加部分,请在system.serviceModel部分下的配置文件中添加以下内容

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

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

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