WCF Part 5: consuming the service

Source: Internet
Author: User

last time we generated the client and configuration file. whereas in the asmx world we had a proxy class, the WCF team renamed this in the June CTP to client. for us to use the generated files, we need a new console application and add the files. don't forget to add the system. servicemodel reference to the project. once this is done, it shoshould compile without errors.

We'll have to create an instance of the client. at that point, the application configuration on the client will be read. however, there won't be a connection until the first call has been made. after creating the instance, we can call the helloworld operation. notice that the hellocomputer operation isn't exposed, as we didn't apply the operationcontractattribute in the interface. besides the generated files, not much code is needed to execute the call.

 
1 Static VoidMain (String[] ARGs)
2{
 
3Console. Writeline ("Press any key when the service is available ...");
 
4Console. Readkey ();
 
6HelloclientClient =New Helloclient();
 
 
8StringMSG = client. helloworld ();
 
10Console. Writeline ("The message is: {0 }", MSG );
 
11Console. Writeline ("Press any key to quit ...");
 
12Console. Readkey ();
 
13}

On Line 6 the instance is created, on line 8 we call the service. line 10 shows the message in the console window. notice line 3 and 4. when we start (or execute) both assemblies at the same time, we need to be able to wait and allow the Service to get up and running.

There are two methods to execute the assemblies. you can right-click a project in Visual Studio 2005, choose debug and Start new instance . or you can right-click the solution, choose set startup projects... and select the radiobutton multiple startup projects . then specify 'start' for both projects. after hitting the F5 key, both projects will be started at the same time.

Consumer Configuration
You can examine the configuration of the consumer, in our case our console application that's calling the service. you'll see a lot of info on the basichttpbinding, but that's not really important right now. just stuff on timeouts, message encoding, etc. that's all default. what is (kind of) important however, is everything in the client element.

 
<Client>
<Endpoint Address="Http: // localhost: 8080/helloservice/"Binding="Basichttpbinding"
 
Bindingconfiguration="Basichttpbinding_ihello"Contract="Ihello"
Name="Basichttpbinding_ihello"/>
 
</Client>

Again you'll notice the wcf abc, address, binding and contract. All this has been imported by the service utility (svcutil.exe) and is now inside the client class as well.

We 've setup our first (have we? Running Service and client application and we're re talking in text/XML over HTTP with each other. we're re running this on the same computer, but you could place either one of these applications on the other side of the world and wouldn't notice a difference, but perhaps some delay because of the distance.

You can download the complete solution here, to see how I 've set everything up. before we take further steps into the world of Windows Communication Foundation and get beyond the wcf abc, we'll examine what we're 've done exactly.

Update: be sure to have the WCF/WF extensions installed ! You don't need the> 1 gb sdk installed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.