Bellow i have explained how to create a simple WCF service in Visual Studio 2013
1. Click on new Project
2. From the left hand side of the templates please select WCF then you will get a list of templates associated with WCF.
3. Please select WCF service Application from the list. By default our visual studio will add one Interface and one svc file.
The interface will be have bellow contracts:
-
ServiceContract
- OperationContract
- DataContract
- DataMember
it looks like bellow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace MyService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[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; }
}
}
}
The Service1.svc.cs file looks like bellow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace MyService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
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;
}
}
}
4. The service will look like bellow in browser.
Now please create one website to consume the Service. Now we have the website.
The next work is to add service reference for the service, which we have created above.
1. Right click on the Website and the add service reference. Bellow i have provided the screenshots, please have a look.
3. A pop up will open there you can add your service reference URL and you can add your service there.
You can get the service reference url by viewing the service in browser. From the browser you can get the service url.
You can change your reference name like bellow:
Now you are done with reference adding in website. Now you can call the service for the website. Bellow here is some code
using MyServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class User : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Service1Client client = new Service1Client();
lblUserName.Text = client.GetData(4);
}
}
This was small demo to Create and consume WCF service. Please put your valuable comments.
CodeProject