presentation 24: windows communication foundation introduced objektorienteret netværkskommunikation
TRANSCRIPT
Presentation 24:Windows Communication Foundation
Introduced
Objektorienteret Netværkskommunikation
Introducing WCF
• Windows Vista => .NET Framework 3.0• Also for Windows XP, 2003 Server & 2007• Not Windows Mobile / Windows CE
• Unified Service-Oriented Programming Model
• Replaces / Suplements • .NET Remoting• DCOM• ASP.NET Web services• MSMQ (Queued Messaging) • .NET Enterprise Services
• Protocol Neutrality and Flexibility
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnlong/html/wcfroadmap.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnlong/html/wcfarch.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnlong/html/wcfroadmap.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnlong/html/wcfarch.asp
Learning WCF
• WCF much like .NET Remoting / ASP.NET WS
• Easier / More extensive
• ITONK will adress key issues of WCF
WCF Basic Concepts
• Remote system (like .NET Remoting) is:• Exchanging Messages
• Using Channels, consisting of
• Encodeders• Transports
• Also called the Channel layer
• On top of this is the Service Model Layer
WCF Architecture
Channels
• Channels is chain of stacks • Much like .NET Remoting• Sending messages, being encoded and transported
Messages
• WCF Applications Exchanges Messages• Modelled on SOAP messages
• Envelope, header, body
• Adressing
• Messages are smallest unit of transmission• Messsages have a SOAP and addressing version
Messages
• Message Exchange Pattern (MEP)• One way, request, response, duplex
Channels
• Messages are payload• Channels provide transmission stream• Protocol channels
• Independent of transport
• Extensible
• Transport channels• HTTP, TCP, MSMQ, P2P, Named Pipes
• Extensible
Encoders
• Text (XML, interop) • JSON/POX (XML or JSON, interop)• MTOM (XML with binary part, interop)• Binary (non-interop)
Service Model Layer
• Channel Layer may be used alone – but low level• Service Model Layer
• Build on top of Channel Layer
• .NET Typed layer
• Uses object serialization to generate message XML
Endpoints
• Applications communicates through endpoints• Endpoints defined by WCF ABC
• Address (where is the service)
• Binding (which transport and encoding to use)
• Contract (what operations are available)
Contracts
• Uses annotated .NET interfaces
Contracts
• Like in ASP.NET – only expose explict annotated operations
Bindings
• Many Bindings:
• Interopable• basicHttpBinding• webHttpBinding• wsHttpBinding, wsFederationHttpBinding
• WCF Specific• netTCPBinding• netPeerTcpBinding• netNamedPipeBinding• netMsmqBinding
• MSMQ interop• MsmqlIntegrationBinding
• Extensible (write your own)
WCF Bindings Matrix
Choose your Binding
• Before – you had to choose different middlewares• Now – just choose different bindings
Hosting Services
• Two models supported• Self-hosting: own process (console, winform)
• WAS hosting
• Windows Activation Service• Supported by IIS7• Emulated for IIS6 for HTTP/S
Self-hosting
• Make a ServiceHost, Add Endpoints, Call Open
Creating a Client
• Dynamic Proxies supported (like .NET Remoting)• Use ChannelFactory for this
Specify Endpoint in Config file
Behaviors
• Behaviors can be used to affect the internals of WCF• Eg. Transaction Flow
• Has nothing to do with the wire• Many build-in behaviors, but extensible for own • Attributes can be used [ServiceBehavior],
[OperationBehavior]• Configuration file defined behaviors• Code defined behaviors
Meta Data
• Two ways of sharing contracts• WSDL or Shared Contract DLL
Creating Static Proxies
• Unlike .NET Remoting, which only supports dynamic proxy generation there is also static gen. in WCF
• Or ”Add Service Reference” in VS
Defining the Contract
using System.ServiceModel;
//a WCF contract defined using an interface[ServiceContract]public interface IMath{ [OperationContract] int Add(int x, int y);}
using System.ServiceModel;
//a WCF contract defined using an interface[ServiceContract]public interface IMath{ [OperationContract] int Add(int x, int y);}
//the service class implements the interfacepublic class MathService : IMath{ public int Add(int x, int y) { return x + y; }}
//the service class implements the interfacepublic class MathService : IMath{ public int Add(int x, int y) { return x + y; }}
Implementing the Servicepublic class WCFServiceApp{ public void DefineEndpointProgrammable() { //create a service host for MathService ServiceHost sh = new ServiceHost(typeof(MathService));
//use the AddEndpoint helper method to //create the ServiceEndpoint and add it //to the ServiceDescription sh.AddServiceEndpoint( typeof(IMath), //contract type new WSHttpBinding(), //one of the built-in bindings "http://localhost/MathService/Ep1"); //the endpoint's address
//create and open the service runtime sh.Open();
}
public void DefineEndpointInConfig() { //create a service host for MathService ServiceHost sh = new ServiceHost (typeof(MathService));
//create and open the service runtime sh.Open();
}}
public class WCFServiceApp{ public void DefineEndpointProgrammable() { //create a service host for MathService ServiceHost sh = new ServiceHost(typeof(MathService));
//use the AddEndpoint helper method to //create the ServiceEndpoint and add it //to the ServiceDescription sh.AddServiceEndpoint( typeof(IMath), //contract type new WSHttpBinding(), //one of the built-in bindings "http://localhost/MathService/Ep1"); //the endpoint's address
//create and open the service runtime sh.Open();
}
public void DefineEndpointInConfig() { //create a service host for MathService ServiceHost sh = new ServiceHost (typeof(MathService));
//create and open the service runtime sh.Open();
}}
Create the Service EndpointProgrammatically
Create the Service EndpointProgrammatically
Create the Service Endpoint
Using a ConfigurationFile (see next slide)
Create the Service Endpoint
Using a ConfigurationFile (see next slide)
Configuration File
<!-- configuration file used by above code --><configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.serviceModel> <services> <!-- service element references the service type --> <service type="MathService"> <!-- endpoint element defines the ABC's of the endpoint --> <endpoint address="http://localhost/MathService/Ep1" binding="wsHttpBinding" contract="IMath"/> </service> </services> </system.serviceModel></configuration>
<!-- configuration file used by above code --><configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.serviceModel> <services> <!-- service element references the service type --> <service type="MathService"> <!-- endpoint element defines the ABC's of the endpoint --> <endpoint address="http://localhost/MathService/Ep1" binding="wsHttpBinding" contract="IMath"/> </service> </services> </system.serviceModel></configuration>
Implementing the Client Using Static Proxy
//this class is generated by svcutil.exe//from the service's metadata//generated config is not shown herepublic class MathProxy : IMath{ ...}
public class WCFClientApp{ public void SendMessageToEndpoint() { //this uses a proxy class that was //created by svcutil.exe from the service's metadata MathProxy proxy = new MathProxy();
int result = proxy.Add(35, 7);
proxy.Close(); }
//this class is generated by svcutil.exe//from the service's metadata//generated config is not shown herepublic class MathProxy : IMath{ ...}
public class WCFClientApp{ public void SendMessageToEndpoint() { //this uses a proxy class that was //created by svcutil.exe from the service's metadata MathProxy proxy = new MathProxy();
int result = proxy.Add(35, 7);
proxy.Close(); }
Implementing the Client Using Dynamic Proxy
public class WCFClientApp{
public void SendMessageToEndpointUsingChannel() { //this uses ChannelFactory to create the channel //you must specify the address, the binding and //the contract type (IMath) ChannelFactory<IMath> factory=new ChannelFactory<IMath>( new WSHttpBinding(), new EndpointAddress("http://localhost/MathService/Ep1")); IMath channel=factory.CreateChannel(); int result=channel.Add(35,7); factory.Close();
}
public class WCFClientApp{
public void SendMessageToEndpointUsingChannel() { //this uses ChannelFactory to create the channel //you must specify the address, the binding and //the contract type (IMath) ChannelFactory<IMath> factory=new ChannelFactory<IMath>( new WSHttpBinding(), new EndpointAddress("http://localhost/MathService/Ep1")); IMath channel=factory.CreateChannel(); int result=channel.Add(35,7); factory.Close();
}
Data transfer objects
• WCF support DTO’s• Use [DataContract]
decoration• All serializable objects
may be used instead -> less configurable
Plenum Discussion
• Use 5 minutes in your groups:• Differences with Web services / .NET Remoting
• Strength over Web services / .NET Remoting
• Weaknesses compared to –”-?
• Plenum: 5 minutes discussion of findings