WCF光芒下的Web Service
public class MyHeader:SoapHeader
{
public int ID { get; set; }
public string Name { get; set; }
public string PassWord { get; set; }
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Info : System.Web.Services.WebService
{
public SoapUnknownHeader[] unknownHeaders;
public MyHeader MyHeader { get; set; }
[WebMethod]
[SoapHeader("unknownHeaders")]
public string HelloWorld()
{
Console.WriteLine("?>>>>>>>>>>>>>>>>>>>>");
return "Hello World";
}
[SoapHeader("MyHeader", Direction = SoapHeaderDirection.InOut)]
[WebMethod]
public string Audit()
{
Validate();
return "这里是验证";
}
//验证函数 自定义
private string Validate()
{
if (MyHeader != null)
{
if (MyHeader.Name == MyHeader.PassWord)
{
return "验证通过";
}
else
{
return "验证失败";
}
}
return "未传递消息头";
}
[System.Web.Services.Protocols.SoapHeaderAttribute("MyHeaderValue", Direction=System.Web.Services.Protocols.SoapHeaderDirection.InOut)]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Audit", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public string Audit() {
object[] results = this.Invoke("Audit", new object[0]);
return ((string)(results[0]));
MyService.Info info = new MyService.Info();
MyService.MyHeader myHeader = new MyService.MyHeader();
myHeader.ID = 1;
myHeader.Name = "qingyuan";
myHeader.PassWord = "qingyuan";
info.MyHeaderValue = myHeader;
string content= info.Audit();
public class MyInfo:SoapHeader
{
public MyInfo()
{
}
public int ID { get; set; }
public string Name { get; set; }
/*******************************************************************/
public MyInfo MyInfo { get; set; }
[SoapHeader("MyInfo", Direction = SoapHeaderDirection.InOut)]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/HelloWorld", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public string HelloWorld() {
object[] results = this.Invoke("HelloWorld", new object[0]);
return ((string)(results[0]));
}
MyService.Info info = new MyService.Info();
MyInfo myInfo = new MyInfo();
myInfo.ID = 1;
myInfo.Name = "dddd";
info.MyInfo = myInfo;
string name = info.HelloWorld();
从上图可以看出,数据已经传输过来了,得到的是一个xml格式的数据,对于xml的解析在.NET中不是难事,在此就可以完成匿名SoapHeader报文的发送和接收