HelloWorld PingPong DataEF DataAD WCF-2 Radu Nicolescu Department of Computer Science University of Auckland 21 Sept 2018 1 / 21
HelloWorld PingPong DataEF DataAD 1 WCF Hello World 2 WCF Ping Pong 3 WCF Data Service EF 4 WCF Data Service A#4R Demo 2 / 21
HelloWorld PingPong DataEF DataAD Outline 1 WCF Hello World 2 WCF Ping Pong 3 WCF Data Service EF 4 WCF Data Service A#4R Demo 3 / 21
HelloWorld PingPong DataEF DataAD Declarative Web Interface – Attributes • Auto: ”Wire” (URLs) ⇔ Method invocations 1 [ S e r v i c e C o n t r a c t ( ) ] 2 public interface I H e l l o W o r l d S e r v i c e { 3 [ OperationContract ( IsOneWay= true ) ] 4 [ WebGet ( ) ] 5 void Hello ( string name ) ; 6 7 [ OperationContract ( ) ] 8 [ WebGet( ResponseFormat=WebMessageFormat . Json ) ] 9 string SayHello ( string name ) ; 10 } • Read more: see links in the code samples, ... 4 / 21
HelloWorld PingPong DataEF DataAD Declarative Web Interface – Attributes • [X] : auto-generated instance of XAttribute class • ServiceContractAttribute : ”Wire” interface • OperationContractAttribute : ”Wire” method • IsOneWay= true : property of OperationContractAttribute • indicates no response • WebGetAttribute : specific ”Wire” format: HTTP REST • ResponseFormat=WebMessageFormat.Json: property of WebGetAttribute • selects JSON format (vs XML) 5 / 21
HelloWorld PingPong DataEF DataAD Declarative Web Interface – Attributes • [X] : auto-generated instance of XAttribute class • ServiceContractAttribute : ”Wire” interface • OperationContractAttribute : ”Wire” method • IsOneWay= true : property of OperationContractAttribute • indicates no response • WebGetAttribute : specific ”Wire” format: HTTP REST • ResponseFormat=WebMessageFormat.Json: property of WebGetAttribute • selects JSON format (vs XML) 5 / 21
HelloWorld PingPong DataEF DataAD Declarative Web Interface – Attributes • [X] : auto-generated instance of XAttribute class • ServiceContractAttribute : ”Wire” interface • OperationContractAttribute : ”Wire” method • IsOneWay= true : property of OperationContractAttribute • indicates no response • WebGetAttribute : specific ”Wire” format: HTTP REST • ResponseFormat=WebMessageFormat.Json: property of WebGetAttribute • selects JSON format (vs XML) 5 / 21
HelloWorld PingPong DataEF DataAD Server side implementation • Service implementation of the service contract 1 [ S e r v i c e B e h a v i o r ( 2 InstanceContextMode=InstanceContextMode . Single , 3 ConcurrencyMode=ConcurrencyMode . M u l t i p l e ) ] 4 5 public class HelloWorldService : 6 I H e l l o W o r l d S e r v i c e { 7 Hello ( string name) { public void 8 . . . ; 9 // no return: one way 10 } 11 12 SayHello ( string name) { public string 13 . . . ; 14 return $” Hello , { name } ” ; 15 } 16 } 6 / 21
HelloWorld PingPong DataEF DataAD Server side implementation • ServiceBehaviorAttribute : specifies the internal execution behavior of a service contract implementation • property InstanceContextMode=InstanceContextMode.Single • indicates one single instance, automatically created • property ConcurrencyMode=ConcurrencyMode.Multiple • indicates automatic multi-threading (CAVEAT: potential race conditions!) 7 / 21
HelloWorld PingPong DataEF DataAD Server side implementation • ServiceBehaviorAttribute : specifies the internal execution behavior of a service contract implementation • property InstanceContextMode=InstanceContextMode.Single • indicates one single instance, automatically created • property ConcurrencyMode=ConcurrencyMode.Multiple • indicates automatic multi-threading (CAVEAT: potential race conditions!) 7 / 21
HelloWorld PingPong DataEF DataAD Server side implementation • ServiceBehaviorAttribute : specifies the internal execution behavior of a service contract implementation • property InstanceContextMode=InstanceContextMode.Single • indicates one single instance, automatically created • property ConcurrencyMode=ConcurrencyMode.Multiple • indicates automatic multi-threading (CAVEAT: potential race conditions!) 7 / 21
HelloWorld PingPong DataEF DataAD Server side hosting • External host: IIS, IISExpress, ... • Self-hosted (sketch) 1 Uri baseAddress = 2 new Uri ( ” http :// l o c a l h o s t :8081/ h e l l o ” ) ; 3 4 WebServiceHost host = 5 new WebServiceHost ( 6 typeof ( HelloWorldService ) , baseAddress ) ; 7 8 ServiceEndpoint ep = 9 host . AddServiceEndpoint ( 10 typeof ( I H e l l o W o r l d S e r v i c e ) , 11 new WebHttpBinding () , ”” ) ; 12 13 host . Open ( ) ; • Service endpoint: actual access to the service functionality 8 / 21
HelloWorld PingPong DataEF DataAD Server side hosting • External host: IIS, IISExpress, ... • Self-hosted (sketch) 1 Uri baseAddress = 2 new Uri ( ” http :// l o c a l h o s t :8081/ h e l l o ” ) ; 3 4 WebServiceHost host = 5 new WebServiceHost ( 6 typeof ( HelloWorldService ) , baseAddress ) ; 7 8 ServiceEndpoint ep = 9 host . AddServiceEndpoint ( 10 typeof ( I H e l l o W o r l d S e r v i c e ) , 11 new WebHttpBinding () , ”” ) ; 12 13 host . Open ( ) ; • Service endpoint: actual access to the service functionality 8 / 21
HelloWorld PingPong DataEF DataAD Server side hosting • External host: IIS, IISExpress, ... • Self-hosted (sketch) 1 Uri baseAddress = 2 new Uri ( ” http :// l o c a l h o s t :8081/ h e l l o ” ) ; 3 4 WebServiceHost host = 5 new WebServiceHost ( 6 typeof ( HelloWorldService ) , baseAddress ) ; 7 8 ServiceEndpoint ep = 9 host . AddServiceEndpoint ( 10 typeof ( I H e l l o W o r l d S e r v i c e ) , 11 new WebHttpBinding () , ”” ) ; 12 13 host . Open ( ) ; • Service endpoint: actual access to the service functionality 8 / 21
HelloWorld PingPong DataEF DataAD Server side hosting • Further • How to terminate the service • How to dispose of used system resources • How to deal with errors/exceptions 9 / 21
HelloWorld PingPong DataEF DataAD Client side • Using the same interface, if available • Using a reconstructed interface, from the service metadata endpoint • Tools, wizards: VS, Linqpad, SvcUtil.exe, ... • Using URLs directly 10 / 21
HelloWorld PingPong DataEF DataAD Testing • Additional service methods, to also test service behaviour (multi-threading) 1 Func < int > Tid = public s t a t i c 2 () = > Thread . CurrentThread . ManagedThreadId ; 3 4 public s t a t i c Func < double > M i l l i s = 5 () = > DateTime .Now. TimeOfDay . T o t a l M i l l i s e c o n d s ; • Tid: Thread ID • Milliseconds since start of the day 11 / 21
HelloWorld PingPong DataEF DataAD Testing • Sample output for several concurrent clients 1 . . . 2 [ 5 ] [46800192.5668] SayHello r e c e i v e d : mano6 3 [ 3 ] [46800195.5665] Hello r e c e i v e d : xinfeng1 4 [ 8 ] [46800204.5683] Hello r e c e i v e d : xinfeng2 5 [ 3 ] [46800208.5662] Hello r e c e i v e d : xinfeng3 6 [ 8 ] [46800209.5664] SayHello r e c e i v e d : mano7 7 [ 3 ] [46800209.5664] SayHello r e c e i v e d : xinfeng1 8 [ 8 ] [46800272.564] Hello r e c e i v e d : xinfeng5 9 . . . • First column: Thread ID • Second columns: Milliseconds since start of the day 12 / 21
HelloWorld PingPong DataEF DataAD Outline 1 WCF Hello World 2 WCF Ping Pong 3 WCF Data Service EF 4 WCF Data Service A#4R Demo 13 / 21
HelloWorld PingPong DataEF DataAD Ping Pong Interfaces • Two nodes, mutual client/servers, that exchange one-way Ping/Pong messages 1 [ S e r v i c e C o n t r a c t ( ) ] 2 I P i n g S e r v i c e { public interface 3 [ OperationContract ( IsOneWay= true ) ] 4 [ WebGet ( ) ] 5 Ping ( int t t l ) ; void 6 } 7 [ S e r v i c e C o n t r a c t ( ) ] 8 public interface IPongService { 9 [ OperationContract ( IsOneWay= true ) ] 10 [ WebGet ( ) ] 11 void Pong ( int t t l ) ; 12 } • Ttl: time to live = number of hops before the message exchange stops 14 / 21
HelloWorld PingPong DataEF DataAD Outline 1 WCF Hello World 2 WCF Ping Pong 3 WCF Data Service EF 4 WCF Data Service A#4R Demo 15 / 21
HelloWorld PingPong DataEF DataAD Data Service EF • Specialised API for exposing Entity Model objects (tables, associations) over HTTP REST OData • Simple scenario: (almost) all code auto-generated (VS) 1 namespace WebApplication1 { 2 public class WcfDataService1 : 3 EntityFrameworkDataService < NorthwindEntities > { 4 public s t a t i c void I n i t i a l i z e S e r v i c e ( 5 D a t a S e r v i c e C o n f i g u r a t i o n c o n f i g ) { 6 c o n f i g . SetEntitySetAccessRule ( ” ∗ ” , 7 E n t i t y S e t R i g h t s . AllRead ) ; 8 c o n f i g . DataServiceBehavior . MaxProtocolVersion = 9 DataServiceProtocolVersion . V3 ; 10 c o n f i g . UseVerboseErrors = true ; 11 } 12 } 13 } 16 / 21
Recommend
More recommend