Windows Communication Foundation

Transcription

Windows Communication FoundationINNOVATION 2.0 WCF Goals and Reasoning Why it is still Valid for Enterprise Level Software Concepts and Details of WCFhttps://slideplayer.se/slide/2406010/ Johan Lindfors

Dilemma in the Enterprise IT WorldHi! we're the hotopen-source startupwe can help with allof your app containerization needs as soonas we geta Know-how is agingYoung (&Wild) but not "Enterprise /en.wikipedia.org/wiki/Docker %28software%29School of Engineering K. Rege, ZHAW2 von 108

What is WCF WCF is Microsoft’s implementation of a set of industry standards around ServiceOriented Architectures (SOA) in an Enterprise Service Bus (ESB) WCF provides interoperability between services. WCF provides developers with the essential off-the-shelf plumbing WCF provides many useful facilities for developing services, Such as hostingService instance managementAsynchronous callsAnd reliability, transaction management, disconnected queued calls, and security Most all of the WCF functionality is included in a single assembly calledsystem.Servicemodel.Dll in the system.Servicemodel namespace. WCF is part of .NET since version 3.0 however .NET Core only contains WCF client, i.e. for Server (full) .NET Framework is neededhttp://en.csharp-online.netSchool of Engineering K. Rege, ZHAW3 von 108

Comparing .NET vs. J2EE Stack at that gingJMSSocketsSocketsSockets Johan LindforsSchool of Engineering K. Rege, ZHAW4 von 108

EJB/SOA Architecturehttp://radar.zhaw.ch/ rege/ea fs16/ejb1.pdf An EJB/SOA Architecture has the following separation of t,z.B.Tomcatz.B. TomcatEnterpriseEnterpriseJavaBeanJavaBeanEJB Containerz.B. JBossObject Request Broker (ORB)Application ServerSchool of Engineering K. Rege, ZHAW5 von 108

SOA Container (EJB or WCF) Is the runtime environment of services EJB and WCF are SOA Containers Every "Enterprise Level" SOA Containerhas to provide a specific set of nfigurationLivecycleLivecycleManagementManagement See lecture "Distributed agementPersistencymanagementSchool of Engineering K. Rege, ZHAW6 von 108

WCF Goals Was originally invented to replace different technologies .NET RemotingASMX: ASP.NET Web ServicesMSMQWeb Services EnhancementsCOM (DCOM) WCF is - much - more complex compared to ASP.NET Web API i.e. ASMX But offers more features reliability, transaction, security If you don't need these features WCF may be the wrong dotnet/framework/wcf/feature-details/School of Engineering K. Rege, ZHAW7 von 108

Communication Technologies in WCF WCF is the foundation for a "connected system" in a Microsoft EnvironmentSchool of Engineering K. Rege, ZHAW8 von 108

What can be learned from WCF Flexible hosting for components and services WCF can be hosted in ASP.NET runtimeWindows ServiceCOM processWPF Application for peer-to peer computing Declarative behavior Attributes can be used to define aspects of service Communication Channels WCF offers multiple channels to communicate: HTTP, TCP, IPC, custom Security Infrastructure Standardized security environment, such as defined in WSE 3.0 Extensibility .WCF allows for custom channels, formatters and proxiesmay inject functionality inside the message flowSchool of Engineering K. Rege, ZHAW9 von 108

What can be learned from WCF Aspects of WCF in an -WCF.aspxSchool of Engineering K. Rege, ZHAW10 von 108

Windows Communication FoundationMicrosoft: "The unified programming model for rapidly buildingservice-oriented applications on the Windows chool of Engineering Unifies today’s distributed technology stacks Appropriate for use on-machine, cross machine, andcross Internet Codifies best practices for building distributedapplications in a service-oriented style and manner Interoperates with applications running on other platforms Integrates with Microsoft’s own distributed stacks K. Rege, ZHAW11 von 108

Advantages of WCF over SOAP Interoperability SOAP is the preferred protocol for WCFbut WCF services are also interoperable, with a variety of network protocols such as https, TCP, MSMQ etc.Applications or services are designed for distributed environment using a single model. Security and Reliability Security is a key element in any SOA, and it is provided in the form of authentication, authorization, confidentiality, integrity and auditingof messages shared between the client and the service. Support also for Plain XML, Ajax and REST formats that are no more restricted by SOAPshare plain XML messages between clients and servicesshare also JSON messages via FormattersSchool of Engineering K. Rege, ZHAW12 von 108

WS-* (W3C/OASIS)Step 2 – Workshops & Community DevStep 2 – Workshops & Community DevStep 3 – StandardizationStep 3 – StandardizationSOAP Specification (2018)Step 4 – Approved StandardStep 4 – Approved Standardhttp://radar.zhaw.ch/ rege/xml hs11/soap ps://www.oasis-open.org/ WS-BusinessActivityActivitycommittees/tc home.php?wg abbrev nfrastructureInfrastructureand Profilesand ol of EngineeringSOAPSOAP/ ML SchemaXML onFoundationSOAP / HTTPSOAP / HTTP K. Rege, ZHAW13 von 108

Hello WCFhttp://dotnetpattern.com/hello-world-wcfSchool of Engineering K. Rege, ZHAW14 von 108

1. Declare ServiceContract Console Solution and name the solutionas “LibraryServiceSolution”. rem: we do not use WCF Project types because wewant it to do step by step Add a new Interface name “ILibraryService” and follow below steps. Add a reference to “System.ServiceModel” assembly. System.ServiceModel assembly is the baseassembly of WCF Service.Add the “System.ServiceModel” namespace in the namespaces section.Add the [ServiceContract] attribute aboveusing System;the interface ILibraryService.using System.ServiceModel; ServiceContract attribute describes thatthis interface contains the methodsnamespace LibraryServiceSolution {signatures of WCF service.[ServiceContract]interface ILibraryServiceAdd method signature{“Book SearchBook(string bookName)” into[OperationContract]the interface.Book SearchBook(string bookName);Add the attribute [OperationContract] on}the method.} attribute describes that this method isincluded in WCF serviceSchool of Engineering K. Rege, ZHAW15 von 108

2. Declare Data Contract In WCF, data contract is used for serializing custom data types.In the interface, We are returning the custom type “Book” as a result of“SearchBook” method. Create a new class name “Book” Add a reference to “System.Runtime.Serialization” assembly.Add the namespace “System.Runtime.Serialization” to the namespaces section.using System;Add the [DataContract] attribute above theusing System.Runtime.Serialization;class “Book”. DataContract attribute specifies that thisnamespace LibraryServiceSolution{class is used in WCF service[DataContract]Add a new property "ID" of data type int inclass Book {class “Book”.[DataMember]Add the attribute [DataMember] to “ID” property.public int ID { get; set; } DataMember attribute specifies that this[DataMember]property is part of data contract.public string Name { get; set; }}Add a new property "Name" of data type string}in class “Book”.Add the attribute [DataMember] to “Name” property.School of Engineering K. Rege, ZHAW16 von 108

3. Implement Service Create a new class name “LibraryService” and follow below steps: Inherit from interface “ILibraryService”Implement the method “SearchBook”using System;using System.Collections.Generic;namespace LibraryServiceSolution {class LibraryService : ILibraryService {private List Book books;public LibraryService(){books new List Book ();for (int i 0; i 20; i){books.Add(new Book { ID i, Name "Name " i });}}public Book SearchBook(string bookName) {return books.Find(w w.Name bookName);}}}School of Engineering K. Rege, ZHAW17 von 108

4. Configure Service An endpoint is the defined by an “App.config” file Address defines the location of the serviceBinding in simple terms specify the transport protocol of the service.Contract list the operations of the service which we already defined in the “ILibraryService” interface ?xml version "1.0" encoding "utf-8" ? name: class of the service implementation. configuration behaviorConfiguration: see below system.serviceModel services service name "LibraryServiceSolution.LibraryService" behaviorConfiguration "LibraryServiceBehavior" host baseAddresses base addresses of our LibraryService add baseAddress "http://localhost:9999/LibraryService" / /baseAddresses /host endpoint address "" binding "basicHttpBinding" contract "LibraryServiceSolution.ILibraryService" / /service /services endpoint, containing address, binding and contract. behaviors Address is blank, so, our final WCF service address serviceBehaviors is “http://localhost:9999/LibraryService” “” behavior name "LibraryServiceBehavior" serviceMetadata httpGetEnabled "true" / /behavior the metadata of our service /serviceBehaviors /behaviors /system.serviceModel /configuration School of Engineering K. Rege, ZHAW18 von 108

5. Host Service For hosting the service, we use the “ServiceHost” class. We need to pass type information of our “LibraryService” service and call the open method.using System;using System.ServiceModel;namespace LibraryServiceSolution {class Program {static void Main(string[] args){ServiceHost server new Console.WriteLine("Your service is started.");Console.ReadLine();server.Close();}}} Run the program as Console Program: not in VS because we need it for building the clientrun the program from with administrator permissionsC:\Users\Karl School of Engineering K. Rege, ZHAW19 von 108

6. Access Client To check whether service is running Enter url of the service address http://localhost:9999/LibraryService in BrowserSchool of Engineering K. Rege, ZHAW20 von 108

7. Create Client Program New Console Project and name the Project e.g. “ConsoleApplication1” Add a Service Reference with NameSpace LibraryServiceSolutionSchool of Engineering K. Rege, ZHAW21 von 108

8. Implement Client Program Create an instance of "LibraryServiceClient" class. Call the SearchBook method.using System;using ce ConsoleApplication1{class Program{static void Main(string[] args){LibraryServiceClient client new LibraryServiceClient();Book book client.SearchBook("Name 12");Console.WriteLine("Book ID : " book.ID);Console.ReadLine();}}Congratulation, your first}WCF ServiceSchool of Engineering K. Rege, ZHAW22 von 108

WCF ArchitectureSchool of Engineering K. Rege, ZHAW23 von 108

WCF Architecture LayersWindows Communication Foundation is a multi-layered architecture thatprovides developers with a rich set of functionality. Contract define interface of service Runtime define runtime behavior Messaging define properties of the Messagingchannel Hosting and Activation Type of HostWhen Service is activatedSchool of Engineering K. Rege, ZHAW24 von 108

ContractsSchool of Engineering K. Rege, ZHAW25 von 108

Contract First Design Contract first design first: design the service contract in terms of the data, messages, and interface it will expose Usually these contracts are called "IDL"second: generate the service interface code from the contractFrom there, you can implement the code behind the service interface that performs the processingrequired. Advantage: This allows you to concentrate on the format of the messages andthe data types they use at the beginning of the process to maximizeinteroperability and compatibility.e.g.e.g.CICS- CICS- Corba- RMI/IIOP- SOAPCorba- RMI/IIOP- SOAPIf technology has to be changed, these contracts can be transcribed SOAP however has suffered from a unreadable contract format: WDSL and XSD. Contract First requires comprehensible contract format - WCF doesSchool of Engineering K. Rege, ZHAW26 von 108

Programming of Contract Code First: Avoid[ServiceContract]public class OrderEntry {[OperationContract(IsOneWay true)]public void PlaceOrder(PurchaseOrder order){ return; }} Contract First: do it[ServiceContract]public interface IOrderEntry {[OperationContract(IsOneWay true)]void PlaceOrder(PurchaseOrder order);}public class OrderE

What is WCF WCF is Microsoft’s implementation of a set of industry standards around Service Oriented Architectures (SOA) in an Enterprise Service Bus (ESB) WCF provides interoperability between services. WCF provides developers with the essential off-the-shelf plumbing WCF provides many useful facilities for developing services,