前言:
在项目当中用到了WCF,WCF是什么?为什么要使用它?小组分享时,只是了解了个大概,所以自己动手实现一个小Demo。
一、什么是WCF:
是由微软开发的一系列数据通信的应用程序框架。详情:WCF
二、WCF的用途:
在基于Asp.net的应用程序中开发中,我们由客户机的浏览器访问应用程序服务器,然后通过应用程序服务器中的数据库连接去连接数据库服务器,读取或是操作数据,有时候可能会多一个文件服务器。
三、创建一个客户端和服务端分离的WCF应用程序:
服务端创建:
1.新建WCF服务应用程序,可以看到有默认的两个文件,IService1.cs和Service1.svn。
2.将以上两个文件删除,添加我们自己的WCF服务,我这里建立了一个Test.svc服务,名称根据自己所需来命名,在解决方案中我们可以看见,当我们添加了一个Test.svc后,vs自动生成了一个接口ITest.cs。
3.在ITest.cs定义WCF方法ShowName,在Test.svc.cs对该接口的方法进行实现,代码如下
ITest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ITest”。
[ServiceContract]
public interface ITest
{
[OperationContract]
string ShowName(string name);
}
}
Test.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Test”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Test.svc 或 Test.svc.cs,然后开始调试。
public class Test : ITest
{
public string ShowName(string name)
{
string wcfName = string.Format("WCF服务,显示姓名:{0},name");
return wcfName;
}
}
}
在ITest.cs中有两个标签,[ServiceContract] 和 [OperationContract],这两个标签都是依赖System.ServiceModel的引用。
[ServiceContract]:来说明接口是一个WCF的接口,如果不加的话,将不能被外部调用。
[OperationContract]:来说明该方法是一个WCF的方法,如果不加的话,将不能被外部调用。
4.将Test.svc设为起始页,运行一下,出现如下图所示,我们就可以将这个服务器端发布,然后部署到IIS上,就可以供客户端使用,这里我就不部署了,用一个简单的办法就是直接预览Test.svc
5.直接预览Test.svc。
简述WCF应用场景,假如现在我有A服务器和B服务器,把刚刚建立的WCF程序部署在B服务器上(A,B服务器都在我自己的一台机器上),我们的目标是在A服务器的应用程序来访问B服务器的WCF程序,实现服务器端的应用程序通讯。
客户端创建:
1.创建Web应用程序。
2.右击引用添加服务引用,如下图所示,将刚刚我们在浏览器中的地址添加到这里,转到,确定。
3.添加完服务引用之后,我们在解决方案中会发现多了一个文件。
4.页面中的代码
Test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication1.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Button ID="btnSubmit" runat="server" Text="测试WCF服务" OnClick="btnClick" />
</form>
</body>
</html>
Test.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.ServiceReference1; //引用WebApplication1项目下的ServiceReference1
namespace WebApplication1
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnClick(object sender, EventArgs e)
{
TestClient test = new TestClient();
string result = test.ShowName(this.txtName.Text);
Response.Write(result);
}
}
}
注:
1.using WebApplication1.ServiceReference ;这个引用中WebApplication1.为我们客户端的命名空间。
2.TestClient test=new TestClient(); TestClient是我们在添加引用的时候生成的服务端Test类的客户端代理类,一般客户端的代理类名称都会是 ###Client,其中Test就是我们服务端添加的那个WCF服务(Test.svc)的名称。
总结
以上完成了一个简单的WCF的小Demo,刚进门,以后还需要不断的学习。期待后续