C# 如何在WCF服务的服务行为中获取请求消息的类型(如request.GetType)
我正在编写一个自定义ServiceBehavior,它希望我知道请求消息的类型,以推断消息是否用自定义属性修饰 我的合同样本可以如下所示:C# 如何在WCF服务的服务行为中获取请求消息的类型(如request.GetType),c#,wcf,servicebehavior,idispatchmessageinspector,C#,Wcf,Servicebehavior,Idispatchmessageinspector,我正在编写一个自定义ServiceBehavior,它希望我知道请求消息的类型,以推断消息是否用自定义属性修饰 我的合同样本可以如下所示: [DataContract] [Auditable] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] [Audit] public bool BoolValue
[DataContract]
[Auditable]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
[Audit]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
[Audit]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
我正在尝试使用以下方法在行为端识别自定义属性:
public object AfterReceiveRequest(ref Message request, IClientChannel channel,
InstanceContext instanceContext)
{
var typeOfRequest = request.GetType();
if (!typeOfRequest.GetCustomAttributes(typeof (AuditableAttribute), false).Any())
{
return null;
}
}
但是typeOfRequest总是以{Name=“BufferedMessage”FullName=“System.ServiceModel.Channels.BufferedMessage”}的形式出现
有没有一种方法可以通过使用请求推断消息的类型
注意:我直接引用了保存契约的程序集,而服务不是通过wsdl引用的 上述问题的解决方案不是使用MessageInspector(如IDispatchMessageInspector或IClientMessageInspector),而是使用参数检查器(如IPParameterInspector) 在BeforeCall方法中,我们可以执行以下操作:
public object BeforeCall(string operationName, object[] inputs)
{
var request = inputs[0];
var typeOfRequest = request.GetType();
if (!typeOfRequest.GetCustomAttributes(typeof(CustomAttribute), false).Any())
{
return null;
}
}
您的服务是否使用SOAP?是的,它使用SOAP消息进行通信。您可以检查SOAP消息以确定请求中序列化了哪些对象。但是,为什么要在服务对消息进行反序列化之前确定消息中定义的类型?正如问题陈述中所述,我只想读取由自定义属性修饰的契约的某些值。