[WF4.0 实战] WPF + WCF + WF 打造Hello World(基础篇)


本篇博客是一个基础的演示样例,也就是一个新手教程吧!让大家熟悉一下WPF + WCF + WF三者的关系!这仅仅是一个基础篇,下篇会继续深入,作为这段时间研究工作流的一个小小总结!

 

三者关系:

 

WPF--client--View

WF--流转--Controller

WCF--对外提供服务的--Model

 

以下是一个演示样例,用这个演示样例再来理解一下为什么我将三者的关系分别映射为MVC的各层

 

WCF:

 

新建一个“控制台应用程序”,然后建立接口和对应的实现类

                

实现对应的方法:

 

接口:接口和方法须要对外公开,加入对应的引用ServiceModel

namespace MyHelloWorld
{
  [ServiceContract]
   public interface IAppServer
    {
       [OperationContract]
       string GetStringFromWCF();
    }
}

实现类:

namespace MyHelloWorld
{
   public class AppServer:IAppServer
    {
        #region IAppServer成员
           public string GetStringFromWCF() {
               return "Hello World";
           }
        #endregion
    }
}

编写宿主程序:

class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(AppServer)))
            {
                host.Open();
                Console.WriteLine("服务已经启动");
                Console.ReadLine();
            }

        }
    }

引入对应的App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="MyHelloWorld.AppServer">
                <endpoint address="" binding="basicHttpBinding" contract="MyHelloWorld.IAppServer">
                </endpoint>
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8080/MyHelloWorld/AppServer/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

启动服务測试:


         


此时我们能够查看对外公开的服务以及它的描写叙述文件:


     

 


WF:


新建工作流的“活动库”(WFProject


              

    


send设置为參数类型


              


send相当于请求发送过去了,即请求服务的方法,那么就应该有个响应,而响应的结果就应该赋给我们之前定义的那个输出參数(returnView

而返回的结果名称规则为调用的服务端的名称+result后缀


     


WPF:

 

新建WPF应用程序(WPFProject


             

加入对应的引用usingSystem.Activities;以及WFProject

namespace WPFProject
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            IDictionary<string, object> results = WorkflowInvoker.Invoke(new WFProject.Activity1());
            string result = results["returnView"].ToString();
            MessageBox.Show(result);
            
        }
    }
}

设置为多启动项


             


终于的执行结果为:


       


附上整个解决方法的说明:


               

好了,整个程序已经完毕了,这是一个很基础的演示样例,目的是让大家对WPF,WF,WCF有一个简单的理解。

WPF--WebForm

WF---简单的逻辑调用B

WCF--方法的实现D

这篇博客还没有真正的应用上这个WF的流程自己主动控制过程,仅仅是起了一个简单的调用的作用,下篇将细化WF的使用!





原文地址:https://www.cnblogs.com/mfrbuaa/p/4197226.html