最简单的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public class ClientEngine : SoapExtension
{
Stream oldStream;
Stream newStream;
string actionName;
/// <summary>
/// 构造函数
/// </summary>
public ClientEngine()
{
}
public override object GetInitializer(LogicalMethodInfo methodInfo,SoapExtensionAttribute attribute)
{
return attribute;
}
public override object GetInitializer(Type t)
{
return null;
}
public override void Initialize(object initializer)
{
}
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
this.actionName = message.Action;
break;
case SoapMessageStage.AfterSerialize:
newStream.Position = 0;
Copy(newStream, oldStream);
break;
case SoapMessageStage.BeforeDeserialize:
this.actionName = message.Action;
Copy(oldStream, newStream);
newStream.Position = 0;
break;
case SoapMessageStage.AfterDeserialize: break;
default:
throw new Exception("invalid stage");
}
}
public override Stream ChainStream(Stream stream)
{
oldStream = stream;
newStream = new MemoryStream();
return newStream;
}
void Copy(Stream from, Stream to)
{
TextReader reader = new StreamReader(from);
TextWriter writer = new StreamWriter(to);
string strContent = reader.ReadToEnd();
writer.Write(strContent);
writer.Flush();
}
}
注意newStream.Position = 0;这个可是很重要,却很容易忽略。
客户端的app.config配置使用上面的类
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
app.config节点configuration添加
<system.web>
<webServices>
<soapExtensionTypes>
<add type="Ysd.Web.Security.ClientEngine, Ysd.Web.Security"
priority="1" group="High" />
</soapExtensionTypes>
</webServices>
</system.web>
客户端调用一下webservcie,你似乎没感觉到我们的类的存在。放断点在