Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access WCF service methods from browser?

Tags:

browser

wcf

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>
like image 575
Deleted because of negativity Avatar asked Aug 14 '14 17:08

Deleted because of negativity


People also ask

How can I access WCF service?

Add Service Reference in Visual Studio With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button.

How do I access SVC files?

Solution: Open IIS Manager (Start > Run > search "inetmgr"). Browse from the top level server to Sites and expand the Default Web Site. Select each of the Revit Server applications, and in the Content View, right click on the SVC file and select Browse.

How do I hit a WCF service?

To step into a WCF Service Create a Visual Studio solution that contains both the WCF client and WCF service projects. In Solution Explorer, right-click the WCF Client project and then click Set as Startup Project. Enable debugging in the app. config or web.

Can we call WCF service from angular?

Yes you can use wcf service first I will suggest please upgrade with angular 4 as there are alots of bugs in angular 2 below are angular 4 service sample code.


1 Answers

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>
like image 191
Deleted because of negativity Avatar answered Oct 18 '22 18:10

Deleted because of negativity