1

我想确保在 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();
        }

    }
4

2 回答 2

1

看看IDispatchMessageFormatter接口。它定义了在服务应用程序中反序列化请求消息和序列化响应消息的方法。

于 2011-08-23T10:34:06.680 回答
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;
                }
            }
        }
    }
于 2011-08-23T11:08:18.143 回答