lebarillier
lebarillier

Reputation: 6714

How can I use a WCF Service?

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.

Upvotes: 0

Views: 403

Answers (1)

Abraham Qian
Abraham Qian

Reputation: 7522

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.

Upvotes: 2

Related Questions