wcf网址

http://www.geekzone.co.nz/vs2008/4653

 

With Windows Communication Foundation (WCF), now developers can create interoperable services that send and receive messages between server and client. WCF is based on message based communication and supports many Communication Protocols like TCP, MSMQ, and HTTP etc.

This article discusses some of the new features that Visual Studio 2008 provides us for WCF. This article assumes that the reader has a basic knowledge on how WCF Services are built and used.

You can find more on how to get started with WCF here.

Multi-targeting
Visual Studio 2008 itself has many good and notable features like Refactoring, WPF support etc. and one such feature is also Multi-targeting. With Visual Studio 2008 you can build projects against .NET Framework 2.0 or 3.0 or 3.5.

Yes, one IDE for all the three framework versions! Gone are the days where you had to keep the earlier version of Visual Studio for the earlier .NET versions. Now you can use your Visual Studio 2005 projects which were targeted towards .NET 2.0 versions in Visual Studio 2008.



This means that you can also target your WCF Service to any of these versions. You can also change the target version later by going into the Project Properties page.

Default WCF Project Templates
Unlike in Visual Studio 2005, Visual Studio 2008 now has WCF Project Templates installed by default.



There is a separate section called WCF which also lists Windows Workflow Foundation templates along with WCF Service Library template.

And there is also a separate WCF Service Application template which can be used for creating Services that are hosted in web space.



Once you choose a template, the necessary project files are created for you.

For this blog post, I am using WCF Service Application and going to host it in IIS 7.

SimpleCalculator – Our WCF Example
We are going to create a simple calculator service which can

1) Add two numbers
2) Subtract two numbers
3) Multiply two numbers, and,
4) Return a set of numbers

So, we have two projects, mainly

1) SimpleCalculatorService – WCF Service
2) SimpleCalculatorClient – WCF Client for the above SimpleCalculatorService

Here is the class diagram for our Service



And for our Client



WcfTestClient
With Visual Studio 2008, we have a new tool called WcfTestClient which is a Windows application which can be used to test our WCF Services. We could use this to invoke service operations and check whether they are working properly or not. It is similar to how we test our ASMX Web Service applications, but here it is a separate Windows Application.

Open up Visual Studio command prompt. It can be found in your Visual Studio 2008 Tools folder in the Start menu



You can now test our CalculatorService by giving the WSDL address to WcfTestClient: http://localhost:7476/CalculatorService.svc?wsdl

The WcfTestClient then adds the service and loads all the operations available.



To test the operations, you can just double click on each operation and then supply the parameters. Here is for the Add operation:



It’s now easy to see whether your Service works or not before actually starting to write a Client.

WCF Services are Self Hosted now
With Visual Studio 2008 now you can actually debug a WCF Service and that is possible because they are self hosted when you start debugging (when you hit F5).

The WcfSvcHost.exe does the trick for you. If it’s a WCF Service Library, then it’s hosted via WcfSvcHost.exe and if it’s WCF Service Application, it’s hosted in the local ASP.NET Development Server and that’s how we got http://localhost:7476/CalculatorService.svc?wsdl working.

Adding Service Reference
Let’s start building our Client. Our Client is a console application. Visual Studio 2008 provides you an improved Add Service Reference option when compared with what was available in Visual Studio 2005.



Now with the Add Service Reference wizard’s Advanced Options, you can also generate asynchronous operations for your Service!



Adding the Reference generates a new folder called Service References in your project where the Service resides.



To configure/update our Service Reference later, we could right click on our service and say Update or Configure. I find this feature to be more stable than in Visual Studio 2005.



Our SimpleCalculatorClient also uses asynchronous calls to our SimpleCalculatorService to add and here is the code for that



In our Service, we have an operation which could return the numbers, the client requested from a start number to end number. The signature of that method in our Service is

[OperationContract]
List[int] GetNumbers(int from, int to);

And how has the Client interpreted it?



If a WCF Service returns IEnumerable[T] or IList[T] or ICollection[T], the proxy by default converts them to array type and thus we got int[] in place of List[int].

Can we change this behavior? Yes, you can. In your Add Service Reference Wizard’s Advanced Options, there is an option which lets you specify this behavior.



Deploying our Service
With IIS 7 deploying our WCF Service is simple thanks to WAS (Windows Process Activation Services)! Read here on how to configure IIS 7 for WCF.

Open up your IIS Manager and traverse to your Default Website and Add Application



And then add an Alias to our Service and give the path where we have our Service files



And now we are ready and deployed our SimpleCalculatorService! You can check in your browser by typing
http:///SimpleCalculatorService/CalculatorService.svc

You can also add the CalculatorService.svc as your Default Document, and now with Visual Studio 2008 and IIS 7 you can do it in your web.config:




And now you can just type http:///SimpleCalculatorService

You can download the sample SimpleCalculatorService and SimpleCalculatorClient application here.

Download Visual Studio 2008 90 day trial
For detailed information and to request a free 90-day trial DVD of Visual Studio 2008 Team Suite to be sent out to you, go to the Microsoft Visual Studio webpage.

About the Author
Chakkaradeep (a.k.a Chaks among his friends) is currently doing his Masters at University Of Otago, Dunedin. He is also the current Microsoft Student Partner for University of Otago. He writes two blogs: Chaks' Corner and Chaks' Geekzone Corner.

Other related posts:
Writing your own Html Helpers for the ASP.NET MVC Framework
Automating Visual Studio 2008
Windows Azure Table Storage - Getting Started - Part 2

posted @ 2010-12-14 00:09  庚武  Views(273)  Comments(0Edit  收藏  举报