ASP.net Examples

Download Java SE 8.

Create ASP.net WCF Service in Visual Studio 2017 and Test Using WCF Test Client

Steps to Create ASP.net WCF Service

1) Create a new asp.net WCF Service Application project.

asp.net wcf service application project

2) Create ITestService.cs file with below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace TestWcfService
{
    [ServiceContract]
    public interface ITestService
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

3) Create WCF Service file as TestService.svc with below markup.

<%@ ServiceHost Language="C#" Debug="true" Service="TestWcfService.TestService" CodeBehind="TestService.svc.cs" %>

4) Add this code in WCF service code behind file (TestService.svc.cs

here two wcf service methods are defined, first one is GetData method which takes integer as input and return string value.
Another one WCF service method is GetDataUsingDataContract which takes composite type object as input and returns.
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace TestWcfService
{
    public class TestService : ITestService
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

5) Add this configuration under configuration tag after system.web tag in web.config of wcf service application. this configuration is for only basic http binding.

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Access WCF service information

We can access WCF service information using URL 'http://localhost:/TestService.svc' on browser


Example:
http://localhost:65472/TestService.svc
WCF Service information

Access WCF service WSDL information

We can access WCF service WSDL using URL 'http://localhost:/TestService.svc?wsdl' on browser


Example:
http://localhost:65472/TestService.svc?wsdl
WCF Service WSDL information

Test WCF Service using WCF Test Client in Visual Studio

Running WCF Service with WCF Test Client just like launching any asp.net web or press F5.

WCF Test Client

When clicking on WCF Service method, request and response blocks are displayed in right side.

Provide input values for request blocks and click invoke button, response is updated from WCF service.

WCF Test Client

WCF Test Client is not opened in Visual Studio

Select Set As Start Page for "TestSetvice.svc" on solution window as below

WCF Test Client Popup issue



Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^