【问题标题】:explanation of system.servicemodel endpoint.address element for rest serviceREST 服务的 system.servicemodel endpoint.address 元素的解释
【发布时间】:2011-06-09 07:03:02
【问题描述】:

我有一个 WCF 休息网络服务。它工作正常。我想了解端点元素中可用的不同配置值。

特别是,我试图了解地址元素的用途。更改值似乎并没有改变我处理服务的方式。为此,我从 Visual Studio 2010 和 cassini 运行该服务。端口号设置为888。

地址设置为空字符串,我得到... http://localhost:888/restDataService.svc/hello 将返回“hello world”。

地址设置为“本地主机”我得到... http://localhost:888/restDataService.svc/hello 将返回“hello world”。

地址设置为“pox”我得到... http://localhost:888/restDataService.svc/hello 将返回“hello world”。

我在地址字段中设置什么值并不重要。它不影响网址。我唯一的解释是非 REST 服务的价值更高。

<system.serviceModel>
  <services>
    <service behaviorConfiguration="MobileService2.DataServiceBehaviour" name="MobileService2.DataService">

      <endpoint address="pox" binding="webHttpBinding" contract="MobileService2.IRestDataService" behaviorConfiguration="webHttp">
      </endpoint>

      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"  />
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webHttp">
        <webHttp />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="MobileService2.DataServiceBehaviour" >
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

我还有以下服务合同

    [ServiceContract]
    public interface IRestDataService
    {
        [OperationContract]
        [WebGet(UriTemplate = "hello")]
        string Hello();
    }

在 .svc 中

<%@ ServiceHost Language="C#" Debug="true" 
            Service="MobileService2.RestDataService" 
            Factory="System.ServiceModel.Activation.WebServiceHostFactory"
            CodeBehind="RestDataService.svc.cs" %>

还有“代码隐藏”

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestDataService : IRestDataService
{
    public string Hello()
    {
        return "hello";
    }
}

【问题讨论】:

    标签: wcf web-services rest configuration endpoint


    【解决方案1】:

    您还可以显示配置的服务元素吗?我认为您的配置未使用,或者您正在访问应用程序的其他实例(您是否将 Cassini 配置为使用端口 80?),因为您的第二个和第三个测试应该返回 HTTP 404 Resource not found。

    您的测试的正确地址是:

    1. http://localhost/restDataService.svc/hello
    2. http://localhost/restDataService.svc/localhost/hello
    3. http://localhost/restDataService.svc/pox/hello

    检查您在服务元素中的名称是否与 .svc 标记中的 ServiceHost 指令中使用的服务类型名称(包括命名空间)完全相同。

    【讨论】:

    • +1 绝对正确!刚刚自己测试过 - 当配置正确时,这就是发生的行为。
    猜你喜欢
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 2016-05-24
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    相关资源
    最近更新 更多