简体   繁体   English

WCF测试客户端给出正确的结果,但在Chrome中执行时却不能

[英]WCF Testing Client giving right results but not when executed in Chrome

I'm trying to build my first WCF service. 我正在尝试构建我的第一个WCF服务。 I've got the following behavior now. 我现在有以下行为。

  1. When I run my WCF service, I can send in input and get the right results in Testing Client. 运行WCF服务时,我可以发送输入并在Testing Client中获得正确的结果。
  2. When I type http://localhost:12345/Service1.svc into Chrome, I get a page. 当我在Chrome中输入http://localhost:12345/Service1.svc ,我得到一个页面。
  3. Clicking on svcutil.exe http://localhost:12345/Service1.svc?wsdl gives me an XML. 单击svcutil.exe http://localhost:12345/Service1.svc?wsdl会给我一个XML。

However, when I type http://localhost:12345/Service1.svc/test/13 , I only get an empty response. 但是,当我输入http:// localhost:12345 / Service1.svc / test / 13时 ,我只会得到一个空响应。 There's nothing in there but <body> with a <pre> . 除了<body><pre>什么都没有。 What can I be doing wrong and how do i resolve it? 我该怎么办?我该如何解决? (Keep in mind that I'm a rookie at this.) Once I'll get the behavior working the way I want (so I can see the right result in the browser) I'll be producing either REST or JSON data in XML format (if that's of any importance). (请记住,我是新手。)一旦按照我想要的方式运行行为(以便在浏览器中看到正确的结果),我将以XML生成REST或JSON数据格式(如果有任何重要性)。

From this discussion I got this. 讨论中我得到了这一点。

namespace WcfService1
{
  [ServiceContract]
  public interface IService1
  {
    [OperationContract]
    [WebGet(UriTemplate = "/test/{indata}", 
      ResponseFormat = WebMessageFormat.Xml)]
    String Ping(String indata);
  }
}

As can be seen in this question my implementation is as follows. 这个问题可以看出,我的实现如下。

namespace WcfService1
{
  public class Service1 : IService1
  {
    public string Ping(String indata)
    {
      return "Pong " + indata;
    }
  }
}

The suggested web.config didn't work so I've tried to publish metadata (whatever that is) using the pointers in this article in combination with this discussion . 建议的web.config无法正常工作,因此我尝试结合本文的讨论使用本文中的指针发布元数据(无论是哪种形式)。 My configuration file look pretty much as the one in the latter link (except that I've removed the diagnostic part). 我的配置文件看起来与后一个链接中的配置文件差不多(除了我已删除诊断部分)。

I believe that the WCF testing client operates on SOAP. 我相信WCF测试客户端在SOAP上运行。 It tests more the fact that you're serving something, than that you're serving what you'd like to get. 它测试的是您正在提供某些东西的事实,而不是您正在提供想要得到的东西。

The empty body you're getting is, according to my experience, nothing but an error message. 根据我的经验,您得到的空的身体不过是一条错误消息。 However, under some circumstances, such as cross domain calls (not sure if it's the correct name nor if it's the full list of possible issues), when you work with eg XDomainRequest object in JavaScript (as opposed to the usual XmlHttpRequest ), the response being empty is a result of an error message. 但是,在某些情况下,例如跨域调用(不确定名称是否正确,也不是可能出现的问题的完整列表),当您使用JavaScript中的XDomainRequest对象(与通常的XmlHttpRequest相对)时,响应为空是错误消息的结果。

Did you try to check for the status code? 您是否尝试检查状态码? Is it 200 OK or something (much) larger? 是200 OK还是更大的东西?

I believe that you've asked a similar question on Social MSDN and that There was some confusion as how to form the code. 我相信您在Social MSDN上也提出了类似的问题,并且在如何编写代码方面有些困惑。 Let me recap the highlights below. 让我回顾一下下面的要点。

Web.config Web.config文件

<configuration>

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

  <system.serviceModel>
    <services>
      ...
    </services>
    <behaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

services - contents of the tag describing the service's nature 服务 -描述服务性质的标签内容

<service name="DemoRest.RestService" 
         behaviorConfiguration="ServiceBehavior">
  <endpoint address="" binding="webHttpBinding" 
            contract="DemoRest.IRestService" 
            behaviorConfiguration="web"></endpoint>
</service>

behaviors - contents of the tag describing the behavior of the service and the end-point behaviors-标记的内容,描述服务的行为和端点

<serviceBehaviors>
  <behavior name="ServiceBehavior">
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true"/>
  </behavior>
</serviceBehaviors>

<endpointBehaviors>
  <behavior name="web">
    <webHttp/>
  </behavior>
</endpointBehaviors>

Now the response can be retrieved using the following URL. 现在,可以使用以下URL检索响应。

localhost/Service1.svc/inputData

You can try following: 您可以尝试以下操作:

  1. Mark service implementation with ServiceBehaviour attribute 使用ServiceBehaviour属性标记服务实现

     [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class Service1 : IService1 
  2. Inside the web.config add/modify following preserving existing data: 在web.config内,添加/修改以下保留现有数据:

     <services> <service name="WcfService1.Service1"> <endpoint binding="webHttpBinding" contract="WcfService1.IService1" /> </service> </services> <behaviors> <endpointBehaviors> <behavior> <webHttp /> </behavior> </endpointBehaviors> ... </behaviors> 

These steps makes it working. 这些步骤使其正常工作。

To Get REST working using the dot net framework 4 simplified configuration , your web.config needs to contain the following: 为了使REST使用dot net framework 4简化的配置工作 ,您的web.config需要包含以下内容:

<system.serviceModel>
  <!-- 1) Specify webHttp as an endpoint behavior -->
  <behaviors>
    <endpointBehaviors>
      <behavior >
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>

  <!-- 2) Setup a protocol mapping to allow the service to be accessed via webHttp-->
  <protocolMapping>
    <add scheme="http" binding="webHttpBinding"/>
  </protocolMapping>
</system.serviceModel>

To get the output without all the xml in the browser, add this: 要获得浏览器中没有所有xml的输出,请添加以下内容:

<!-- Configure the webHttp standard endpoint -->
<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" />
  </webHttpEndpoint>
</standardEndpoints>

To allow access to the service metadata (needed to create proxies easily), add this to the behaviours element: 要允许访问服务元数据(需要轻松创建代理),请将其添加到behaviors元素:

 <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
 </serviceBehaviors>

Personally I prefer the old style configuration where you explicitly configure the endpoints, which would look like this: 我个人更喜欢旧式配置,在该配置中可以显式配置端点,如下所示:

 <system.serviceModel>
  <services>
    <service name="WcfService1.Service1">
      <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1"  behaviorConfiguration="webHttpBehavior"/>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webHttpBehavior">
        <webHttp automaticFormatSelectionEnabled="true"/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

Finally, you can use the WCF Service Configuration Editor to do the configuration using a gui. 最后,您可以使用WCF服务配置编辑器使用gui进行配置。 You can find it on the tools menu of Visual Studio. 您可以在Visual Studio的工具菜单上找到它。 Once open, open the web.config for your project with it and start editing. 打开后,用它打开项目的web.config并开始编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM