我正在尝试通过在 this blog post 上重新创建项目来学习如何使用 WCF 构建 RESTful 服务。安东尼·斯蒂尔着。他在配置中使用以下 XML 来设置服务的端点。
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/greeter"/>
</baseAddresses>
</host>
但是,当我尝试在 ASP.NET 3.5 网站的 web.config 中执行相同的操作时,我无法导航到我的服务。这是我正在使用的 XML:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="GreeterBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="GreeterBehavior" name="Greeter">
<host>
<baseAddresses>
<add baseAddress="http://localhost:49268/TestREST/webapi/services/greeter"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="IGreeter">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
我想我的配置将允许我导航到 http://localhost:49268/TestREST/webapi/services/greeter并查看我的服务。我收到的只是一条找不到资源的消息 - 我是否遗漏了什么?
编辑:我的部分问题是我的绑定(bind)是 wsHttpBinding。使用 webHttpBinding 让我能够正确使用该服务 - 除了 baseAddress 配置部分仍然没有效果。
最佳答案
我的预感是服务端点没有成功创建。
在服务“名称”属性中,您不包含服务类型的 FQN(完全限定名称)。其次,在端点“契约(Contract)”属性中,您也不会将 FQN 包含到契约(Contract)类型中。
另一方面,这可能是端口问题。为了确定这一点,请尝试运行 Visual Studio 2008 发行版中包含的 WcfTestClient.exe。如果可以连接到http://localhost:49268/TestREST/webapi/services/greeter/mex ,那么你就知道这不是端口问题。
假设您可以通过 MEX 连接,然后尝试执行一些方法,这些方法可能会映射到 http://localhost:49268/TestREST/webapi/services/greeter .
如果您在服务器上操作,请在此处查看有关 HttpCfg.exe 的一些有值(value)的详细信息: WCF ServiceHost basicHttpBinding 503 error
如果您需要有关 WcfTestClient 的更多详细信息,请在此处查找: Is it possible to make the WcfTestClient work for custom transport channels?
以防万一:逐字复制示例并验证它是否按定义工作,包括配置文件,然后再做出最轻微的偏差。
关于wcf - 为什么我的 WCF 服务不响应 web.config 中的 baseAddress 设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/541762/