WCF Rest服务不在测试客户端上显示方法

时间:2013-09-09 16:48:02

标签: c# wcf rest

我正在编写WCF Rest服务以返回JSON消息。我一直在尝试使用我在互联网上找到的一个例子作为指导。每次启动测试客户端时,都不会显示任何方法。在服务运行时导航到Uri会产生“页面无法显示”页面。不太确定从哪里开始。任何帮助将不胜感激。

Web配置:

<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingWithJasonP"
             crossDomainScriptAccessEnabled="true" />
  </webHttpBinding>
</bindings>
<services>
  <service name="WcfRestLicense.LicenseService"
           behaviorConfiguration="WebServiceBehavior">
    <endpoint behaviorConfiguration="jsonBehavior"
              binding="webHttpBinding"
              bindingConfiguration="webHttpBindingWithJasonP"
              contract="WcfRestLicense.ILicenseService" />
    <endpoint contract="IMetadataExchange"
              binding="mexHttpBinding"
              address="mex" />

  </service>
</services>
<!--<client />-->

<behaviors>
  <endpointBehaviors>
    <behavior name="jsonBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="WebServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<protocolMapping>
  <add
    scheme="http"
    binding="webHttpBinding"
    bindingConfiguration="webHttpBindingWithJasonP" />
</protocolMapping>
<serviceHostingEnvironment
  aspNetCompatibilityEnabled="false"
  multipleSiteBindingsEnabled="true" />

服务方式:

public IQueryable<customer> GetCustomerById(string customerId)
    {
        int custId = Convert.ToInt32(customerId);
        return _context.customers.Where(c => c.cust_id == custId);
    }

接口:

[ServiceContract]
public interface ILicenseService
{
    [OperationContract]
    [WebGet(UriTemplate = "customer/{customerId}/",
        RequestFormat= WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare)]
    IQueryable<customer> GetCustomerById(string customerId);
}

1 个答案:

答案 0 :(得分:0)

如果测试客户端您正在谈论WcfTestClient,那么它将不适用于RESTful服务;它旨在使用基于SOAP的Web服务。您可以通过传入适当的URI来在浏览器中测试RESTful服务,如下所示:

http://<location of your service>/service/1

数字1是客户ID。这是一个粗略的例子,因为a)我没有用RESTful服务做很多事情而且b)我不确定你的实际地址是什么。

当你去Uri时获得404,听起来你正在寻找帮助页面。您可以在配置文件中启用它:

<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingWithJasonP"
             crossDomainScriptAccessEnabled="true" 
             enableHelp="true" />
  </webHttpBinding>
</bindings>