0

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

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

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

地址设置为“localhost”我得到... 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";
    }
}
4

1 回答 1

2

您还可以显示配置的服务元素吗?我认为您的配置未使用,或者您正在访问应用程序的其他实例(您是否将 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 指令中使用的服务类型名称(包括命名空间)完全相同。

于 2011-01-14T17:38:16.457 回答