4

我新创建了一个 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中所有可用的问题..没有得到任何帮助..请提出更改建议。

谢谢

4

2 回答 2

3

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

于 2012-03-15T13:40:44.073 回答
0

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

这对我有用:

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

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

}
于 2012-03-15T10:34:06.343 回答