您的位置:首页 > 其它

WCF 一步一步从入门到精通(一)建立一个简单入门的例子(适合一点不会的朋友阅读)

2009-03-30 08:28 609 查看
Windows Communication Foundation (WCF) 是一个统一框架,用于创建既安全可靠又可交互的分布式事务处理应用程序。

在我学习一样新的技术的时候总喜欢一开始就知道他如何使用和配置,然后再深入的了解的更多细节。下面我们就通过图片一步一步的从一点不会开始建立一个简单的WCF程序(这个例子取自微软,很简单):

我使用的环境是: VSTS2008 sp1

1.建立一个名为WCF的空解决方案,再右键点击他,为这个解决方案添加一个“WCF 服务库”项目





WCF服务库被添加进去,结果如下





2.WCF项目中有两个默认建立好的文件Iservice1.cs和Service1.cs ,我们下面要对这两个文件稍稍的做一下修改

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceLibrary
{
// 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(string value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// 任务: 在此处添加服务操作
}

// 使用下面示例中说明的数据协定将复合类型添加到服务操作
[DataContract]
public class CompositeType
{
bool boolValue = false;
string stringValue = "朋友 ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}



Service1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceLibrary
{
// 注意: 如果更改此处的类名“IService1”,也必须更新 App.config 中对“IService1”的引用。
public class Service1 : IService1
{
public string GetData(string value)
{
return string.Format("你输入的是: {0}", value);
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += " 你好!";
}
return composite;
}
}
}



这里定义了两个方法,分别用来演示,普通类型参数及返回值的调用,以及传递和返回复合类型的调用

3.之后,点击F5运行这个“WCF服务库”,VS会启动“WCF 调试客户端”调试WCF服务

我们可以在调试窗口右侧看到我们“WCF服务库”的一些基本信息,包括相应的配置文件信息,双击其中的方法,可以在右侧的窗口打开方法。输入相关参数,点击“调用”按钮可以在响应中得到返回的结果。





4.我们再为这个解决方案,添加一个Web应用程序,作为调用这个WCF服务的演示程序。

Web应用程序被添加进来,右键此项目,点击“添加服务引用”,





在弹出的窗口中点击“发现”,可以发现我们刚才定义的WCF服务,选中他后点击”确定”按钮





5.向Web应用程序页面拖进去4个控件,一个文本框,一个标签,两个按钮,按钮的名字分别为“调用”和“复合类型调用”





并为这两按钮添加单击事件,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

Label1.Text = client.GetData(TextBox1.Text);
}

protected void Button2_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

ServiceReference1.CompositeType paramCT = new WebApplication.ServiceReference1.CompositeType();
paramCT.BoolValue = true;
paramCT.StringValue = TextBox1.Text;

ServiceReference1.CompositeType returnCT = client.GetDataUsingDataContract(paramCT);
Label1.Text = returnCT.StringValue;
}
}
}



好了,现在设置Web应用程序为启动项,点击F5,可以测试程序的结果。结果和文件结构如下:

文本框输入内容,点击“调用”按钮,会在Label中显示返回的结果



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: