如何测试我的服务?

How to test my service?

那是我的 link:http://localhost:54483/BusinessService.svc/GetCustomers?numberOf=12&valid=true

在客户端执行时总是出现 "Bad Request" 错误。 在浏览器中输入URL时停留在起始页(您已创建服务...)。

按照方法:

[ActionName("GetCustomers")]
[HttpGet]
public System.Collections.Generic.List<BusinessObjects.Customer> GetCustomers(byte numberOf, bool valid) {
    return BS.GetCustomers(byte numberOf, bool valid); //BS = BusinessService Instance
}

编辑

那是我的 BusinessService 接口:

[System.ServiceModel.OperationContract]
System.Collections.Generic.List<BusinessObjects.Customer>   
GetCustomers(byte numberOf, bool valid);

执行该方法后:

public virtual System.Collections.Generic.List<BusinessObjects.Customer> GetCustomers(byte numberOf, bool valid) {
    return this.BL.GetCustomers(byte numberOf, bool valid); //BL = BusinessLogic Instance
}

在我的 API:

[ActionName("GetCustomers")]
[HttpGet]
public System.Collections.Generic.List<BusinessObjects.Customer> GetCustomers(byte numberOf, bool valid) {
    return BS.GetCustomers(byte numberOf, bool valid); //BS = BusinessService Instance
}

Web 配置文件:

<?xml version="1.0"?>
<configuration>

<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>

<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1"/>
</system.web>


<system.serviceModel>
<services>
  <service name="WcfService2.Service1">
    <endpoint address=""
              behaviorConfiguration=""
              binding="webHttpBinding"
              contract="WcfService2.TestService" />
  </service>
</services>

<behaviors>
  <endpointBehaviors>
    <behavior name="restBehavior">
      <webHttp helpEnabled="true"/>
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>
    <behavior>

      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>

      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
multipleSiteBindingsEnabled="true" />
</system.serviceModel>


<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>

</configuration>

我想对这个问题发表评论,但我必须有 50 声望才能发表评论。

默认情况下,WCF 网络服务只有 SOAP 协议。如果你想从浏览器调用它,你必须启用 RESTful。

您可以关注 here and here 以启用 RESTful。

覆盖我最初的所有评论并将它们转换成一个(希望如此)有用的答案:我认为您在构建服务时考虑到了 REST,但默认情况下 WCF 是 SOAP,这会让您的客户感到困惑。

网络上有很多关于如何在 WCF 中启用 REST 的文章,快速 google 搜索应该可以立即找到您想要的地方。我已经在评论中分享了这个 link,但如果将来有人需要它可能会有用:http://www.codeproject.com/Articles/803409/REST-enabled-WCF-service#10.

长话短说 - REST 不是您可以直接使用 WCF 开箱即用的东西。