Presentation is loading. Please wait.

Presentation is loading. Please wait.

OE-NIK HP Advanced Programming WCF WCF SOAP service, host, client Exception handling (NOT required)

Similar presentations


Presentation on theme: "OE-NIK HP Advanced Programming WCF WCF SOAP service, host, client Exception handling (NOT required)"— Presentation transcript:

1 OE-NIK HP Advanced Programming WCF WCF SOAP service, host, client Exception handling (NOT required)

2 OE-NIK HP Advanced Programming WCF WCF SOAP service, host, client Exception handling (NOT required)

3 V1.0 IP-based alternatives to call a method TCP/UDP (or theoretically even ICMP) –Send and receive a sequence of bytes, must handle the format –Difficult to implement, very fast, almost zero overhead SOAP –Pre-defined SOAP XML formats to define and call the service methods and transmit various data types (array, list, object) –Arbitrary protocol to transmit the XML messages, usually HTTP –Very easy to implement, relatively slow, big overhead –We will use this one REST –Custom JSON format to transmit unique objects (rarely XML) –We rarely need complex parameters, in this case we use HTTP POST to send a JSON object (rarely XML) –Typically we only need the method name and string/int parameters, thus most of the method calls can be represented as HTTP GET requests –The answer is typically JSON (rarely XML) –Easy to implement, medium speed, medium overhead 3 OE-NIK HP

4 V1.0 Windows Communication Foundation An advanced API to develop and use distributed systems/services Exists since.NET 3.0, hides the physical communication Integrates all the technologies that existed before in.NET –XML-webservices, DCOM,.NET remoting, etc... –This means: SOAP, since the beginning! –Later: REST (REST+Entity Framework: WCF Data Services!) –The actual TCP/HTTP communication is hidden 4 OE-NIK HP Client applicationWCF host ProxyWCF service

5 V1.0 WCF ABC To facilitate the communication, the following data are necessary: –Address: for accessing the service Protocol:// [:port]/path e.g. http://localhost:8080/MyService –Binding: to specify the network protocol, encoding, transport layer E.g. WSHttpBinding for web services NetTcpBinding for non-HTTP communication –Contract: to describe the accessible methods All of these are described in the WSDL... We don’t want to create WSDL by hand! 5 OE-NIK HP

6 Advanced Programming WCF WCF SOAP service, host, client Exception handling (NOT required)

7 V1.0 Parts of a WCF application WCF service –Usually a DLL that contains the descriptor interface and the actual service implementation –We use attributes to mark the roles of the actual interfaces/classes/methods –The WSDL is automatically generated! WCF service host –This application hosts (executes) the service (i.e. creates the instance, etc.) –With SOAP this can be any.NET executable (console, WPF) –With REST this is typically a web application / web page hosted by IIS (this is also possible with soap) WCF client –Uses the service via a proxy class –Can be any.NET application 7 OE-NIK HP

8 V1.0 Creating a WCF SOAP service Create a WCF Service Library project –This generates the skeleton – always use automatic rename! ServiceContract –An interface that describes the methods of the service –Every accessible method must be marked with the attribute OperationContract –Should specify a unique namespace E.g.. [ServiceContract(Namespace="http://nik.uni-obuda.hu/hp")] DataContract –Every class that is sent/received –Usually contains no methods (they cannot be transferred...) –Public properties are marked with the DataMember attribute Service class –Implements the ServiceContract interface –Actual service implementation 8 OE-NIK HP

9 V1.0 To test the service Visual Studio can create a WCF Test Client that can be used to test the service methods The App.config is required  we only have the app.config near the DLL if we want tot test the service. Otherwise: rename/move Use the Invoke button to call the method Check the actual SOAP communication 9 OE-NIK HP

10 V1.0 App.config An XML file With WCF: Contains the service descriptors –ABC: Address, Binding, Contract –Service Behaviors, MEX (metadata) configuration –Usually automatically updated Edit with wizard –Right click on App.config; Edit WCF configuration –Services: class name –Host: IP address –Endpoints: ABC and MEX data 10 OE-NIK HP

11 V1.0 App.config 11 OE-NIK HP ABC MEX endpoint Behaviors for the MEX

12 V1.0 Creating a service host New project (console app), add reference to System.ServiceModel Move the App.Config into this project from the DLL The host creates an instance using the type as a parameter –ServiceHost host = new ServiceHost(typeof(EightBallService)) The service is closed on keypress/enter key –using: automatic release of resources, without the GC 12 OE-NIK HP

13 V1.0 Creating a service client Same process that we used last lesson Host must be running when we add the service reference Run separately (or: multiple startup projects) 13 OE-NIK HP

14 V1.014 OE-NIK HP Exercises Create a service that allows us to get/set the ratings for restaurants (store everything in XML files) –GetRestaurants(), GetRatings(int restaurant), AddRating(int restaurant, int rating, string comment) –Use LINQ to XML Create a service that allows us to store and query log events in a central location –One entry = software name, entry text, datetime –Store the entries in an XML file –The Read(string programname) method should process this XML

15 V1.0 Solution 1.Create the service 1.WCF Service Library -> LogServiceDLL 2.Contract -> ILogService 3.Functionality -> LogService 2.Create the host 1.Console Application -> LogServiceHost 2.ABC ->Move the App.Config file here 3.Create the client 1.Do NOT add reference to the DLL!!! (same name, same properties, different classes!) 2.Console Application -> LogServiceTester 15 OE-NIK HP

16 Advanced Programming WCF WCF SOAP service, host, client Exception handling (NOT required)

17 V1.0 Exceptions in WCF If unhandled exceptions occur, the exception is thrown to the client –CommunicationException - the server did not provide a meaningful reply; this might have been caused due to a contract mismatch, a premature session shutdown or an internal server error. –The communication channel is closed To keep the channel open, we must use SOAP-based exception handling: –returnUnknownExceptionsAsFaults / [ServiceBehavior(IncludeExceptionDetailInFaults=true)]: Every exception will be transmitted as fault messages INSECURE –FaultException (FaultContract needed in addition to the usual ABC) 17 OE-NIK HP

18 V1.0 SOAP Fault Typical SOAP error fields: –faultcode: required string. Usually starts with the text “server” or “client”, depending on where the error originated from –faultstring: readable error message –faultactor: the exact fault location In WCF, we have to create a FaultException instance Every class can be put into a SOAP fault (the FaultContract attribute is required) –new FaultException (myfe,faultReason,faultCode); myfe: encapsulated exception instance faultReason: same as faultString faultCode: same as faultCode 18 OE-NIK HP

19 V1.0 FaultContracts The WCF contract (descriptor interface) must contain the information required for the possible exceptions [OperationContract] [FaultContract(typeof(SomeCustomFaultException))] double GetBMI(double weight, double height); –If the contract has no FaultContract, then everything will be thrown as UnknowFaultException –The FaultContract will be put into the WSDL Classes used with the FaultContract attributes must be also included in the WSDL – DataContract + DataMember [DataContract] public class SomeCustomFaultException { [DataMember] public string Message{get;set;} } 19 OE-NIK HP

20 V1.0 Throwing and catching faults Create the descriptor instance Create a FaultException instance Specify the faultReason and the faultCode –SomeCustomFaultException error = new SomeCustomFaultException(); error.Message = "Zero value passed for height“; throw new FaultException ( error, new FaultReason(ex.StackTrace), new FaultCode("Client.DivideByZero")); In the client, catch the exception the usual way –catch (System.ServiceModel.FaultException fex) 20 OE-NIK HP

21

22


Download ppt "OE-NIK HP Advanced Programming WCF WCF SOAP service, host, client Exception handling (NOT required)"

Similar presentations


Ads by Google