我使用消息检查器和实现的占位符类型在这方面取得了一些进展IExtensibleDataObject
。检查器处理传入的消息并将类型提示更改为占位符的提示,并将原始类型添加为属性。然后在回复中发送类型时,会发生相反的情况,从而使占位符看起来像原始类型。
我对这个解决方案的不满在于它与服务绑定,因为我必须包含服务的 XML 命名空间并明确命名要操作的方法和参数。除此之外,它似乎工作得相当好,尽管我只在从BaseType
.
有人可以改进吗?里面有赏金给你。
public class PlaceholderType : BaseType, IExtensibleDataObject
{
[IgnoreDataMember]
public string OriginalTypeName { get; set; }
[IgnoreDataMember]
public string OriginalNamespace { get; set; }
ExtensionDataObject IExtensibleDataObject.ExtensionData { get; set; }
}
public class FunkadelicInspector : IDispatchMessageInspector, IContractBehavior
{
const string PlaceholderNamespace = "http://my.placeholder.namespace";
const string ServiceNamespace = "http://tempuri.org/";
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
XmlDocument xmlDoc = ReadMessage(request);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
// Dislike: having to know the service namespace, method and parameters
nsmgr.AddNamespace("s", ServiceNamespace);
XmlNode itemElement = xmlDoc.SelectSingleNode("//s:Push/s:item", nsmgr);
if (itemElement != null)
{
XmlAttribute typeAttribute = itemElement.Attributes["type", "http://www.w3.org/2001/XMLSchema-instance"];
if (typeAttribute != null)
{
// Record original type
string[] parts = typeAttribute.Value.Split(':');
string originalTypeName = parts[1];
// Replace with placeholder type
typeAttribute.Value = parts[0] + ":" + typeof(PlaceholderType).FullName;
// Record original assembly
XmlAttribute nsAtt = itemElement.Attributes["xmlns:" + parts[0]];
string originalAssembly = nsAtt.Value;
// Replace with placeholder type's assembly
nsAtt.Value = typeof(PlaceholderType).Assembly.FullName;
// Add placeholders
itemElement.AppendChild(xmlDoc.CreateElement("OriginalType", PlaceholderNamespace)).InnerText = originalTypeName;
itemElement.AppendChild(xmlDoc.CreateElement("OriginalAssembly", PlaceholderNamespace)).InnerText = originalAssembly;
}
}
//Now recreate the message
request = WriteMessage(request, xmlDoc);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
XmlDocument xmlDoc = ReadMessage(reply);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("s", ServiceNamespace);
nsmgr.AddNamespace("plc", PlaceholderNamespace);
// Dislike: having to know the service namespace, method and parameters
XmlNode resultElement = xmlDoc.SelectSingleNode("//s:PopResponse/s:PopResult", nsmgr);
if (resultElement != null)
{
XmlElement originalType = resultElement.SelectSingleNode("plc:OriginalType", nsmgr) as XmlElement;
XmlElement originalAssembly = resultElement.SelectSingleNode("plc:OriginalAssembly", nsmgr) as XmlElement;
if (originalType != null && originalAssembly != null)
{
// Replace original type
XmlAttribute type = resultElement.Attributes["type", "http://www.w3.org/2001/XMLSchema-instance"];
string[] parts = type.Value.Split(':'); // 0 is an alias for the assembly, 1 is the type
type.Value = parts[0] + ":" + originalType.InnerText;
// Replace original assembly
XmlAttribute ass = resultElement.Attributes["xmlns:" + parts[0]];
ass.Value = originalAssembly.InnerText;
// Remove placeholders
resultElement.RemoveChild(originalType);
resultElement.RemoveChild(originalAssembly);
}
}
//Now recreate the message
reply = WriteMessage(reply, xmlDoc);
}
private static Message WriteMessage(Message original, XmlDocument xmlDoc)
{
MemoryStream ms = new MemoryStream();
xmlDoc.Save(ms);
ms.Position = 0;
XmlReader reader = XmlReader.Create(ms);
Message newMessage = Message.CreateMessage(reader, int.MaxValue, original.Version);
newMessage.Properties.CopyProperties(original.Properties);
return newMessage;
}
private static XmlDocument ReadMessage(Message message)
{
MemoryStream ms = new MemoryStream();
using (XmlWriter writer = XmlWriter.Create(ms))
{
message.WriteMessage(writer); // the message was consumed here
writer.Flush();
}
ms.Position = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ms);
return xmlDoc;
}
void IContractBehavior.AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
void IContractBehavior.ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
void IContractBehavior.ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
dispatchRuntime.MessageInspectors.Add(this);
}
void IContractBehavior.Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
}
}