1) Create a new 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.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>
http://localhost:65472/TestService.svc
http://localhost:65472/TestService.svc?wsdl
Running WCF Service with WCF Test Client just like launching any asp.net web or press F5.
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.
Select Set As Start Page for "TestSetvice.svc" on solution window as below
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page