Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / WPF

Writing a 'Hello World' program using wpf+wcf+wf step by step

Rate me:
Please Sign up or sign in to vote.
4.53/5 (29 votes)
17 May 2010CPOL3 min read 94.5K   2.5K   69   22
Writing a 'Hello World' program using wpf+wcf+wf step by step

Introduction

In my development,I use WF to do business logic ,and use wpf to do UI ,and use WCF to do communication.

Yesterday, I saw one post:what is the WCF,WPF,WF anybody can u give me the brief ? ,So I want to write an article to express my point of view.

A "hello world" program has become the traditional first program that many people learn. So I decide to write this 'Hello World' program with wcf+wpf+wf to express it .

About the Code

This example is very simple, just use the WPF, WCF, WF three new technologies. This example has three project:

WPFProject:A WPF Application ,It is used to implement the UI

WFProject:WF4 can be used to customize the business logic, this case I just use it to call the WCF service

WCFProject:A Console Application,It is used to otain data: ‘hello world’

The WPFProject start WFProject's workflow, WFProject call WCFProject 's WCF Service. Then WCFProject return 'Hello World' to the WFProject, Last WFProject retun 'Hello World' to the WPFProject's UI. Implementation of the order as shown below:

HW1.jpg

Figure 1

Implementation steps

Create a new WpfApplication, a newConsoleApplication, a workflow of ActivityLibrary, a total of three projects, named respectively: WPFProject, WCFProject, WFProject,as show below:

hwsou.jpg

Figure 2

WCFProject:

1、Because it is a console application, so I need to add System.ServiceModel.dll reference

hwref2.jpg

Figure 3


2、Add an interface IService1 for Contract

C#
[ServiceContract] 
public interface IService1 
{ 
[OperationContract] 
string GetData(); 
}      

3、Add a class Service1 to implement IService1:

C#
public class Service1 : IService1
{
    public string GetData()
    {
        return string.Format("Hello World");
    }
}

4、 Deploy App.config with Address and Binding for wcf service:

<system.serviceModel>
<services>
<service name="WCFProject.Service1" behaviorConfiguration="metadata"> <host>
<baseAddresses>
<add baseAddress="http://localhost:8001/Service1"/>
</baseAddresses>
</host>
<endpoint binding="basicHttpBinding" contract="WCFProject.IService1"/> </service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadata">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

5、Add the following code in the Program.cs to start wcf service :

C#
using (ServiceHost host = new ServiceHost(typeof(Service1)))
{
    host.Open();
    Console.WriteLine("The service is ready.");
    Console.WriteLine("Press <enter /> to terminate service.");
    Console.ReadLine();
}

WFProject:

If you are not familiar with WF, see this http://msdn.microsoft.com/library/dd851337.aspx

1、Drag and Drop a SendAndReceiveReply to Activity1,as show below.

hwworlflow1.jpg

Figure 4

2、Send:I use this Send Acitivy to call the WCF service.Please note.

a)、OperateName: GetData .(WCF Method)

b)、 EndPoint : EndPoint

c)、Binding : basichttpBingding.(WCF Binding)

d)、EndPointAddress:New Uri ("http://localhost:8001/Service1") (WCF Address)

e)、ServiceContractName :IService1.(WCF Contract)

3、ReceivReplyForSend: I use this ReceivReplyForSend Acitivy to obtain the return value of the WCF service

a)、Add an output parameter:returnValue.

hwpara.jpg

Figure 5

b)、Content as show below:

hw3.png

Figure 6

4、The entire workflow as show below:

hwwokflow2.jpg

Figure 7

WPFProject

In this Demo, this proect is very easy , the relationship between WPF and WF is the reference dll. WPFProject reference WFProject.dll.So

1、Reference WFProject.dll、System.Activities.dll in the WPFProject

hw5.png

Figure 8

hwref.jpg

Figure 9

2、In MainWindow Form drag a button, change the Content: "Invoke Workflow", in the click event add the following code:

C#
IDictionary<string, object> results = WorkflowInvoker.Invoke(new Activity1());
 MessageBox.Show(results["returnValue"].ToString());

Debugging

Start WCFProject, as shown below:

hw6.png

Figure 10

Start WPFProject. Click Invoke Workflow, the results below:

hw7.png

Figure 12

Points of Interest

This article use the most simple Hello World program teach you to use three kinds of the latest technology. I think this is a good way to development

WPF:UI presents(In client)

WF:Business logic (In server or In client )

WCF:Data communication (In server)

This is my first article on codeproject, my first language is not English.I hope you give me some advice to improve

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Kirin
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioninformative Pin
Meenakshi Kalyanasundaram18-May-16 4:41
Meenakshi Kalyanasundaram18-May-16 4:41 
QuestionUpdate for New Versions of VS? Pin
Victor Arnoldo Olvera11-Jul-15 4:30
Victor Arnoldo Olvera11-Jul-15 4:30 
Generalquestion Pin
Monica149-Mar-13 2:43
Monica149-Mar-13 2:43 
GeneralMy vote of 2 Pin
Sperneder Patrick31-Oct-12 3:22
professionalSperneder Patrick31-Oct-12 3:22 
GeneralMy vote of 4 Pin
Gorkemokur22-Sep-12 3:38
Gorkemokur22-Sep-12 3:38 
GeneralMy vote of 5 Pin
Farhan Ghumra27-Aug-12 2:04
professionalFarhan Ghumra27-Aug-12 2:04 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey23-Mar-12 0:49
professionalManoj Kumar Choubey23-Mar-12 0:49 
GeneralMy vote of 3 Pin
akhil khare30-Sep-11 3:10
akhil khare30-Sep-11 3:10 
GeneralMy vote of 3 Pin
ajith murali21-Sep-11 20:09
ajith murali21-Sep-11 20:09 
Generalgood article Pin
zooom_915-May-11 6:14
zooom_915-May-11 6:14 
GeneralRe: good article Pin
awadhendra tiwari22-Aug-11 1:54
awadhendra tiwari22-Aug-11 1:54 
GeneralI vote 1 Pin
Hafeez Rehman23-Mar-11 6:21
Hafeez Rehman23-Mar-11 6:21 
GeneralMy vote of 5 Pin
Sinto Antony12-Jan-11 19:30
Sinto Antony12-Jan-11 19:30 
GeneralMy vote of 5 Pin
chandru22210-Nov-10 20:38
chandru22210-Nov-10 20:38 
Generalplease let me know how to do in web application with sample code Pin
chandru22210-Nov-10 20:35
chandru22210-Nov-10 20:35 
GeneralAddressAccessDeniedException Pin
darora8528-Jul-10 3:47
darora8528-Jul-10 3:47 
GeneralHave a 4 Pin
SGnK25-May-10 1:50
SGnK25-May-10 1:50 
GeneralBest tutorial for beginners Pin
Jitendra Zaa17-May-10 21:01
Jitendra Zaa17-May-10 21:01 
GeneralGood, simple article Pin
UserWhoCares17-May-10 15:53
UserWhoCares17-May-10 15:53 
GeneralMy vote of 2 Pin
Not Active17-May-10 7:06
mentorNot Active17-May-10 7:06 
GeneralRe: My vote of 2 Pin
Tomas Brennan17-May-10 9:14
Tomas Brennan17-May-10 9:14 
GeneralRe: My vote of 2 Pin
Not Active17-May-10 10:13
mentorNot Active17-May-10 10:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.