现在开始学习Web服务,写下自己学习过程中的一些感受,拿出来与大家分享,欢迎批评指正。希望能给跟我一样初学Web服务的朋友一些帮助。
文章在我小站的地址:Web Service学习笔记:创建一个示例和WebMethod特性解析
Web服务的宗旨是创建不需要用户界面就能与其他应用程序交互的Web应用程序。它是松耦合的,并与服务器端和客户端所使用的操作系统、编程语言都无关,这样就在一定的情况下就会使你的效率大大提高。但创建Web服务必须保证的是,服务器端和客户端都要支持行业标准协议HTTP、SOAP(simple object access protocal 简单对象访问协议)和XML。
创建Web服务的过程:
(1):打开VS,创建一个ASP.NET WEB服务应用程序;
(2):输入下列代码,这是一个有关学生信息的二维数组和几个方法
string [,] strStuInfo = {
{ " 200511020120 " , " 贰零零五届 " , " 人文法律学院 " , " 社会工作 " , " (1)班 " , " 无悔 " , " 1 " },
{ " 200511020121 " , " 贰零零五届 " , " 人文法律学院 " , " 社会工作 " , " (1)班 " , " 梁需 " , " 2 " },
{ " 200511010122 " , " 贰零零五届 " , " 人文法律学院 " , " 法律专业 " , " (1)班 " , " 陆磊 " , " 3 " },
{ " 200511010220 " , " 贰零零五届 " , " 人文法律学院 " , " 法律专业 " , " (2)班 " , " 白灵 " , " 4 " },
{ " 200511010221 " , " 贰零零五届 " , " 人文法律学院 " , " 法律专业 " , " (2)班 " , " 剑付 " , " 5 " },
{ " 200511020222 " , " 贰零零五届 " , " 人文法律学院 " , " 社会工作 " , " (2)班 " , " 敬意 " , " 6 " },
{ " 200511100120 " , " 贰零零五届 " , " 化工生物学院 " , " 生物制药 " , " (1)班 " , " 黄兴 " , " 7 " },
{ " 200511100221 " , " 贰零零五届 " , " 化工生物学院 " , " 生物制药 " , " (2)班 " , " 蕾蕾 " , " 8 " },
{ " 200511100322 " , " 贰零零五届 " , " 化工生物学院 " , " 生物制药 " , " (3)班 " , " 白冰 " , " 9 " },
{ " 200611120120 " , " 贰零零六届 " , " 化工生物学院 " , " 发酵工程 " , " (1)班 " , " 书并 " , " 10 " },
{ " 200611120121 " , " 贰零零六届 " , " 化工生物学院 " , " 发酵工程 " , " (1)班 " , " 小鱼 " , " 11 " },
{ " 200611120421 " , " 贰零零六届 " , " 化工生物学院 " , " 发酵工程 " , " (4)班 " , " 声声 " , " 12 " },
};
[WebMethod(Description = " 根据学号获得学生的姓名 " )]
public string GetName( string strStuNum)
{
for ( int i = 0 ; i < strStuInfo.GetLength( 0 ); i ++ )
{
if (String.Compare(strStuNum, strStuInfo[i, 0 ], true ) == 0 )
return strStuInfo[i, 5 ].ToString();
}
return " 您输入的学号不存在 " ;
}
[WebMethod(Description = " 根据学号获得学生的专业和班级 " )]
public string GetClass( string strStuNum)
{
for ( int i = 0 ; i < strStuInfo.GetLength( 0 ); i ++ )
{
if (String.Compare(strStuNum, strStuInfo[i, 0 ], true ) == 0 )
{
return strStuInfo[i, 3 ].ToString() + strStuInfo[i, 4 ].ToString();
}
}
return " 您输入的学号不存在 " ;
}
[WebMethod(Description = " 根据学号获得学生的入学时间 " )]
public string GetYear( string strStuNum)
{
for ( int i = 0 ; i < strStuInfo.GetLength( 0 ); i ++ )
{
if (String.Compare(strStuNum, strStuInfo[i, 0 ], true ) == 0 )
{
return strStuInfo[i, 1 ].ToString();
}
}
return " 您输入的学号不存在 " ;
}
[WebMethod(Description = " 根据学号获得学生的学院 " ]
public string GetCollege( string strStuNum)
{
for ( int i = 0 ; i < strStuInfo.GetLength( 0 ); i ++ )
{
if (String.Compare(strStuNum, strStuInfo[i, 0 ], true ) == 0 )
{
return strStuInfo[i, 2 ].ToString();
}
}
return " 您输入的学号不存在 " ;
}
[WebMethod(Description = " 点击次数 " , EnableSession = true )]
public int HitCounter()
{
if (Session[ " HitCounter " ] == null )
{
Session[ " HitCounter " ] = 1 ;
}
else
{
Session[ " HitCounter " ] = (( int )Session[ " HitCounter " ]) + 1 ;
}
return (( int )Session[ " HitCounter " ]);
}
(3)生成Web服务,在浏览器中打开(效果如下)
(4)创建个其他程序,调用就可以了!
WebMethod特性的EnableSession属性
此属性默认为false。如果设置为true,web方法将会启用会话状态,且继承自WebService类,那么会话状态集合可以使用WebService.Session属性访问,如果没有集成,则可以直接从HttpContext.Current.Session访问。
上面代码中HitCounter()方法为每个会话准备了单击计数器。
会话状态为应用程序增加了额外的开销,如果移除会话状态可以提高性能。然而,会话状态作为全局变量很有用,因为它能够超出成员变量的工作范围。
需要注意的是:会话状态在Web服务中是通过cookies实现的,如果传输机制不是HTTP的话,则不可用。
WebMethod特性的MessageName属性
Web服务禁止使用函数重载。MessageName属性可以解决由多个相同的名称的方法造成的无法识别的问题,它允许对每一个方法的重载使用唯一的别名。当从在方法在SOAP消息中引用时,SOAP将使用MessageName而非方法名。
在上面的例子中,为了试验这个属性,我改了一下,写两个GetCollege()方法。
public string GetCollege( string strStuNum)
{
for ( int i = 0 ; i < strStuInfo.GetLength( 0 ); i ++ )
{
if (String.Compare(strStuNum, strStuInfo[i, 0 ], true ) == 0 )
{
return strStuInfo[i, 2 ].ToString();
}
}
return " 您输入的学号不存在 " ;
}
[WebMethod(Description = " 根据排名获得学生的学院 " , MessageName = " GetCollegeStuRank " )]
public string GetCollege( int intStuRank)
{
for ( int i = 0 ; i < strStuInfo.GetLength( 0 ); i ++ )
{
if (String.Compare(intStuRank.ToString(), strStuInfo[i, 6 ], true ) == 0 )
{
return strStuInfo[i, 2 ].ToString();
}
}
return " 您输入的学生排名不存在 " ;
}
可是当你从新生成的时候,会发现不会在报错了,但是在浏览器打开时,却会出现下面的问题。
后来看书发现了这个问题的原因在于:
由于默认WebServiceBinding特性遵循WS-1 BP V1.1,如果两个方法处于相同的数据绑定,那么该规范不允许方法的重载(尽管使用了MessageName属性)可以做两种选择:
(1)重命名方法,当然用这种方法最好是客户端没有链接到Web服务。
(2)创建一个新的绑定来代特重载方法
做法如下:
1)为类添加新的属性(文件的摘要下面),代码如下:
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1, Name = " OverloadedGetCollege " ,EmitConformanceClaims = true )]
[ToolboxItem( false )]
多添加了这个两个属性:Name = "OverloadedGetCollege",EmitConformanceClaims=true
2)在任意一个GetCollege()方法上部添加SoapDocumentMethod特性:
[SoapDocumentMethod(Binding="OverloadedGetCollege")]
[WebMethod(Description = "根据学号获得学生的学院", MessageName = "GetCollegeStuNum")]
public string GetCollege(string strStuNum)
3)重新生成,就成功了。效果如下:
想了解这样的好处,点击如上图的“服务说明”,就可以看到WSDL的变化,我也就不多说了!