简体   繁体   中英

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

I'm trying to build my first WCF service. 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.
  2. When I type http://localhost:12345/Service1.svc into Chrome, I get a page.
  3. Clicking on svcutil.exe http://localhost:12345/Service1.svc?wsdl gives me an XML.

However, when I type http://localhost:12345/Service1.svc/test/13 , I only get an empty response. There's nothing in there but <body> with a <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).

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 . 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. 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.

Did you try to check for the status code? Is it 200 OK or something (much) larger?

I believe that you've asked a similar question on Social MSDN and that There was some confusion as how to form the code. Let me recap the highlights below.

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

<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.

localhost/Service1.svc/inputData

You can try following:

  1. Mark service implementation with ServiceBehaviour attribute

     [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class Service1 : IService1 
  2. Inside the web.config add/modify following preserving existing data:

     <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:

<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:

<!-- 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:

 <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. You can find it on the tools menu of Visual Studio. Once open, open the web.config for your project with it and start editing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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