ASP.NET+WCF 应用

由于自己要写得论文需要,自己又开始学习.NET啦~

自己前段时间主要看了一下WCF比较基本的知识,了解下WCF服务从定义契约到之后的使用服务的大体流程,然后又翻翻ASP.NET的开发过程,看 看其中包含的控件,及控件的使用方法,及ADO.NET,学会有关数据库的操作,无非就是对表的增删查改,及存储过程,触发器等内容!

昨天这些工作刚刚完成,今天下午就把以前学过的内容拼拼凑凑,嘻嘻,就出来了一个ASP.NET+WCF的Demo了,在此和大家分享下,这个Demo的制作过程啊!

这个Demo的主要内容是实现用户登录,功能比较简单,主要是为了实现,ASP.NET与WCF服务的整合!(编程语言:C#)

1. WCF服务

第一步:创建一个类库(Class Library)工程, 根据自己的喜好取个名字, WebAppService(我的工程名);

第二步:定义契约,契约实际上就是加了服务特性的接口,新建一个类文件,IWebAppService.cs 代码如下:

using System;
using System.Collections.Generic;
using System.ServiceModel;// 这个程序集要记得引用
using System.Text;

namespace WebAppService
{

   
   [ServiceContract]//服务契约
    public interface IWebAppService
    {
        [OperationContract]//操作合约
        bool Login(string username, string password);
    }
}

从上面的代码看,就是一个普通的接口定义, 加上了服务特性([ServiceContract], [OperationContract]),其他的就没有什么特别的了!

第三步:实现契约,实现契约的类就叫做服务类型(ServiceType), 新建一个类文件, WebAppServiceType.cs。代码如下

using System;
using System.Collections.Generic;
using System.Text;

using System.Data.SqlClient;

namespace WebAppService
{
    public class WebAppServiceType : IWebAppService
    {
        public bool Login(string username, string password)
        {
             bool mark = false;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = "server=localhost; database=cx; uid=sa; pwd=sa";
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText="select count(*) from [user] where username='"+username+"' and password='"+password+"'";
            Conn.Open();
            int i = (int)Cmd.ExecuteScalar();
            if (i > 0)
                mark = true;
            Conn.Close();
            return mark;
        }
    }
}

一个简简单单的类文件~~~~

第四步:创建Web.config文件,也就是配置文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="WebAppService.WebAppServiceType" behaviorConfiguration="WebAppService">
                <endpoint address="wa" binding="basicHttpBinding" contract="WebAppService.IWebAppService"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WebAppService">
                    <serviceMetadata httpGetEnabled="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

第五步:创建WebAppSVC.svc文件,我是将WCF服务承载(HOST)在ISS中的,所以就需要这样的一个文件,代码如下:

<%@ ServiceHost Service="WebAppService.WebAppServiceType"%>

这个文件中只有一行代码,呵呵~

第六步:就是将这个工程部署在ISS中,我用的是ISS7,所以要要新建一个应用程序,下面的操作想必大家都很熟练了

验证这个服务是否部署成功

http://localhost/ISS应用程序名/WebAppSVC.svc 如何出现下面的页面即WCF服务部署成功了,哈哈

第七步: 使用VS命令窗口中,跳到工程目录上, 运行下述命令:

svcutil http://myron-pc/Service/WebAppSvc.svc?wsdl(与图片上的路径相同)

回车之后,会生成两个文件,一个是config文件,还有一个是类文件, 这两个文件都是有关WCF的描述,相应的服务接口(可以提供的功能)都会在类文件中详细描述, 在客户端程序中只要根据类文件的要求进行调用初始化实例,调用方法即可。

2. 创建Web项目

新建一个Web项目,将上面生成的两个文件copy到工程中,将config文件中的内容复制到Web项目中的Web.config中,然后创建一个Login.aspx中,后台处理代码如下:

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

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

        protected void LoginBtn_Click(object sender, EventArgs e)
        {
            using (WebAppServiceClient client = new WebAppServiceClient())
            {
                string username=this.username.Text.Trim();
                string password=this.password.Text.Trim();
                if (client.Login(username, password))
                {
                    Response.Redirect("Home.aspx");
                }
                else
                {
                    this.label.Text = "登录失败,请重试!";
                }
            }
        }
    }
}

OK, 在浏览器中打开Login.aspx页面,事先要创建好数据库,输入用户名和密码,能跳转到Home.aspx,说明调用成功了,哈哈,大功告成~~~

原文地址:https://www.cnblogs.com/myronxy/p/2078594.html