How to access WCF service methods from browser?

It's a code that was created in Visual Studio 2013 by default wizard. In project properties I set to use Local IIS. WCF Test Client test it successfully. But if I access page

http://localhost/WcfService1/Service1.svc/GetTime

in browser then I see empty browser screen and Fiddler show "HTTP/1.1 400 Bad Request". I understand that I need to modify web.config, and attributes for the method in interface but don't know how. Could you help me? Thank you.

IService1.cs

using System.Runtime.Serialization;
using System.ServiceModel;
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetTime();
    }
}

Service1.svc.cs

using System;
namespace WcfService1
{
    public class Service1 : IService1
    {
        public string GetTime()
        {
            return DateTime.Now.ToShortTimeString();
        }
    }
}

web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </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>

Upvotes: 5

Views: 21637

Answers (1)

Thank you to sakir and Tim, you comments allow me to look for answer in proper places. Yes, this is SOAP service, so I need to reconfigure it in order to access by web http.

  1. I added section "behavior" and configuration of this behavior that allow HttpBinding in web.config
  2. I added attribute [WebGet] to the method that I want to make visible in the browser.

Note: similar result we can get in Visual Studio, if we will create WCF web service by adding in empty project "WCF Service (Ajax-enabled)" template (which I had hard time finding in VS2013, but it's on the top in VS2012 as a separate project). This will modify web.config for us, and give working code example (however not based on interface).

Adding WCF Service(Ajax-enabled) template to the empty project in VS2013

Modified files and reconfigured web.config below:

IService1.cs

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet]
        string GetTime();
    }
}

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.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

      **<!--added behavior-->
      <endpointBehaviors>
        <behavior name="WcfService1.Service1AspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>**

    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    **<!--added service behaviour configuration-->
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="" behaviorConfiguration="WcfService1.Service1AspNetAjaxBehavior"
          binding="webHttpBinding" contract="WcfService1.IService1" />
      </service>
    </services>**

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

Upvotes: 9

Related Questions