WCF-2 Radu Nicolescu Department of Computer Science University of - - PowerPoint PPT Presentation

wcf 2
SMART_READER_LITE
LIVE PREVIEW

WCF-2 Radu Nicolescu Department of Computer Science University of - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

HelloWorld PingPong DataEF DataAD

WCF-2

Radu Nicolescu Department of Computer Science University of Auckland 21 Sept 2018

1 / 21

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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 public void Hello ( string name) { 8 . . . ; 9 // no return: one way 10 } 11 12 public string SayHello ( string name) { 13 . . . ; 14 return $” Hello , {name}” ; 15 } 16 }

6 / 21

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

HelloWorld PingPong DataEF DataAD

Testing

  • Additional service methods, to also test service behaviour

(multi-threading)

1 public s t a t i c Func<int> Tid = 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

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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 public interface I P i n g S e r v i c e { 3 [ OperationContract ( IsOneWay=true ) ] 4 [ WebGet ( ) ] 5 void Ping ( int t t l ) ; 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

slide-21
SLIDE 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

slide-22
SLIDE 22

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

slide-23
SLIDE 23

HelloWorld PingPong DataEF DataAD

Data Service EF models

  • NorthwindEntities: auto-generated model for the Northwind

database (selected objects)

  • Model1.Context.cs: database model
  • Customer.cs, Order.cs: C# classes for tables Customers,

Orders metadata

  • Model1.csdl, Model1.ssdl, Model1.msl: conceptual models,

storage models, mappings

  • SetEntitySetAccessRule(”∗”, EntitySetRights .AllRead): allow

read-access to all model items (here Customers and Orders)

  • MaxProtocolVersion: protocol version (we use the more

common V3)

  • UseVerboseErrors = true: to get more detailed error message
  • n the returned HTML page (own addition)

17 / 21

slide-24
SLIDE 24

HelloWorld PingPong DataEF DataAD

Data Service EF models

  • NorthwindEntities: auto-generated model for the Northwind

database (selected objects)

  • Model1.Context.cs: database model
  • Customer.cs, Order.cs: C# classes for tables Customers,

Orders metadata

  • Model1.csdl, Model1.ssdl, Model1.msl: conceptual models,

storage models, mappings

  • SetEntitySetAccessRule(”∗”, EntitySetRights .AllRead): allow

read-access to all model items (here Customers and Orders)

  • MaxProtocolVersion: protocol version (we use the more

common V3)

  • UseVerboseErrors = true: to get more detailed error message
  • n the returned HTML page (own addition)

17 / 21

slide-25
SLIDE 25

HelloWorld PingPong DataEF DataAD

Data Service EF models

  • NorthwindEntities: auto-generated model for the Northwind

database (selected objects)

  • Model1.Context.cs: database model
  • Customer.cs, Order.cs: C# classes for tables Customers,

Orders metadata

  • Model1.csdl, Model1.ssdl, Model1.msl: conceptual models,

storage models, mappings

  • SetEntitySetAccessRule(”∗”, EntitySetRights .AllRead): allow

read-access to all model items (here Customers and Orders)

  • MaxProtocolVersion: protocol version (we use the more

common V3)

  • UseVerboseErrors = true: to get more detailed error message
  • n the returned HTML page (own addition)

17 / 21

slide-26
SLIDE 26

HelloWorld PingPong DataEF DataAD

Data Service EF models

  • NorthwindEntities: auto-generated model for the Northwind

database (selected objects)

  • Model1.Context.cs: database model
  • Customer.cs, Order.cs: C# classes for tables Customers,

Orders metadata

  • Model1.csdl, Model1.ssdl, Model1.msl: conceptual models,

storage models, mappings

  • SetEntitySetAccessRule(”∗”, EntitySetRights .AllRead): allow

read-access to all model items (here Customers and Orders)

  • MaxProtocolVersion: protocol version (we use the more

common V3)

  • UseVerboseErrors = true: to get more detailed error message
  • n the returned HTML page (own addition)

17 / 21

slide-27
SLIDE 27

HelloWorld PingPong DataEF DataAD

Data Service EF – how does it work

  • The service is hosted under IISExpress ⇒ base URL

http://localhost:8181/

  • An HTTP GET request arrives at the base service URL

http://localhost:8181/WcfDataService1.svc/...

  • WcfDataService1.svc is a text file, indicating a namespace and

a class name, that will actually offer the service

1 . . . S e r v i c e=” WebApplication1 . WcfDataService1 ”

  • The runtime looks for this class through the bin DLLs ...
  • Summary: URL ⇒ SQL SELECT ...

18 / 21

slide-28
SLIDE 28

HelloWorld PingPong DataEF DataAD

Data Service EF – how does it work

  • The service is hosted under IISExpress ⇒ base URL

http://localhost:8181/

  • An HTTP GET request arrives at the base service URL

http://localhost:8181/WcfDataService1.svc/...

  • WcfDataService1.svc is a text file, indicating a namespace and

a class name, that will actually offer the service

1 . . . S e r v i c e=” WebApplication1 . WcfDataService1 ”

  • The runtime looks for this class through the bin DLLs ...
  • Summary: URL ⇒ SQL SELECT ...

18 / 21

slide-29
SLIDE 29

HelloWorld PingPong DataEF DataAD

Data Service EF – how does it work

  • The service is hosted under IISExpress ⇒ base URL

http://localhost:8181/

  • An HTTP GET request arrives at the base service URL

http://localhost:8181/WcfDataService1.svc/...

  • WcfDataService1.svc is a text file, indicating a namespace and

a class name, that will actually offer the service

1 . . . S e r v i c e=” WebApplication1 . WcfDataService1 ”

  • The runtime looks for this class through the bin DLLs ...
  • Summary: URL ⇒ SQL SELECT ...

18 / 21

slide-30
SLIDE 30

HelloWorld PingPong DataEF DataAD

Data Service EF – how does it work

  • The service is hosted under IISExpress ⇒ base URL

http://localhost:8181/

  • An HTTP GET request arrives at the base service URL

http://localhost:8181/WcfDataService1.svc/...

  • WcfDataService1.svc is a text file, indicating a namespace and

a class name, that will actually offer the service

1 . . . S e r v i c e=” WebApplication1 . WcfDataService1 ”

  • The runtime looks for this class through the bin DLLs ...
  • Summary: URL ⇒ SQL SELECT ...

18 / 21

slide-31
SLIDE 31

HelloWorld PingPong DataEF DataAD

Data Service EF – how does it work

  • The service is hosted under IISExpress ⇒ base URL

http://localhost:8181/

  • An HTTP GET request arrives at the base service URL

http://localhost:8181/WcfDataService1.svc/...

  • WcfDataService1.svc is a text file, indicating a namespace and

a class name, that will actually offer the service

1 . . . S e r v i c e=” WebApplication1 . WcfDataService1 ”

  • The runtime looks for this class through the bin DLLs ...
  • Summary: URL ⇒ SQL SELECT ...

18 / 21

slide-32
SLIDE 32

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

19 / 21

slide-33
SLIDE 33

HelloWorld PingPong DataEF DataAD

Data Service A#4R Demo

  • Simulate a simple EF model!
  • Create C# classes for your ”table” metadata, with attributes

indicating the primary keys

1 [ DataServiceKey ( ”CustomerID” ) ] 2 public class MyCustomer { 3 public string CustomerID { get ; set ; } 4 public string Name { get ; set ; } 5 . . . 6 }

20 / 21

slide-34
SLIDE 34

HelloWorld PingPong DataEF DataAD

Data Service A#4R Demo

  • Build IQueryable sequences ≈ SQL tables

1 public IQueryable <MyCustomer> MyCustomers { 2 get { 3 Console . WriteLine ( ” . . . MyCustomers” ) ; 4 return MyCustomers . AsQueryable ( ) ; 5 } 6 }

  • Build associations MyCustomers ⇔ MyOrders

21 / 21