Archive for November, 2014

Creating and consuming WCF Services

Friday, November 14th, 2014

Create a WCF Service Application
Run it, right click on the service and copy the address

Create a custom class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SandCastleServices
{
public class TestClass
{
public int Id;
public string Name;
public List<string> WordList;
}
}

in Service.svc.cs add function
public TestClass GetTestClass()
{
TestClass newClass = new TestClass();
newClass.Id = 3;
newClass.Name = “Test3″;
newClass.WordList = new List<string>();
newClass.WordList.Add(“asdf”);
newClass.WordList.Add(“gjh”);
newClass.WordList.Add(“ety”);

return newClass;
}

in IService.cs add an Operation contract
[OperationContract]
TestClass GetTestClass();

Create a Web Project
Right click on the project and coick on “Add Service Reference”
Paste the address

In the code:
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
string test = client.GetData(89);

//This is how you get a custom class
ServiceReference1.TestClass testClass = client.GetTestClass();