代码之家  ›  专栏  ›  技术社区  ›  kgriffs

RESTful WCF的最低裸配置

  •  6
  • kgriffs  · 技术社区  · 18 年前

    要使WCF与REST一起工作,我需要在web.config中输入的最小值是多少?我已经用[WebGet]注释了我的方法,但它们没有得到消息。

    3 回复  |  直到 12 年前
        1
  •  6
  •   kgriffs    18 年前

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

    Factory="System.ServiceModel.Activation.WebServiceHostFactory"
    

    请注意,名称空间与web上其他地方提到的名称空间略有不同(例如 this MSDN article

    完成此操作后,我可以从web.config中删除整个部分,并且所有内容仍然有效!

        2
  •  2
  •   willem    18 年前

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

        <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
          contract="WcfCore.ICustomer">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
    
        3
  •  1
  •   Ta01    18 年前

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

    <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模板调用您的操作。

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

    推荐文章