我需要在web.config中放置什么才能让WCF使用REST?我已经用[WebGet]注释了我的方法,但他们没有收到消息。

有帮助吗?

解决方案

我发现您可以将以下内容添加到* .svc文件中的ServiceHost指令中,它会自动为您设置WebHttpBinding和WebHttpBehavior:

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

请注意,命名空间与网络上其他地方提到的名称略有不同(例如这篇MSDN文章)。

执行此操作后,我能够从web.config中删除整个部分,一切仍然有效!

其他提示

确保在端点上使用webHttpBinding(而不是httpBinding或wsHttpBinding)。这是我的端点配置...

    <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
      contract="WcfCore.ICustomer">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>

您需要确保拥有服务主机的地址,例如

<services>
      <service name="SomeLib.SomeService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/somebase"/>
          </baseAddresses>
        </host>
<!-- And one EndPoint **basicHttpBinding** WILL WORK !!! -->

        <endpoint 
                   address="basic"
                   binding="basicHttpBinding"
                   contract="SomeLib.SomeContract"/>
</service>
</services>

现在,如果您通过控制台应用程序进行自我托管,例如...您可以通过以下方式调用您的主机:

WebChannelFactory<IServiceContract> factory =
        new WebChannelFactory<IServiceContract>(
            new Uri("http://localhost:8080/somebase"));

当控制台应用启动时,即使其自托管,该地址也可以浏览,您应该可以根据您的webget uri模板调用您的操作。

此最小配置允许您通过自托管来调用WCF RestFULLY。如果您在IIS中托管它将基本上以相同的方式工作,除了svc文件替换我们的自定义主机。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top