WCF方法在GET / POST方法中给出400错误请求

时间:2012-03-15 10:27:59

标签: wcf http-status-code-400

当我尝试从浏览器中点击网址时,我新创建了一个WCF并且面临400个糟糕的请求错误。

我的服务合同似乎是

[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

2 个答案:

答案 0 :(得分:3)

您已使用在端点上定义的wsHttpBinding。只需将其更改为webHttpBinding即可使其正常工作。

答案 1 :(得分:0)

首先,使用WebGet进行GET请求。其次,在没有服务合同接口的情况下使服务工作,然后在其工作时,将合同移动到接口中。

这对我有用:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[ServiceContract]
public class HelloService 
{

    [WebGet(UriTemplate = "helloworld")]
    [OperationContract]
    public string HelloWorld()
    {
         return "Hello World!";
    }

}