1.WebService服务创建和部署!
1.第一种方式:直接在网站的根目录WebUI里写
1.创建一个解决方案WebService_12(包含第1和第2种方法部署WebService)->新建网站WebUI(右键解决方案)
2.右键WebUI(添加新项)-->选Web服务(名字取为Ws_Hello,会自动创建一个服务和一个类)
3.右键该网站(选添加Web引用)-->(在弹出框中点)此解决方案中的Web服务-->点刚才新建的服务Ws_Hello
-->Web引用名改为Ws_Service1(这个相当Ws_Hello类的命名空间)
4.WebService创建完毕,测试:在Default.aspx页面的.cs文件的load事件里加下面代码,然后运行。
Ws_Hello ws_Hello = new Ws_Hello();
Response.Write(ws_Hello.HelloWorld());
注:HelloWorld()方法在在网站的App_Code的Ws_Hello.cs里(方法前面一定要加上[WebMethod]特性声明)
2.第二种方式:添加一个类库,在类库写,然后WebUI引用类库
1.右键解决方案(添加->新建网站(三层的时候添加的是类库),选asp.net Web服务(命名为Ws_Service2))
-->此服务下会自动创建一个服务和一个类(名为Service)-->把2个Service改为自己的名字Ws_ShowTime,
一个是Service.asmx,还有一个在app_code下Service.cs(包括文件里面的代码,总共6处)
2.把HelloWorld的方法改为下面的方法后保存-->然后在WebUI里添加WebUI引用,方法同上,如:
(右键该网站(选添加Web引用))->(在弹出框中点)此解决方案中的Web服务->点刚才改名的服务Ws_ShowTime
-->Web引用名填Ws_Service2(这个相当Ws_ShowTime类的命名空间)
[WebMethod]
public DateTime GetDate()
{
return DateTime.Now;
}
3.测试:在WebUI下的Default.aspx页面的.cs文件的load事件里继续添加代码,运行有时间显示就对了
Ws_Service2.Ws_ShowTime ws_Time = new Ws_Service2.Ws_ShowTime();
Response.Write(ws_ShowDate.GetDate().ToString());
3.第三种方式:专门建立一个WebService作为Web服务器
1.重新打开一个VisualStudio->新建解决方案WebService_3(右键选新建asp.netWeb服务(Ws_ProductsShow)
->把自动创建的服务名Service全部改成Ws_ProductsShow(和上面第二种方式的第1点里的一样)
->在Ws_ProductsShow.cs里写如下代码(实现一个简单的查询功能)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Ws_ProductsShow : System.Web.Services.WebService
{
public Ws_ProductsShow()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public DataSet GetAllProducts()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM [Products]";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
[WebMethod]
public DataSet GetProductsByCategoryId(int categoryId)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = string.Format("SELECT * FROM [Products] WHERE CategoryID ={0}",categoryId);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
[WebMethod]
public DataSet GetAllCategories()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM [Categories]";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
2.上面用到了3个命名空间,其中System.Configuration要在解决方案里添加(右键添加引用->.net
->System.Configuration),还有需要添加数据库,并且在Web.Config里配置好连接字符串
(配置好连接字符串简单方法:在WebUI下新建一个Default页面,在数据库里拖一个表进来,自动会在
web.config里设置好,然后把Default页面把刚才拖的表删除,这里把整个Default页面删除)
3.WebService创建完毕,测试:右键Ws_ProductsShow.aspx浏览,会看到自己定义的几个方法GetAllProducts()
每个都点击测试下,最后能够显示出xml文件表示正确。
4.iis部署,先把web.config里的Debug设置为false(这样速度会比较快),然后关闭解决方案进入iis发布,
发布了之后,浏览Ws_ProductsShow.asmx,同样可以测试所有的方法,如果都能打开到xml文件,表示正确,
记住一个路径,形如:http://localhost/temp2/Ws_ProductsShow.asmx
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
IIS发布
运行(mmc)->文件(选添/删除管理单元)->添加(选Internet信息服务,添加、关闭)->点开节点(右键默认网站属性,新建虚拟目录,别名随便(例如Temp),点浏览,找到要部署的站点,如C:\Documents and Settings\Administrator \桌面\TempDemo\WebUI,实际上选到用来保存页面的文件夹,WebUI文件夹下就是页面了 (Default_Repeater.aspx)) 5.部署完毕,测试:
右键前面建立的WebService_12的WebUI(添加Web引用),把刚才的路径复制到最上面的Url,点前往
-->把Web 引用名改为WebService_3->点添加引用,然后在WebUI的Default.aspx页面,拖一个DropDownList
和GridView,并写如下代码展示数据
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ws_Service2;
using Ws_Service3;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Ws_Hello ws_Hello = new Ws_Hello();
//Response.Write(ws_Hello.HelloWorld());
//Ws_ShowTime ws_Time = new Ws_ShowTime();
//Response.Write(ws_Time.ShowDate().ToString());
if (!Page.IsPostBack)
{
Ws_ProductsShow ws_service3 = new Ws_ProductsShow();
ddl_Category.DataSource = ws_service3.GetAllCategories();
ddl_Category.DataTextField = "CategoryName";
ddl_Category.DataValueField = "CategoryID";
ddl_Category.DataBind();
}
}
protected void ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
{
Ws_ProductsShow ws_service3 = new Ws_ProductsShow();
gv_Products.DataSource = ws_service3.GetProductsByCategoryId(Convert.ToInt32(ddl_Category.SelectedValue));
gv_Products.DataBind();
}
}
6.如果上面第4步发布iis的时候遇到错误,也可以直接在Ws_ProductsShow服务里新建一个Default页面来
测试代码(即把下面的代码复制到Ws_ProductsShow服务里的Default),如果测试成功就可以删除这个页面
(Ws_ProductsShow不应该有.aspx页面)
7.代码 下载