• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
WFC #1
 

WFC #1

on

  • 438 views

 

Statistics

Views

Total Views
438
Views on SlideShare
438
Embed Views
0

Actions

Likes
0
Downloads
22
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    WFC #1 WFC #1 Presentation Transcript

    • 1Introduction to WCF Services
    • Objectives Describe Windows Communication Foundation (WCF) and service-oriented development with WCF Explain the procedure to create and host a WCF service using Visual Studio 2008 Explain the procedure to consume a WCF service using Visual Studio 2008 Describe programmatic configuration of applications to host and call a WCF service Explain deployment of WCF services Introduction to WCF Services / Session 1 2
    • Introduction Service Oriented Architecture (SOA): Real World Example of SOA-based application  Is an approach to create enterprise applications  Integrates existing business components  Exposes the application to your business partners  Addresses and accommodates changes easily  Helps heterogeneous platforms and applications communicate with each other  Exposes the logic of an application as a service without letting others know about the actual code Introduction to WCF Services / Session 1 3
    • SOA In SOA:  The server machine or the server program hosts the code  The server machine or the server program exposes the code to the clients as a service  Any enterprise application is built using the three building blocks:  Code  Server Program  Client Program Introduction to WCF Services / Session 1 4
    • Windows Communication Foundation (WCF) WCF:  Is an important part of the Microsoft .NET Framework architecture  Is a SOA-based technology to create distributed enterprise applications  Includes features like multiple protocol support, reliability, and security  Offers interoperability with other services WCF key components include:  Service Class  WCF Runtime  Host Application Service class is the actual business logic exposed as a WCF service WCF runtime serves any class as a WCF service  The class should implement an interface with the ServiceContract attribute Host application hosts the WCF runtime Introduction to WCF Services / Session 1 5
    • WCF Architecture 1-3 The following figure shows the WCF architecture:  architecture application hosts a service client host includes: The host application hosts the WCF runtimeclass through multiple Theserviceapplication communicates with the server application endpoints host application A service through the proxy class   A This service class will be exposed as a WCF service client application Introduction to WCF Services / Session 1 6
    • WCF Architecture 2-3 Channels: the URL at which the server provides the service Address is Endpoint: A dispatcher component: Are a helper component client communicates with the service Specifies the following: Is processing units Is a combination of address, binding, and contract    Binding describes howservice class on the server the    Acts How componentson the server machine and takes the service request Commonlyhostclientasof WCF  as if to isreferred ABC Are inbuilt it a a application Contract on athecan beofthe server theprocess inputnext toservicemessage is   Information reply specified in to the channel or output message  Passes client and functionalitycode and configuration file describes the the server to provided by the it. The Exist The protocol to be used for communication passed through a series of channels, till it reaches the client Introduction to WCF Services / Session 1 7
    • WCF Architecture 3-3 The WCF runtime exposes any class as a WCF service class provided:  The class implements an interface with the ServiceContract attribute  The WCF runtime exposes a method declared in an interface only with the OperationContract attribute The client application talks to the service application through the proxy class  The client can choose to interact with the interface directly instead of using the proxy class  In the figure, the proxy class:  Forms a message on behalf of the client application  Passes the message on to the server Introduction to WCF Services / Session 1 8
    • Describing the WCF Service The WCF runtime:  Exposes and controls a class as the WCF service  Is represented in the System.ServiceModel namespace that resides inside the System.ServiceModel.dll library  Is hosted in the host process that acts as a server application This server application can be:  The EXE assemblies created using the console-based application, Windows-based application, and Windows service  IIS or Windows Activation Service The WCF service class hosted in an application other than IIS is called self-hosted WCF service Introduction to WCF Services / Session 1 9
    • Creating and Hosting a WCF Service using Visual Studio 2008 To create and host a WCF service in Visual Studio 2008, you need to create a:  Service interface  Service class  Server alias host application Introduction to WCF Services / Session 1 10
    • Creating a Service Interface 1-2 The service interface describes the functionality offered by the server: Syntax namespace <namespace Name> { [ServiceContract] public interface <interface Name> { [OperationContract] <return type><Function Name> (Parameters …); [OperationContract] <return type><Function Name> (Parameters …); . . . . } } Introduction to WCF Services / Session 1 11
    • Creating a Service Interface 2-2 The code shows a simple IHello service interface: Snippet namespace MyService { [ServiceContract] public interface IHello { [OperationContract] string SayHello(string inputMessage); } } In order to use the code, you need to import the System.ServiceModel namespace from System.ServiceModel.dll Introduction to WCF Services / Session 1 12
    • Creating a Service Class 1-2 A service class implements the functionality declared in the service interface: Syntax namespace <namespace Name> { public class<class Name>:<interface Name> { public <return type><Function Name> (Parameters) { // Actual functionality related code return <return value>; } public <return type><Function Name> (Parameters) { // Actual functionality related code return <return value>; } } } Introduction to WCF Services / Session 1 13
    • Creating a Service Class 2-2 In the code, the service class, HelloService, implements the IHelloService interface: Snippet namespace MyService { public class HelloService: IHello { public string SayHello(string inputMessage) { return "Hello " + inputMessage; } } } In order to use the code, you need to import the System.ServiceModel namespace from System.ServiceModel.dll Introduction to WCF Services / Session 1 14
    • Creating a Server or Host Application 1-7 After creating the service class, you need to create a host application You can host the WCF service class on:  IIS  Custom host application You need to specify the Address, Binding and Contract (ABC) information  Generally, ABC information is written in XML-based configuration files  If the service class is IIS hosted, then the host application uses web.config file  If the service class is self-hosted, then the host application uses app.config file The host application needs to know the following:  Which service interface functionality should be offered to the client?  Which protocol should be used?  What is the information regarding input/output parameters?  How to start listening requests from the client applications? Introduction to WCF Services / Session 1 15
    • Creating a Server or Host Application 2-7 <?xml version="1.0" encoding="utf-8" ?>Syntax  The syntax for the configuration file is as follows: <configuration> <system.serviceModel> <services> <service name="<Fully Qualified Name of the Service Class>"> <endpoint address="<URI where the service needs to accessed>" binding="<way how service needs to be accessed>" contract="<Fully Qualified Name of the Interface – defines what is offered from the service>"/> <other endpoints> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> Introduction to WCF Services / Session 1 16
    • Creating a Server or Host Application 3-7Snippet <service name="MyService.HelloService"> <?xml version="1.0"?> informs the WCF runtime that the  The following code showsasaa sample code class needsthe <configuration> <system.serviceModel> MyService.HelloService exposed WCF service inside to be app.config file <services> <service name="MyService.HelloService">  To use the code, you need to reference <endpoint address="http://localhost:9898/HelloService" binding="basicHttpBinding" System.ServiceModel.dll contract="MyService.IHello"/> </service> binding="basicHttpBinding" address=http://localhost:9898/ </services> informs the WCF runtime that the HTTP HelloService informs the WCF runtime contract="MyService.IHello" communicate and ask be used that the client will no security needs to for protocol with for communication informs the WCF runtime that only the the port number 9898. <behaviors> HelloService at <serviceBehaviors> MyService.IHello kind of <behavior> functionality needs to be exposed from <serviceMetadata httpGetEnabled="true"/> the MyService.HelloService </behavior> class </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> Introduction to WCF Services / Session 1 17
    • Creating a Server or Host Application 4-7 Consider a console application as a server application  This means that you are hosting the WCF service on your own server, that is, the console application  This refers to self-hosting Consider that this server listens to the client requests for the HelloService WCF service Introduction to WCF Services / Session 1 18
    • Creating a Server or Host Application 5-7 The code shows how the server listens to the client requests Note that this code should be written inside the main method of the server Snippet ServiceHost host = new ServiceHost(typeof(MyService.HelloService)); host.Open(); In order to use the code, you need to import the System.ServiceModel namespace from System.ServiceModel.dll In the code, a call to Open() method creates and opens the listener for the service Introduction to WCF Services / Session 1 19
    • Creating a Server or Host Application 6-7 In the IIS-hosted WCF service, the ServiceHost object used in the previous code is replaced with a file that has the extension .SVC A directive called @ServiceHost represents the entire code The syntax to use the @ServiceHost directive in the .SVC file is as follows: Syntax <%@ ServiceHost Language=”C#” Service=”<name of service class which implements functionality >" CodeBehind="<File which contains service class>" %> Introduction to WCF Services / Session 1 20
    • Creating a Server or Host Application 7-7 The code shows a @ServiceHost directive in the HelloService.SVC file while you host MyService.HelloService To use the code, you need to add the reference to System.ServiceModel.dll Snippet <%@ ServiceHost Language="C#" Debug="true" Service="MyService.HelloService" CodeBehind="HelloService.svc.cs" %> The Service property informs the WCF runtime that when a call is given to the HelloService.SVC file, it should create an object of the MyService.HelloService class and serve it to the client The CodeBehind property informs the WCF runtime about the file in which the MyService.HelloService class is defined Introduction to WCF Services / Session 1 21
    • Consuming the WCF service using Visual Studio 2008 You can call the WCF service functionality hosted on the server using:  The service interface in combination with a mediator  The mediator communicates on behalf of the client with the service that is represented by a class called ChannelFactory  The Service Reference option in Visual Studio  The SVCUTIL.EXE utility that ships with the .NET framework When you use the SVCUTIL.EXE utility or the Service Reference option, a proxy class is created To share only the interface library with the client, you can use the ChannelFactory class Introduction to WCF Services / Session 1 22
    • ChannelFactory 1-5 To access a WCF service hosted on the server by using ChannelFactory:  In the client application, add a reference to the service interface library  Add app.config configuration file in the client application Introduction to WCF Services / Session 1 23
    • ChannelFactory 2-5 The syntax adds the client-side app.config configuration file: Syntax <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint address="<URI where the service needs to accessed>" binding="<way how service needs to be accessed>" contract="<Fully Qualified Name of the Interface – defines what is offered from the service>" name="<Name_For_the_Configuration>"/> </client> </system.serviceModel> </configuration> Introduction to WCF Services / Session 1 24
    • ChannelFactory 3-5 The code shows the code in the app.config configuration file for the HelloService WCF service: Snippet <?xml version="1.0"?> <configuration> <system.serviceModel> <client> <endpoint address="http://localhost:9898/HelloService" binding="basicHttpBinding" contract="MyService.IHello" name="MyEndPoint"/> </client> </system.serviceModel> </configuration> Introduction to WCF Services / Session 1 25
    • ChannelFactory 4-5 The syntax to use the service interface and create an object is as follows: Syntax <Interfacereference>= new ChannelFactory<Interface Type>("<Name_For_the_Configuration>").CreateChannel(); Introduction to WCF Services / Session 1 26
    • ChannelFactory 5-5 The code shows how to use ChannelFactory to call the HelloService WCF service You need to import the System.ServiceModel namespace from System.ServiceModel.dll Snippet IHello obj = new ChannelFactory<IHello>(" MyEndPoint").CreateChannel(); Console.WriteLine(obj.SayHello("WCF")); Call the CreateChannel() method Introduction to WCF Services / Session 1 27
    • The Service Reference Option To access the WCF service hosted on the server by using the Service Reference option in Visual Studio, follow these steps: 1. Ensure that the server application is running 2. In the client application, in the Solution Explorer window, right- click References and click Add Service Reference. The Add Service Reference window is displayed 3. To call the service, in the Address text box, type the URL 4. To hold the proxy class, in the Namespace textbox, type the namespace name 5. To add the proxy, click OK 6. Create a proxy class object and call the Web method Introduction to WCF Services / Session 1 28
    • Using the SVCUTIL.EXE Utility 1-2 To create a proxy class and access the WCF service using the SVCUTIL.EXE utility, perform the following steps: 1. Ensure the server application is running 2. Click Start Programs Microsoft Visual Studio 2008 Visual Studio Tools Visual Studio 2008 Command Prompt 3. Use the following syntax to use the SVCUTIL.EXE utility Syntax SVCUTIL /Config:App.Config /Out:<Proxy Class File Name> “<type the URL with which the service can be called >” The code shows how to use the SVCUTIL command to generate the proxy class and the configuration file for HelloService WCF service Snippet SVCUTIL /Config:App.Config /Out:proxy.cs “http://localhost:9898/HelloService” Introduction to WCF Services / Session 1 29
    • Using the SVCUTIL.EXE Utility 2-24. Press Enter. This generates two files, the proxy class file and the app.config configuration file5. Add the generated files, the proxy class file and the app.config file to the client application6. Call the WCF service using the proxy class object (generated in Step 4) Introduction to WCF Services / Session 1 30
    • Deploying a WCF Service 1-2 The following figure displays different types of host applications host a WCF service:  IIS 6.0 only supports HTTP protocol  IIS 7.0 supports TCP and HTTP protocols  The service hosted using the following is called the self-hosted WCF service:  Windows application  Console application  Windows service Introduction to WCF Services / Session 1 31
    • Deploying a WCF Service 2-2 The table shows various hosting environments and the protocols that each environment supports: Hosting Environment Supported ProtocolsWindows or Console application HTTP, net.tcp, net.pipe, net.msmqWindows service application HTTP, net.tcp, net.pipe, net.msmqWeb Server IIS6 HTTPWeb Server IIS7 - Windows Process HTTP, net.tcp, net.pipe,Activation Service (WAS) net.msmq Introduction to WCF Services / Session 1 32
    • Summary WCF is a SOA-based technology from Microsoft for distributed enterprise application development A WCF service class implements an interface with the [ServiceContract] attribute and exposes the methods with the [OperationContract] attribute The server application hosts a WCF service class and offers the functionality of the class as a service to the clients WCF services can be self-hosted or IIS hosted A client determines the functionality offered by the server using the interface or the proxy class A client requests the functionality from a WCF service using the ChannelFactory class, the proxy class, or the SVCUTIL.EXE utility The client and server configuration can be written either in code or in configuration files Introduction to WCF Services / Session 1 33