关于WebService中SOAP扩展求助~~
最近整了个WebService的SOAP扩展,遇到了问题,网上找了很久,没找到一个好的方法。想到园子里肯定高手如云,特来此求助~!
情况是这样的:
我有一个WebService,在没加SOAP扩展前是可以访问的。代码如下:
后来通过学习,想到通过SOAP扩展来加密消息,于是:
这里在序列化之后和反序列化之前都输出一次当前流中的数据,但是第次都会出错,当我在客户端调用方法时,总会弹出个对话框:
并且日志文件里输出的是:
BeforeDeserialize
GetAll
还有XML头,
没搞明白是怎么回事,
当:
情况是这样的:
我有一个WebService,在没加SOAP扩展前是可以访问的。代码如下:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[Serializable]
public class UsersDAL : System.Web.Services.WebService
{
public UsersDAL() { }
//private static UserDataAccess access = new UserDataAccess();
private static DataAccess.UserDataAccess access = new DataAccess.UserDataAccess();
[WebMethod, MySoap]
public void Create(DataAccess.Users user)
{
access.Create(user);
}
[WebMethod, MySoap]
public DataAccess.Users Get(int id)
{
return access.Get(id);
}
[WebMethod, MySoap]
public List<DataAccess.Users> GetAll()
{
return access.GetAllUsers();
}
[WebMethod, MySoap]
public string Hello(string name)
{
return "Hello:" + name;
}
}
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[Serializable]
public class UsersDAL : System.Web.Services.WebService
{
public UsersDAL() { }
//private static UserDataAccess access = new UserDataAccess();
private static DataAccess.UserDataAccess access = new DataAccess.UserDataAccess();
[WebMethod, MySoap]
public void Create(DataAccess.Users user)
{
access.Create(user);
}
[WebMethod, MySoap]
public DataAccess.Users Get(int id)
{
return access.Get(id);
}
[WebMethod, MySoap]
public List<DataAccess.Users> GetAll()
{
return access.GetAllUsers();
}
[WebMethod, MySoap]
public string Hello(string name)
{
return "Hello:" + name;
}
}
后来通过学习,想到通过SOAP扩展来加密消息,于是:
public class MySoap : SoapExtension
{
public override object GetInitializer(Type serviceType)
{
return GetType();
}
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return null;
}
public override void Initialize(object initializer)
{
//throw new Exception("The method or operation is not implemented.");
}
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
Print(message);
break;
case SoapMessageStage.BeforeDeserialize:
Print(message);
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}
public override Stream ChainStream(Stream stream)
{
return base.ChainStream(stream);
}
void Print(SoapMessage message)
{
if (message.Stream.CanRead)
{
StreamReader reader = new StreamReader(message.Stream);
string xml = reader.ReadToEnd();
//FileStream fs = new FileStream("C:\\log.txt", FileMode.Append, FileAccess.Write);
FileStream fs = new FileStream("C:\\log.txt", FileMode.Append, FileAccess.Write);
StreamWriter w = new StreamWriter(fs);
w.WriteLine(message.Stage.ToString() + "\r\n" + message.MethodInfo.Name + "\r\n" + xml);
w.Close();
fs.Close();
}
}
}
public class MySoapAttribute : SoapExtensionAttribute
{
public override Type ExtensionType
{
get { return typeof(MySoap); }
}
private int priority = 1;
public override int Priority
{
get { return priority; }
set { priority = value; }
}
}
{
public override object GetInitializer(Type serviceType)
{
return GetType();
}
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return null;
}
public override void Initialize(object initializer)
{
//throw new Exception("The method or operation is not implemented.");
}
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
Print(message);
break;
case SoapMessageStage.BeforeDeserialize:
Print(message);
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}
public override Stream ChainStream(Stream stream)
{
return base.ChainStream(stream);
}
void Print(SoapMessage message)
{
if (message.Stream.CanRead)
{
StreamReader reader = new StreamReader(message.Stream);
string xml = reader.ReadToEnd();
//FileStream fs = new FileStream("C:\\log.txt", FileMode.Append, FileAccess.Write);
FileStream fs = new FileStream("C:\\log.txt", FileMode.Append, FileAccess.Write);
StreamWriter w = new StreamWriter(fs);
w.WriteLine(message.Stage.ToString() + "\r\n" + message.MethodInfo.Name + "\r\n" + xml);
w.Close();
fs.Close();
}
}
}
public class MySoapAttribute : SoapExtensionAttribute
{
public override Type ExtensionType
{
get { return typeof(MySoap); }
}
private int priority = 1;
public override int Priority
{
get { return priority; }
set { priority = value; }
}
}
这里在序列化之后和反序列化之前都输出一次当前流中的数据,但是第次都会出错,当我在客户端调用方法时,总会弹出个对话框:
并且日志文件里输出的是:
BeforeDeserialize
GetAll
还有XML头,
没搞明白是怎么回事,
当:
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
//Print(message);
break;
case SoapMessageStage.BeforeDeserialize:
//Print(message);
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
//Print(message);
break;
case SoapMessageStage.BeforeDeserialize:
//Print(message);
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}
这样的时候,肯定是正确的,当然日志也没有输出。
我这样做的目的是想看一下每回那个流到底是什么样子(我想应该是XML吧~),然好想个办法加密一下这个流。
但是我像上面那样输出流应该没什么问题吧,为什么出错呢~
我再改了一下:
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
Print(message);
break;
case SoapMessageStage.BeforeDeserialize:
//Print(message);
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
Print(message);
break;
case SoapMessageStage.BeforeDeserialize:
//Print(message);
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}
这样的时候,数据可以正确访问,但是没日志输出。表示在充列化之后流是不可读的~~那如果这里不行的话,加密应该在那里进行呢?
请各位高手指点一下,这个问题困扰很久啦~~~
我不是最强的,但我是最努力的!