【问题标题】:WCF Method giving 400 Bad Request in GET/POST methodWCF 方法在 GET/POST 方法中给出 400 错误请求
【发布时间】:2012-03-15 10:27:58
【问题描述】:

我新创建了一个 WCF,当我尝试从浏览器点击 url 时,我面临 400 bad req 错误。

我的服务合同看起来像

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetUsers")]
string GetUsers();

我已经在webconfig中输入了

<serviceMetadata httpGetEnabled="true"/>

我在浏览器中点击的网址是

http://localhost:51561/AceWebService.svc/GetUsers

这是 webconfig 的一部分:

<system.serviceModel>
    <services>
        <service name="AceWebService.AceWebService" behaviorConfiguration="AceWebService.AceWebServiceBehavior">
            <!-- Service Endpoints -->
            <endpoint address="" binding="wsHttpBinding" contract="AceWebService.IAceWebService">
                <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="AceWebService.AceWebServiceBehavior">
                <!-- 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="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

参考了 stackoverflow 中所有可用的问题。没有得到任何帮助。请提出更改建议。

thnx

【问题讨论】:

    标签: wcf http-status-code-400


    【解决方案1】:

    您使用了在端点上定义的 wsHttpBinding。只需将其更改为 webHttpBinding 即可。

    【讨论】:

      【解决方案2】:

      首先,将 WebGet 用于 GET 请求。其次,让服务在没有服务契约接口的情况下工作,然后在它工作时,将契约移动到接口中。

      这对我有用:

      [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
      [ServiceContract]
      public class HelloService 
      {
      
          [WebGet(UriTemplate = "helloworld")]
          [OperationContract]
          public string HelloWorld()
          {
               return "Hello World!";
          }
      
      }
      

      【讨论】:

      • 我想知道我上面的代码有什么问题.. dnt想要一个替代品
      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多