- Use generated early bound service proxy classes
- Use service reference
Creating WCF web service
We use very similar WCF web service which used to consume C# web service using Android application ("C# Web Services and Android").
WCF web service provides functionality to retrieve person details based on the person Id. Use same person business object class to create person object which used in "C# Web Services and Android". Create "IPersonOperations" interface to define person operations and create "PersonOperations" concrete class to implements the services already defined in " IPersonOperations " interface class.
interface IPersonOperations { ////// Creates the person list. /// ///list of person ListCreatePersonList(); /// /// Gets the person by unique identifier. /// /// The user unique identifier. ///person Person GetPersonById(string userId); }
public class PersonOperations:IPersonOperations { ////// Creates the person list. /// ///list of person public ListCreatePersonList() { List personList = new List (); Person person1 = new Person(); person1.FirstName = "Iron"; person1.LastName = "Man"; person1.Id = "IM01"; Person person2 = new Person(); person2.FirstName = "Avatar"; person2.LastName = "Aang"; person2.Id = "AA02"; Person person3 = new Person(); person3.FirstName = "Avatar"; person3.LastName = "Korra"; person3.Id = "AA03"; personList.Add(person1); personList.Add(person2); personList.Add(person3); return personList; } /// /// Gets the person by unique identifier. /// /// The user unique identifier. ///person public Person GetPersonById(string userId) { ListpersonList = this.CreatePersonList(); if (string.IsNullOrEmpty(userId) || personList == null || personList.Count == 0) { return null; } Person result = personList.Where(x => x.Id.Equals(userId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (result == null) { return null; } return result; } }
Creating WCF operation contract and service contract
To access the WCF web service we need to create service contract and operation contract. Service contract create service interface to outside of the web service. Operation contract defines the operations which can perform under the relevant service contract. Namespace of the service contract is very important. Accessing web service and operations are totally depends on the service contract namespace. If you didn't provide namespace for service contract then it use default namespace " http://tempuri.org/". In this scenario we use namespace called " http://DemoWebService.com/ ". " IDemoService " is the service which provided by WCF web service and " GetPersonById " is the operation which provided under previously mentioned service.
[ServiceContract(Namespace="http://DemoWebService.com/")] public interface IDemoService { ////// Gets the person by unique identifier. /// /// The user unique identifier. ///person [OperationContract] Person GetPersonById(string userId); }
public class DemoService : IDemoService { ////// Gets the person by unique identifier. /// /// The user unique identifier. ////// person /// public Person GetPersonById(string userId) { if (string.IsNullOrEmpty(userId)) { return null; } else { PersonOperations result = new PersonOperations(); return result.GetPersonById(userId); } } }
Important: before add the namespace for the WCF service please add " System.ServiceModel " reference. Otherwise it will give compile time errors.
Configure WCF web service
This is the one of the important step of the hosting WCF service. Following web config shows the important configurations we need to set before host WCF web service.
I add the behavior called " WcfService.DemoService " under service behaviors to register Demo Service behavior in http context. In this example I only used basic http bindings not use secure https bindings. Due to that reason need to set http binding to true. Set basichttpBindings with none security mode.
To avoid following error set " multipleSiteBindingsEnabled " to false. " When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address. If you are specifying a relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://localhost:8080/DemoService.svc'. " Then finally add service end point like following.