----------BOX----------
//TimestampOutputFilter
public class TimestampOutputFilter : SoapOutputFilter
{
public override void ProcessMessage(SoapEnvelope envelope);
}
//TimestampInputFilter
public class TimestampInputFilter : SoapInputFilter
{
public override void ProcessMessage(SoapEnvelope envelope);
}
----------end----------
----------BOX----------
static void Main(string[] args)
{
// 建立空白的SOAP訊息
SoapEnvelope env = new SoapEnvelope();
XmlElement body = env.CreateBody();
env.Envelope.AppendChild(body);
// 產生原始的SOAP訊息
Console.WriteLine("Original message:\n\n{0}\n", env.OuterXml);
// 建立時間輸出filter
TimestampOutputFilter tsOutput = new TimestampOutputFilter();
// 處理訊息,寫出時間標頭
tsOutput.ProcessMessage(env);
// 輸出Filter處理過的訊息
Console.WriteLine("Output filtered message:\n\n{0}\n", env.OuterXml);
// 建立時間輸入Filter
TimestampInputFilter tsInput = new TimestampInputFilter();
// 處理訊息,讀取時間標頭
tsInput.ProcessMessage(env);
//輸出Filter處理過的訊息
Console.WriteLine("Input filtered message:\n\n{0}\n", env.OuterXml);
}
----------end----------
----------BOX----------
//原始訊息:
//輸出Filter處理過的訊息:
2002-11-14T19:03:27Z
2002-11-14T19:08:27Z
//輸入Filter處理過的訊息:
----------end----------
----------BOX----------
// 建立信封
SoapEnvelope env = new SoapEnvelope();
...
//設定結束時間為10分鐘
env.Context.Timestamps.Ttl = 600000;
----------end----------
----------BOX----------
public class Pipeline
{
public Pipeline();
public Pipeline(Pipeline p);
public Pipeline(SoapInputFilterCollection inputFilters,
SoapOutputFilterCollection outputFilters);
public SoapInputFilterCollection inputFilters { get; }
public SoapOutputFilterCollection outputFilters { get; }
public void ProcessInputMessage(SoapEnvelope envelope);
public void ProcessOutputMessage(SoapEnvelope envelope);
}
----------end----------
----------BOX----------
static void Main(string[] args)
{
// 建立輸入Filterd的集合
SoapInputFilterCollection inputFilters = new SoapInputFilterCollection();
// 建立輸出Filters集合
SoapOutputFilterCollection outputFilters =new SoapOutputFilterCollection();
// 加入想要使用的輸出Filter
outputFilters.Add(new TraceOutputFilter());
outputFilters.Add(new TimestampOutputFilter());
// 建立空的SOAP訊息
SoapEnvelope env = new SoapEnvelope();
XmlElement body = env.CreateBody();
env.Envelope.AppendChild(body);
// 輸出原始訊息
Console.WriteLine("Original message:\n\n{0}\n", env.OuterXml);
// 建立Pipeline來封裝filter集合
Pipeline pipe = new Pipeline(inputFilters, outputFilters);
// 使用所有Pipeline中的輸出Filters來處理訊息
// pipeline的集合
pipe.ProcessOutputMessage(env);
// 輸出output filter處理過的訊息
Console.WriteLine("Output filtered message:\n\n{0}\n", env.OuterXml);
}
----------end----------
----------BOX----------
//原始訊息:
//output filter處理過的訊息:
2002-11-14T23:17:48Z
2002-11-14T23:22:48Z
----------end----------
----------BOX----------
public static void ReconfigureDefaultPipeline()
{
// 取得預設輸入filter集合
SoapInputFilterCollection defaultInputFilters =
WebServicesConfiguration.FilterConfiguration.InputFilters;
// 移除繞送與參考輸入filter
defaultInputFilters.Remove(typeof(RoutingInputFilter));
defaultInputFilters.Remove(typeof(ReferralInputFilter));
// 取得預設輸出filter集合
SoapOutputFilterCollection defaultOutputFilters =
WebServicesConfiguration.FilterConfiguraiton.OutputFilters;
// 移除繞送與參考filter
defaultOutputFilters.Remove(typeof(RoutingOutputFilter));
defaultOutputFilters.Remove(typeof(ReferralOutputFilter));
}
----------end----------
----------BOX----------
static void Main(string[] args)
{
//第一個pipeline擁有所有的filter
Pipeline pipe1 = new Pipeline();
// 修改預設的filter集合
//移除繞送與參考filter
ReconfigureDefaultPipeline();
// 第二個pipeline有一個安全性與timestamp filter,但是沒有繞送與參考Filter
Pipeline pipe2 = new Pipeline();
... //使用Pipeline
}
----------end----------
----------BOX----------
<%@ import namespace="Microsoft.Web.Services" %>
<%@ import namespace="Microsoft.Web.Services.Configuration" %>
<%@ import namespace="Microsoft.Web.Services.Routing" %>
<%@ import namespace="Microsoft.Web.Services.Referral" %>
----------end----------
----------BOX----------
public class SoapFaultOutputFilter : SoapOutputFilter
{
public override void ProcessMessage(SoapEnvelope envelope)
{
// 使用Xpath來搜尋訊息中的fault
XmlNamespaceManager nsMgr =
new XmlNamespaceManager(envelope.NameTable);
nsMgr.AddNamespace("soap",
"http://schemas.xmlsoap.org/soap/envelope/");
XmlElement fault = (XmlElement)
envelope.Body.SelectSingleNode("soap:Fault", nsMgr);
// 如果訊息存在fault...
if (fault != null)
{
//取得fault子節點
XmlNode faultcode = fault.SelectSingleNode("faultcode");
XmlNode faultstring = fault.SelectSingleNode("faultstring");
XmlNode faultactor = fault.SelectSingleNode("faultactor");
XmlNode detail = fault.SelectSingleNode("detail");
//格式化fault子節點為字串格式
string msg = string.Format("{0}\n{1}\n{2}\n\n{3}",
((faultcode != null) ? faultcode.InnerText : ""),
((faultstring != null) ? faultstring.InnerText : ""),
((faultactor != null) ? faultactor.InnerText : ""),
((detail != null) ? detail.OuterXml : ""));
//將fault本體寫到byte陣列中
MemoryStream stm = new MemoryStream();
StreamWriter sw = new StreamWriter(stm);
sw.Write(fault.OuterXml);
sw.Flush();
byte[] buf = stm.ToArray();
// 將fault資料寫到事件記錄中
WriteToEventLog(msg, "WSE", buf);
sw.Close();
}
}
//寫入事件記錄函式
private void WriteToEventLog(string msg, string src, byte[] buf)
{
EventLog eventLog = null;
try
{
// 將 fault寫入事件記錄中
eventLog = new EventLog();
eventLog.Log = "WSEFaultLog";
eventLog.Source = src;
eventLog.WriteEntry(msg, EventLogEntryType.Error, 0, 0, buf);
}
finally
{
if (eventLog != null) eventLog.Dispose();
}
}
}
----------end----------
----------BOX----------
public class SoapContext
{
public void Add(string key, object value);
public void Clear();
public bool Contains(string key);
public IDictionaryEnumerator GetEnumerator();
public void Remove(string key);
... // 忽略其他屬性與方法
}
----------end----------
----------BOX----------
// 將fault資料寫到事件記錄中
WriteToEventLog(msg, "WSE", buf);
----------end----------
----------BOX----------
//將fault資料寫到事件記錄中
string src = "WSE";
if (envelope.Context.Contains("FaultSource"))
src = envelope.Context["FaultSource"];
WriteToEventLog(msg, src, buf);
----------end----------
----------BOX----------
----------end----------
----------BOX----------
WebServicesConfiguration.FilterConfiguration.OutputFilters.Add(
new SoapFaultOutputFilter();
----------end----------
----------BOX----------
public void EnvelopeToDIMEStream(SoapEnvelope env, Stream stm)
{
// 建立DIME訊息的writer
DimeWriter dw = new DimeWriter(stm);
// 產生新的紀錄編號
Guid guid = Guid.NewGuid();
string id = string.Format("uuid:{0}", guid.NewGuid().ToString()));
// 建立新紀錄,指出SOAP信封的內容並且估計長度
DimeRecord rec = dw.LastRecord(id,
"http://schemas.xmlsoap.org/soap/envelope/",
TypeFormatEnum.AbsoluteUri, -1);
// 寫 SOAP 訊息到DIME記錄
env.Save(rec.BodyStream);
// 清除
dw.Close();
}
----------end----------
----------BOX----------
public DIMEStreamToEnvelope(Stream stm, SoapEnvelope env)
{
// 建立DIME訊息的記錄
DimeReader dr = new DimeReader(stm);
// 讀取包含SOAP訊息的記錄
DimeRecord rec = dr.ReadRecord();
//從DIME記錄中讀取SOAP訊息
env.Load(rec.BodyStream);
// 確定沒有多餘的記錄
Debug.Assert(rec.MessageEnd);
}
----------end----------