Huong Dan Thuc Hanh Wcf Part 1 6945

download Huong Dan Thuc Hanh Wcf Part 1 6945

of 21

Transcript of Huong Dan Thuc Hanh Wcf Part 1 6945

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    1/21

    Hng dn thc hnh WCF Part 1

    1Lu

    Trong phn 1: chng ti scha i su vbo mt, khnng chu ti, cng nh cc chun truyn thng c bit nh

    tcpBinding, MSMQ, chi tit lp trnh WCF vi WF.. Trong phn 2, chng ti scp su hn.

    2

    To project WCF Service Library

    Figure 1: Chn WCF Service Library project template

    Figure 2: Chy thphng thc Add ca MathServer

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    2/21

    Figure 3: Trong mt ng dng, c ththm nhiu WCF Service

    3To ng dng WCF Console khng dng Project Template

    3.1 Khng dng nh ngha dch vWCF trong app.config

    Xem v d: 02_ConsoleWCF

    Bc 1: To Console Application

    Bc 2: Thm reference ti Service.Model

    Figure 4: Add Reference System.ServiceModel

    Bc 3: Khai bo usingSystem.ServiceModel;

    Bc 4: To Service Contract

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    3/21

    Bc 5: nh ngha Operation Contract

    Bc 6: Vit hm thc hin operation contract

    Bc 7: Vit code nh ngha cu hnh v khi to servicehost

    Bc 8: Chy th

    3.2 Khai bo cu hnh dch vWCF bng file app.config

    Bc 9: Thay bng nh ngha cu hnh trong code bng khai bo trong file XML app.config

    Cu hi: Trong mt ng dng c thc khai bo nhiu th c khng?

    Trong mt th c thkhai bo nhiu th c khng?

    Th khi no cn v khi no khng cn?

    3.3

    Thkt ni bng WCF Test Client

    Chy WCF Test Client thmc. ng dn file ny l:

    C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\WcfTestClient.exe"

    Figure 5: Li pht sinh v khng ly c Metadata tdch v.

    Cu hi: Ti sao bli? Gii thch nguyn nhn

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    4/21

    4Thm MEX endpoint cho client tm hiu dch v

    Xem v d02_ConsoleWCFWithConfig

    4.1 Thm MEX endpoint bng code

    Bc 1: Thm dng khai bo usingSystem.ServiceModel.Description;

    Bc 2: Thm on lnh sau y vo App.cs

    ServiceMetadataBehaviorbehavior = newServiceMetadataBehavior();behavior.HttpGetEnabled = true;serviceHost.Description.Behaviors.Add(behavior);serviceHost.AddServiceEndpoint(typeof(IMetadataExchange),MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

    4.2 Thm MEX endpoint bng khai bo trong app.config

    Thay ni dung app.config bng ni dung ca app-mex.config

    Cu hi: gii thch ngha ca:

    behaviorConfiguration

    5To client sdng dch vWCF

    Xem v d02_WPFClient

    Bc 1: To Windows Form nh hnh di

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    5/21

    Bc 2: Add Service Reference

    Bc 3: Vit on code gi dch vthng qua lp proxy c tto ra sau bc Add Service Reference

    privatevoidbtnAdd_Click(objectsender, EventArgs e){

    try //Thi quen tt, nn dng try catchtrc nhng li gi dch vbt li{MathService.MathClient mathClient = newMathService.MathClient();intresult = mathClient.Add(int.Parse(txtA.Text), int.Parse(txtB.Text));txtResult.Text = result.ToString();

    }catch(Exception exception){

    MessageBox.Show(exception.Message, "Exception", MessageBoxButtons.OK,MessageBoxIcon.Warning);

    }}

    6 Gi mt hmmt chiu (one way operation)

    Xem v d03_OneWay\ Oneway.sln

    Bc 1: Chy server 03_OneWay\OneWay.sln

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    6/21

    Bc 2: Chy client 03_OneWay\ClientTest.sln

    Bc 3: Lin tcn vi ln nt Call OneWay operation

    Bc 4: So snh vi lin tcn vi ln nt Call Non OneWay operation ri cho nhn xt.

    Cu hi: Dng dch vmt chiu khi no?

    Trli: Dng dch vmt chiu khi client gi mt hm khng gi trtrv(void) ca service. Ngay sau li gi, client cthtip tc chy lnh tip ngay sau khi li gi dch vc gi i. C ngha khi hm dch vmy chchy chm.

    7Gi hm db(call asynchronous operation)

    Xem v d: 04_Asynchronous\AsynchronousCall.sln

    Bc 1: To AsyncMathServer vi cc hm sau:

    publicclassMathServer: IMathServer{

    publicintAddSlow(intA, intB){

    Thread.Sleep(4000); //Lm cho hm tr gi lp mt tc v chy tn thi gianreturnA + B;

    }

    publicintAddFast(intA, intB){

    //Thread.Sleep(4000);returnA + B;

    }}

    Bc 2: To Winform client , ri Add Service Reference n AsyncMathServer.

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    7/21

    Figure 6 :n nt Advanced... sa i thng sca tham chiu n dch v

    Figure 7 : Chn check box Generate asynchronous operations

    Bc 3: nh ngha event handler cho skin hm c gi db(Asynchronous call) hon tt cng vic. Lu n phm

    Tab tng in m.

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    8/21

    Bc 4: chy thchng trnh.

    Khin nt Add Sync, nu trong hm tnh ton pha dch vchy (y l rt lu) th ngi phi chcho n khi

    hm tnh ton hon tt.

    Khin nt Add Async, trong lc hmtnh ton pha dch vchy, ngi dng vn c thtng tc vi control

    trn giao din, v d: ko thanh trt slider.

    Figure 8 : Hot ng client bblock (ngng tr) nu hm ng bchy mt nhiu thi gian

    Cu hi:

    8Gi hm Duplex

    9

    Hosting mt ng dng WCF bng IIS 7

    Bc 1: Mv d01_WCFServiceLibrary\ 01_WCFServiceLibrary.sln

    Bc 2: Chn kt xut sau bin dch l Release

    Bc 3: Publish ra mt th mc m IIS Application strn. Menu: Build > Publish

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    9/21

    Bc 4: To Virtual Application trong Default web site hoc to mt web site mi port mi.

    Figure 9 : t Alias v trn th mc vt l cha m chy ca WCF Service

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    10/21

    Bc 5: Trong th mc c publish m chy dch vWCF, to cc file *.svc trn tng service c nh ngha

    trong file web.config. Trong v dny, chng ta c 2 dch v:

    _01_WCFServiceLibrary.Service1

    _01_WCFServiceLibrary.MathService

    Ni dung file Service1.svc l

    Ni dung file Math.svc l

    Bc 6: Sdng web browser kim tra dch v

    http://localhost/MyService/service1.svc (xem tng quan)

    http://localhost/MyService/service1.svc?wsdl (xem chi tit nh ngha ni dung XML ca dch v)

    Figure 10 : Kim tra dch vbng web browser

    Bc 7: hoc dng WCF Test Client kim tra

    http://localhost/MyService/service1.svchttp://localhost/MyService/service1.svchttp://localhost/MyService/service1.svc?wsdlhttp://localhost/MyService/service1.svc?wsdlhttp://localhost/MyService/service1.svc?wsdlhttp://localhost/MyService/service1.svc
  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    11/21

    Lu :Khi host trn IIS 7 thng tin baseAddress khng cn cn thit na

    10SilverLight 2.0 gi WCF basicHttpBinding

    Xem v d06_SilverLight\ListCustomer\ListCustomer.sln

    Bc 1: To mt solution SilverLight Application c cha website ASP.net host SilverLight control test th.

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    12/21

    Bc 2: To kt ni n database Northwind trong CSDL SQL 2005.

    Bc 3: Thm mi LINQ to SQL Classess. t tn l NWCustomer.dbml

    Bc 4: TServer Explorer, ko bng Customer vo khung thit kca NWCustomer.dbml .

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    13/21

    Bc 5: Tip , thm mi mt WCF Service vo dn. t tn l Northwind.svc

    Bc 6: Do SilverLight 2.0 chsdng c dch vweb service c bn nn ta sdng basicHttpBinding m t

    endpoint dch vWCF ny.Mfile app.config vo phn sa.

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    14/21

    Bc 7: To Service Contract v Operation Contract. v dny: chcn mt hm getCustomerByCity. Tham svo l

    city, trvList. Kiu dliu Customer c tng nh ngha khi to LINQ to SQL bc 3.

    [ServiceContract]publicinterfaceINorthwind{

    [OperationContract]List getCustomerByCity(stringcity);

    }

    Bc 8: Vit m thhin cho Operation Contract: Chut phi menu, chn Implement Interface

    Bc 9: Vit cu lnh LINQ trv List

    publicclassNorthwind: INorthwind{

    publicList getCustomerByCity(stringcity){

    NWCustomerDataContext db = newNWCustomerDataContext();varresult = fromcustomer indb.Customers wherecustomer.City.StartsWith(city) select

    customer;returnresult.ToList();

    }}

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    15/21

    Bc 10: Chy thweb service, bng cch chn Northwind.svc lm start page. Sau chy WCF Test Client kim tra

    hm getCustomerByCity. G thgi trLondon, rin nt Invoke v chkt qutrvResponse.

    Nu thnh cng th vo project to SilverLight, y l SilverLight WCF thm tham chiu n dch vWCF va to.

    Lu chn cch gi Asynchronous call.

    Bc 11: Thit kgiao din SilverLight bng Expression Blend

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    16/21

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    17/21

    voidclient_getCustomerByCityCompleted(objectsender,ListCustomer.NorthwindServiceReference.getCustomerByCityCompletedEventArgs e){

    Result.ItemsSource = e.Result;}

    Gadget gi WCF trvJSON

    11ADO.NET DataService REST (thm)

    Cu hi: REST l g?

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    18/21

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    19/21

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    20/21

    i t:namespaceNorthwindDataService{

    publicclassNorthwindDataService: DataService< /* TODO: put your data source class name here */>{

    // This method is called only once to initialize service-wide policies.publicstaticvoidInitializeService(IDataServiceConfigurationconfig){

    // TODO: set rules to indicate which entity sets and service operations are visible, updatable,etc.

    // Examples:// config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

    }}

    }

    Thnh:

    namespaceNorthwindDataService{

    publicclassNorthwindDataService: DataService{

    // This method is called only once to initialize service-wide policies.publicstaticvoidInitializeService(IDataServiceConfigurationconfig){

    config.SetEntitySetAccessRule("*", EntitySetRights.All);}

    }}

    Chy th, sthy khng c hm no tdch v:

    y ADO.NET Data Service sdng mt kiu binding c bit ca WCF, khng a ra cc hm gi m lp trnh vin c

    thty thm, sa. REST chchp nhn mt sthao tc dliu chun: Create, Read, Update, Delete (CRUD).

  • 8/10/2019 Huong Dan Thuc Hanh Wcf Part 1 6945

    21/21