修改WCF操作中的返回值
本文关键字:返回值 操作 WCF 修改 | 更新日期: 2023-09-27 18:06:29
我想确保在我们的WCF中返回数据集的所有操作都在属性schemasserializationmode中设置。excludedschema值。
我能做到这一点与CustomBehavior吗?我试图实现一个CustomDispatchBehavior和添加一个MessageInspector,但方法afterreceiverrequest和beforeendreply不让我做任何与返回值。在beforeendreply中,返回值已经被序列化。我可以在哪里插入我的代码?
public class CustomDispatchBehavior : BehaviorExtensionElement, IServiceBehavior
{
public override Type BehaviorType
{
get { return typeof(CustomDispatchBehavior); }
}
protected override object CreateBehavior()
{
return new CustomDispatchBehavior();
}
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
//throw new NotImplementedException();
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
//throw new NotImplementedException();
foreach (ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher ed in chanDisp.Endpoints)
{
ed.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector());
}
}
}
void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
//throw new NotImplementedException();
}
}
看一下IDispatchMessageFormatter接口。它定义了在服务应用程序中反序列化请求消息和序列化响应消息的方法。
我使用IParametorInspector
解决了这个问题 void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
{
foreach (DispatchOperation op in epDisp.DispatchRuntime.Operations)
op.ParameterInspectors.Add(new DataSetParameterInspector());
}
}
}
检查器是这样的
public class DataSetParameterInspector : IParameterInspector
{
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
Type t =returnValue.GetType();
if (t.IsSubclassOf(typeof(GlobalUtils.RR.Response)))
{
foreach (var pi in t.GetProperties())
{
if (pi.PropertyType.IsSubclassOf(typeof(System.Data.DataSet)))
{
object parameter = pi.GetValue(returnValue, null);
if (parameter != null)
((System.Data.DataSet)parameter).SchemaSerializationMode = System.Data.SchemaSerializationMode.ExcludeSchema;
}
}
}
}