180 likes | 196 Views
Learn about how to distribute functionality in Webservice using WCF with ASP.NET in 1-tier, 2-tier, and n-tier scenarios. Explore WCF architecture, data protocols, hosting options, and service setup. Discover the possibilities and configurations of WCF with various technologies.
E N D
Different technology combined to form WCF. http://wcftutorial.net/Introduction-to-WCF.aspx
ASP.NET - 1 tier distribution http://msdn.microsoft.com/en-us/library/bb547119.aspx
ASP.NET - 2 tier distribution http://msdn.microsoft.com/en-us/library/bb547119.aspx
ASP.NET - n tier distribution http://msdn.microsoft.com/en-us/library/bb547119.aspx
Server Client stub proxy remoting remoting formatter channel formatter listener Remoting – architectureServer and client are both .NET • General architecture • Proxy, stub, formatting and channel might be changed. Eg is a binary formatting, witch both is supported by TCP og HTTP, more effective than a textbased (XML)
Remoting service - opportunities • Server-activated object (SAO)also called ”Wellknown objects” • Singleton: one and only one serverobject for all clients • Threadproblem for object data • SingleCall: One object per each method call • The most easy solution, but less effective • State might be keept by using files or database • Client-activated object (CAO) • Activated: One object per each client • lifetime problems – Leasing not supported by IIS
Windows Communication Foundation Architecture http://msdn.microsoft.com/en-us/library/ms733128.aspx
Hosting WCF Side-by-Side with ASP.NET http://msdn.microsoft.com/en-us/library/aa702682.aspx
WCF support the 3 state scenarios Possibly also look for other info about these 3 scenarions on http://www.codeproject.com/KB/WCF/WCFInstance.aspx
WCF host & protocol • Wcfmightlike remoting (singleton og singlecall) beintegrated in an asp.net web project and behosted by the IIS (Internet Information Server),wherewcfalsomightgetaccess for the ASP.NET Session, Application objectsect. • Wcfmightalsobeusedwithoutusing the IIS as selfhosted with otherprotocolsthan http. • Wcfalso support eg json and not only soap, witch not is easyusing the older ASP.NET webservices (asmx - technologi)
WCF data protocols WCF might also be configured to communicate by pure XML http://msdn.microsoft.com/en-us/library/bb547119.aspx
WCF setup • Service is defined with a ServiceContract on the interface / classwitchmightbeworking as service-object, and methodsthat must beavailableexternal for clients must bedefined as OperationContract’s • Transport-objects must bedefined as DataContract and field/propertywitchmightbetransported as data as DataMember’s.
Interface with the contract Normally you will define the contract on an interface but can also define this in the class, if you do not want an interface using System.ServiceModel.Web; // contains ServiceContract and OperationContract classes ……….. namespace MyNamespace { [ServiceContract] // Attribut on interfacet public interface IService1 { [OperationContract] // Attribut on methods witch should be avaible by the service (on proxy) string GetData(int value); If you want to use session on service in a WcfServiceApplication (can not be used on ASP.NET - website under ISS) must ServiceContract'en added parameters and it looks like this : [ServiceContract(SessionMode = SessionMode.Required)]
Implementation of class for the contract usingSystem.ServiceModel; ………. namespaceMyNameSpace { //Default is creattion of a new service-object per eachcall, but it mightbe changed //[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] // Default //[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] // Remember to threadsafe //[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] //DemandwsHttpBinding public class Service1 : IService1 { public stringGetData(intvalue) { return string.Format("You entered: {0} <{1}>", value + count, s); } Connected markeup Service1.svc file, witch is giving the binding to service1: <%@ ServiceHost Language="C#" Debug="true" Service=“MyNameSpace.Service1" CodeBehind="Service1.svc.cs" %> Remark: PerSession på Wwbserver(IIS) demandwsHttpBindingwitchdemandadjusting in web.configThe .SVC file couldalsobereplaced by setting up endpoint information in web.config
Data transport objects (DTO) If transferring complex objects (not simple types, string and array) then these will be serialized - this should be done using a DataContract - previously you could use [Serializable] - on Web service would only be transferred public fields and public property , corresponding to an XML serialization. using System.Runtime.Serialization; [DataContract] // class could be serialized public class CompositeType { [DataMember] // field shoud be included when serialized bool _boolValue = true; string _stringValue = "Hello "; [DataMember] // property value should be included as data when serialized public int OneInt {private get; set;}
Data transport objecter (DTO) DataContract and DataMember When using these you might yourself include a referece to get the dll part of project