0

When I create a new WCF service, I can create a new method like this one:

 [OperationContract]
 [WebInvoke(Method = "GET", UriTemplate = "TryThis", ResponseFormat = WebMessageFormat.Json)]
 string TryThis();

A function that returns a simple string.

I can use it with the test client WCF and that works. But when I want access to my service with Chrome, postman, or my Android application, I can't.

I tried with theses urls :

 http://localhost:54792/Service1.svc/TryThis
 http://tempuri.org/IService1/TryThis

When I use 'http://localhost:54792/Service1.svc' I have the home page, so this is ok. But I don't have access at any method of my service.

The error is a 400 error. but I don't have any message who indicate where this error is. I don't know if this is the configuration of my IIS server, if this is my service. I'm totally blocked.

This is my web.config :

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
   </appSettings>
    <system.web>
     <compilation debug="true" targetFramework="4.7.1" />
     <httpRuntime targetFramework="4.7.1"/>
    </system.web>
    <system.serviceModel>
     <behaviors>
       <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>

PS: I already saw this post : "How Can I access WCF services using a Web Reference?" but that didn't help me.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
lebarillier
  • 5,294
  • 1
  • 9
  • 14
  • 1
    You're **not** showing us the most important parts of your `web.config` - where the service bindings etc. are configured. By default, most WCF bindings use **SOAP** to communicate between client and server - and typical web browsers don't have a SOAP client built-in - you need to use tools like SoapUI or similar to create and handle SOAP requests. The only binding you *could* test in a browser would be the `WebHttpBinding` that uses **REST** to communicate between client and server. – marc_s Nov 08 '18 at 04:58
  • This is all my `web.config`, I have a `web.debug.config` and a `web.release.config` but nothing more. This is the default application. I did not make any changes. And if possible I prefer use REST with `WebHttpBinding`. And Thank you for the Editing. My English is not very good. – lebarillier Nov 08 '18 at 05:11
  • 1
    See this other SO question (and its answers!) : https://stackoverflow.com/questions/17644392/configuring-wcf-rest-services-in-web-config – marc_s Nov 08 '18 at 06:11
  • That works thank you! so much!! – lebarillier Nov 08 '18 at 06:24

1 Answers1

2

It seems that you host the WCF service in IIS, and you want to publish a Restful-style service, you could refer to the following code.
Server:IService.cs

namespace WcfService5
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet]
        string GetData(int value);
    }
}

Server:Service.svc.cs

namespace WcfService5
{
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
}

Web.config.

<system.serviceModel>
    <services>
      <service name="WcfService5.Service1">
        <endpoint address="" binding="webHttpBinding" contract="WcfService5.IService1" behaviorConfiguration="MyRest"></endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="MyRest">
          <webHttp />
        </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>

Result. enter image description here

Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • Thank you but @marc_s already answer me :), I just added the `` and the `` in the `web.config` and that worked. So your solution is the same!! But for now I have another problem, when I use my browser to access my request like that : `http://localhost:54792/Service1.svc/TryThis `that works, but if I replace "localhost" by 127.0.0.1 or by my computer IP I have another 400 error. – lebarillier Nov 08 '18 at 08:28
  • sorry for the late response. In that specific case, I suggest you modify the IIS site binding configuration(IIS>>connections>>your website>>Bindings), change the IP address to All Unassigned. – Abraham Qian Nov 08 '18 at 08:43
  • Thank for you'r answer, I use a IIS Express for this application, I find this https://stackoverflow.com/questions/3313616/how-to-enable-external-request-in-iis-express but I don't find solution on this page (maybe because the answers date from 2010) I tryed to add the `` line but I don't have part about my project in `applicationhost.config`. If I don't find solution I will ask another question about this. – lebarillier Nov 09 '18 at 01:44
  • 1
    I found the solution! when I added this line `` I added in the wrong `applicationhost.config`. So now that work well. Chrome, postman, and other computer can use my service!! Just my android app still can't but I will find the answer soon! have a good day !! – lebarillier Nov 09 '18 at 02:47