WCF Rest Service不在浏览器中返回数据

时间:2013-04-09 18:52:00

标签: .net wcf web-services

我有一个返回简单字符串的REST服务。当我使用WCF测试客户端时,服务返回预期值,但是当我使用浏览器访问服务时,页面显示为空。有任何想法吗?所有代码都在下面。

CUCC.svc.cs

 public class CUCC : ICUCC
{
    public string AllAtms(string serviceKey)
    {
        //return new XElement("AllAtms") ;
        return "test";
    }

    //public XElement AllOrganizationAtms(string serviceKey, int organizationId)
    //{
    //    return new XElement("AllOrganizationAtms");
    //}
}

ICUCC.cs

[ServiceContract]
public interface ICUCC
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "AllAtms/{serviceKey}")]
    string AllAtms(string serviceKey);

    //[OperationContract]
    //[WebInvoke(Method = "GET",
    //    ResponseFormat = WebMessageFormat.Xml,
    //    BodyStyle = WebMessageBodyStyle.Wrapped,
    //    UriTemplate = "AllAtms/{serviceKey}/{organizationId}")]
    //XElement AllOrganizationAtms(string serviceKey, int organizationId);
}

的web.config

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings />
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <httpRuntime />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="CUCC.AllAtms" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="CUCC.AllAtms" behaviorConfiguration="web"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetBinding="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

更新

抱歉,忘记包含我正在使用的网址。

http://localhost:51870/CUCC.svc/AllAtms/testing

1 个答案:

答案 0 :(得分:3)

问题在于:

  <service name="CUCC.AllAtms" behaviorConfiguration="ServiceBehaviour">
    <endpoint address="" binding="webHttpBinding" contract="CUCC.AllAtms" behaviorConfiguration="web"></endpoint>
  </service>

此服务定义声称在CUCC名称空间中有一个名为AllAtms的服务;但是没有这样的服务。当您使用svc文件的URI调用服务时,IIS会尝试查找与您正在使用的服务的实际名称相匹配的服务定义。 IIS从svc文件中获取服务的正确名称,但它找不到与正确服务名称匹配的服务配置,因此它返回HTTP 400(错误请求)。您可以使用Fiddler轻松查看此结果。

服务名称应该是实现类的名称空间限定名称,合同应该是接口的名称空间限定名称。例如:

  <service name="StackOverflow15910199.CUCC">
    <endpoint address="" binding="webHttpBinding" contract="StackOverflow15910199.ICUCC" behaviorConfiguration="web" />
  </service>

必须是与此服务关联的WCF库项目在其App.config文件中具有正确定义的服务和端点,这就是WCF测试客户端正在工作的原因。测试客户端不应该使用您发布的服务配置。