【问题标题】:Simple WCF service - all requests returning empty?简单的 WCF 服务 - 所有请求都返回空?
【发布时间】:2011-12-12 12:20:59
【问题描述】:

根据http://msdn.microsoft.com/en-us/library/bb412178.aspx 上的教程,我目前无法测试应用了WebGet 属性的简单WCF 服务

我创建了一个空白的 ASP.NET Web 应用程序项目,然后添加了一个名为“test”的 WCF 服务,它创建了 test.svcItest.svc

然后我添加了方法“你好”:

public class test : Itest
{
    public string hello(string s){return "hello " + s;}
}

实现接口:

[ServiceContract]
public interface Itest
{
    [OperationContract]
    [WebGet]
    string hello(string s);
}

但是,对http://localhost/test.svc/hello?s=hi 的任何请求(HTTP GET)都会返回一个空页面。 事实上,该 url 下的任何请求都会返回一个空白页面(即http://localhost/test.svc/abcdefg

我错过了一些基本的东西吗?

编辑:web.config 完全标准,除了 Visual Studio 生成的:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_Itest" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/test.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_Itest" contract="ServiceReference1.Itest"
        name="BasicHttpBinding_Itest" />
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

【问题讨论】:

  • 使用 [WebGet] 属性,我不想将它与服务引用一起使用。话虽如此,它在使用 wcftestclient.exe 进行测试时确实有效
  • 我测试了其他浏览器并以编程方式发出了网络请求......仍然得到一个空响应。
  • 尝试将WebGet 更新为类似[WebGet(UriTemplate = "foo", BodyStyle = WebMessageBodyStyle.Bare)]
  • 将 UriTemplate 更改为上述,什么也没做,也没有更改 web.config 的 binding="webHttpBinding"

标签: c# wcf


【解决方案1】:

你应该使用 webHttpBinding 而不是 basicHttpBinding 以下是这里的部​​分答案:testing wcf service in browser

这取决于您拥有的 WCF 服务类型:

如果您使用的是 WCF REST 服务 (webHttpBinding),那么您应该能够导航到服务地址,例如http://yourserver/somedir/service.svc

如果您使用其他任何东西,那么您有一个 SOAP 服务,并且您无法在 Web 浏览器中测试 SOAP 服务 - 浏览器只是不理解和说 SOAP。但是,您的 C:\ 驱动器中有一个 WCF 测试客户端应用程序可以用于此目的。

我自己可以补充说 WCFTestCLient 在这里:“C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\WcfTestClient.exe”

【讨论】:

    【解决方案2】:

    尝试将 web.config 中的上述部分替换为以下部分:

        <system.serviceModel>    
        <services>
          <service name="ServiceReference1.test">
          <endpoint binding="webHttpBinding" contract="ServiceReference1.Itest" behaviorConfiguration="web"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="web">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    您的服务器需要在您的 web.config 中包含 services 元素而不是 client 元素,如上所示。要使用 webGet,您还需要使用 webHttpBinding

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      相关资源
      最近更新 更多