修改WCF操作中的returnvalue

时间:2011-08-23 09:17:18

标签: c# wcf behavior wcf-behaviour iservicebehaviors

我想确保在WCF中返回数据集的所有操作都在Property SchemaSerializationMode中设置了.ExcludedSchema值。

我可以使用CustomBehavior执行此操作吗? 我试图实现一个CustomDispatchBehavior并添加一个MessageInspector,但AfterReceiveRequest和BeforeSendReply方法不允许我对返回值做任何事情。在BeforeSendreply中,返回值已经被序列化。我在哪里可以插入我的代码?

    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();
        }

    }

2 个答案:

答案 0 :(得分:1)

查看IDispatchMessageFormatter界面。它定义了对请求消息进行反序列化并在服务应用程序中序列化响应消息的方法。

答案 1 :(得分:1)

我使用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;
                }
            }
        }
    }