stand on the shoulders of giants

WCF Quick Starter

翻译自:http://www.codeproject.com/KB/aspnet/WCF.aspx#Introduction

What is .NET 3.0?

.NET 3.0 = .NET 2.0 + Windows Communication Foundation + Windows Presentation Foundation + Windows Workflow Foundation + Windows Card Space.

What is WCF?

 a unification of .NET framework communication technologies, NET remoting; MSMQ; Web services; COM+.

What are the important principles of SOA

  • Boundaries are well defined

  以前的methodologies of communication,比如DCOM,边界模糊,就是说,服务端改变,客户端也要变。SOA不是这样,客户端只要知道end point, contract, and bindings. 服务端的改变不影响客户端。

  • Services evolve

  服务端改变,只要把新的service暴露为新的end point,原有的还能继续使用。客户端不受影响。

  • Services share only schemas and contracts

  Services use Schemas to represent data and contracts to understand behavior. XML is used to define schemas and contracts.

  • Service compatibility is policy based

  Web services are the base for SOA. 这说对了一半,web service缺少的正是service compatibility.
  web service只能serve with HTTP communication channel and SOAP message 而SOA要求我们的服务能够兼容不同的communication 技术: Remoting, DCOM, ASMX

What are ends ---  ABC of WCF

Address (Where)

Contract (What)

Contract is an agreement between two or more parties.  it describes parameters and return values for a method.

Binding (How)

Bindings determine how this end can be accessed.
例如你可以暴露你service, 是以SOAP over HTTP还是以BINARY over TCP的方式被访问

What are the main components of WCF?

• Service class.
• Hosting environment
• End point

Service class是包含[ServiceContract]Attribute的类,包含[OperationContract], [DataContract], [DataMember]
[ServiceContract],定义了这个类, 或者说这个application interface是被暴露的service;
[OperationContract], 顾名思义,是暴露给Client端的方法;
[DataContract], 是定义了服务端和客户端交换的数据,注意:应该是复杂的自定义类型;其实是被它定义的是define how data will be serialized will transmission.

动手

1. Host WCF in Console

[发现每次都会启动WcfSvcHost.exe,找了半天如何关闭,答案如此:
http://cedricboudoya.spaces.live.com/blog/cns!7F839BFE7AB33F87!297.entry
VS2008: To enable/disable the WCF Option running WcfSvcHost.exe, you had to add/remove the {3D9AD99F-2412-4246-B90B-4EAA41C64699} entry. [See more here]
VS2008+SP1: 在WCF project property页面可以控制]

  步骤:

  1. new a console app;
  2. add wcf library as reference to the console app; add System.ServiceModel reference
  3. Add a App.Config, fill it with config items in wcf library project;
    Notes: remove "<host>...</host>" in app.config
  4. Code

1 Uri baseAddress = new Uri("http://localhost:8732/WcfLibDemo/Service/");
2 
3 ServiceHost objServiceHost = new ServiceHost(typeof(Service1), baseAddress);
4 objServiceHost.Open();
5 Console.WriteLine("Service started...");
6 Console.ReadLine();

 

  Client app

  步骤:

  1. make proxy class "IService.CS" and "app.config"
    "svcutil  http://localhost:8732/WcfLibDemo/Service/"
  2. Add these files to client app;
  3. Code

Service1Client client = new Service1Client();

string val = client.GetData(1);

CompositeType ct 
= new CompositeType();
ct.BoolValue 
= true;
ct.StringValue 
= "OK";

CompositeType af 
= client.GetDataUsingDataContract(ct);

ct.BoolValue 
= false;
CompositeType bf 
= client.GetDataUsingDataContract(ct);

 

结果:
      val:  You entered: 1
af.StringValue:  OKSuffix 
bf.StringValue:  OK

2. Host a WCF service in IIS?

步骤:

  1. New WCF App;
  2. In IIS, New a virtual directory, publish the WCF app into the directory;
  3. Convert the directory to Application;

Then this WCF service is hosted in IIS: http://localhost/WCFApp/Service1.svc

Client App: svcutil http://localhost/WCFApp/Service1.svc 步骤和上面一样。

Host in IIS的好处:

  • Automatic activation
    service不用一直在运行,当message到来,service launch
  • Process recycling
    防止未知因素把service down掉,可以在IIS中设置,经过多少时间,recycle work process

different bindings

WsHttpBinding: - It is same like BasicHttpBinding. In short, it uses SOAP over HTTP. But with it also supports reliable message transfer, security and transaction. WS-Reliable Messaging, security with WS-Security, and transactions with WS-Atomic Transaction supports reliable message.
NetTcpBinding: - This binding sends binary-encoded SOAP, including support for reliable message transfer, security, and transactions, directly over TCP. The biggest disadvantage of NetTcpBinding is that both server and client should be also made in .NET language.
NetNamedPipesBinding:-Ths binding Sends binary-encoded SOAP over named pipes. This binding is only usable for WCF-to-WCF communication between processes on the same Windows-based machine.
NetMsmqBinding: - This binding sends binary-encoded SOAP over MSMQ. This binding can only be used for WCF-to-WCF communication.

posted @ 2010-02-08 17:01  DylanWind  阅读(240)  评论(0编辑  收藏  举报