WCF REST服务不返回的浏览器数据 [英] WCF Rest Service Not returning data in browser

查看:134
本文介绍了WCF REST服务不返回的浏览器数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

CUCC.svc.cs

 公共类CUCC:ICUCC
{
    公共字符串AllAtms(字符串的serviceKey)
    {
        //返回新的XElement(AllAtms);
        返回测试;
    }

    //公众的XElement AllOrganizationAtms(字符串的serviceKey,INT organizationId)
    // {
    //返回新的XElement(AllOrganizationAtms);
    //}
}
 

ICUCC.cs

  [的ServiceContract]
公共接口ICUCC
{
    [OperationContract的]
    [WebInvoke(方法=GET,
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate =AllAtms / {的serviceKey})]
    字符串AllAtms(字符串的serviceKey);

    // [OperationContract的]
    // [WebInvoke(方法=GET,
    // ResponseFormat = WebMessageFormat.Xml,
    // BodyStyle = WebMessageBodyStyle.Wrapped,
    // UriTemplate =AllAtms / {的serviceKey} / {organizationId})]
    //的XElement AllOrganizationAtms(字符串的serviceKey,INT organizationId);
}
 

web.config中

 <结构>
  < configSections>
    <! - 有关实​​体框架配置的详细信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468  - >
    <节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本= 4.4.0.0,文化=中性公钥= b77a5c561934e089requirePermission =FALSE/>
  < / configSections>
  <的appSettings />
  <的System.Web>
    <编译调试=真正的targetFramework =4.0>
      <组件>
        <添加组件=System.Data.Entity的,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089/>
      < /组件>
    < /编译>
    <的httpRuntime />
  < /system.web>
  < system.serviceModel>
    <服务>
      <服务名称=CUCC.AllAtmsbehaviorConfiguration =ServiceBehaviour>
        <端点地址=绑定=的WebHttpBinding合同=CUCC.AllAtmsbehaviorConfiguration =网络>< /端点>
      < /服务>
    < /服务>
    <行为>
      < serviceBehaviors>
        <行为NAME =ServiceBehaviour>
          < serviceMetadata httpGetEnabled =真httpsGetBinding =真/>
          < serviceDebug includeExceptionDetailInFaults =FALSE/>
        < /行为>
        <行为>
          <! - 为了避免泄露的元数据信息,部署之前的设置值低于假 - >
          < serviceMetadata httpGetEnabled =真httpsGetEnabled =真/>
          <! - 要接收故障异​​常详细信息以进行调试,设置以下为true值。设置为false部署之前,以避免泄露异常信息 - >
          < serviceDebug includeExceptionDetailInFaults =FALSE/>
        < /行为>
      < / serviceBehaviors>
      < endpointBehaviors>
        <行为NAME =网络>
          < webHttp />
        < /行为>
      < / endpointBehaviors>
    < /行为>
    < protocolMapping>
      <添加绑定=basicHttpsBinding计划=HTTPS/>
    < / protocolMapping>
    < serviceHostingEnvironment aspNetCompatibilityEnabled =假multipleSiteBindingsEnabled =真/>
  < /system.serviceModel>
  < system.webServer>
    <模块runAllManagedModulesForAllRequests =真/>
    <! - 
        为了调试过程中浏览Web应用程序根目录,设置以下为true值。
        设置为false部署之前,以避免泄露Web应用程序文件夹的信息。
       - >
    < directoryBrowse启用=真/>
  < /system.webServer>
< /结构>
 

更新

对不起,忘了,包括我使用的URL。

 的http://本地主机:51870 / CUCC.svc / AllAtms /测试
 

解决方案

该问题是在这里:

 <服务名称=CUCC.AllAtmsbehaviorConfiguration =ServiceBehaviour>
    <端点地址=绑定=的WebHttpBinding合同=CUCC.AllAtmsbehaviorConfiguration =网络>< /端点>
  < /服务>
 

此服务定义声称,有一个名为AllAtms在CUCC命名空间服务;但不存在这样的服务。当你调用使用SVC文件的URI的服务,IIS试图找到匹配您正在使用的服务的实际名称的服务定义。 IIS获得正确的名称从SVC文件的服务,但它没有找到一个服务配置匹配正确的服务名称,所以它返回的HTTP 400(错误请求)。你可以看到轻松地使用招这样的结果。

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

 <服务名称=StackOverflow15910199.CUCC>
    <端点地址=绑定=的WebHttpBinding合同=StackOverflow15910199.ICUCCbehaviorConfiguration =网/>
  < /服务>
 

这必须是与该服务相关的WCF库项目有一个正确定义的服务和端点在App.config文件的情况下,这就是为什么WCF测试客户端的工作。测试客户端不应该和你一起工作已经张贴服务配置。

I have a REST service that returns a simple string. When I use WCF Test Client the service returns the expected value, but when I use a browser to access the service, the page comes up empty. Any ideas? All code is below.

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>

UPDATE

Sorry, forgot to include the url that I am using.

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

解决方案

The problem is here:

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

This service definition claims that there is a service named AllAtms in the CUCC namespace; but there is no such service. When you invoke the service using the URI of your svc file, IIS attempts to find a service definition that matches the actual name of the service you are using. IIS gets the correct name for the service from the svc file but it doesn't find a service configuration matching the correct service name, so it returns HTTP 400 (Bad Request). You can see this result easily using Fiddler.

The service name should be the namespace qualified name of the implementation class and the contract should be the namespace qualified name of the interface. For example:

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

It must be the case that the WCF library project associated with this service has a properly defined service and endpoint in its App.config file, that's why the WCF test client is working. The test client should not have worked with service configuration you've posted.

这篇关于WCF REST服务不返回的浏览器数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆