English 中文(简体)
WCF: 信息查询
原标题:WCF: MessageContract handle OnDeserialized Event

我如何利用电传来处理一场关于宇宙化的活动?

需要在收到信息后进行一些数据核查和转变(但在执行方法之前)。

关于数据冲突,通过声明属性解决了这一问题。

但是,对于电传来说,它确实不工作。

是否有办法这样做?

最佳回答

You are better of using WCF extension points instead of serialization. Specifically IOperationInvoker.

http://www.ohchr.org。

例:

服务和电文定义如下。 Note the new MyValidationBeforeInvokeBehavior Depende.

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        [MyValidationBeforeInvokeBehavior]
        AddPatientRecordResponse AddPatientRecord(PatientRecord composite);
    }

    [MessageContract(IsWrapped = false, ProtectionLevel = ProtectionLevel.None)]
    public class AddPatientRecordResponse
    {
        [MessageHeader(ProtectionLevel = ProtectionLevel.None)]
        public Guid recordID;
        [MessageHeader(ProtectionLevel = ProtectionLevel.None)]
        public string patientName;
        [MessageBodyMember(ProtectionLevel = ProtectionLevel.None)]
        public string status;

    }


    [MessageContract(IsWrapped = false, ProtectionLevel = ProtectionLevel.None)]
    public class PatientRecord
    {
        [MessageHeader(ProtectionLevel = ProtectionLevel.None)]
        public Guid recordID;
        [MessageHeader(ProtectionLevel = ProtectionLevel.None)]
        public string patientName;

        //[MessageHeader(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
        [MessageHeader(ProtectionLevel = ProtectionLevel.None)]
        public string SSN;
        [MessageBodyMember(ProtectionLevel = ProtectionLevel.None)]
        public string comments;
        [MessageBodyMember(ProtectionLevel = ProtectionLevel.None)]
        public string diagnosis;
        [MessageBodyMember(ProtectionLevel = ProtectionLevel.None)]
        public string medicalHistory;
    }

public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public AddPatientRecordResponse AddPatientRecord(PatientRecord patient)
        {
            var response = new AddPatientRecordResponse
                               {
                                   patientName = patient.patientName, 
                                   recordID = patient.recordID, 
                                   status = "Sucess"
                               };

            return response;
        }
    }

Hook into wcf exten

public class MyValidationBeforeInvokeBehavior : Attribute, IOperationBehavior
    {
        public void Validate(OperationDescription operationDescription)
        {
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            dispatchOperation.Invoker = new MyValidationBeforeInvoke(dispatchOperation.Invoker);
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
        }

        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
        }
    }

习惯操作者:

public class MyValidationBeforeInvoke : IOperationInvoker
    {
        private readonly IOperationInvoker _original;

        public MyValidationBeforeInvoke(IOperationInvoker original)
        {
            _original = original;
        }

        public object[] AllocateInputs()
        {
            return _original.AllocateInputs();
        }

        public object Invoke(object instance, object[] inputs, out object[] outputs)
        {

            var validator = new ValidatePatientRecord((PatientRecord) inputs[0]);
            if (validator.IsValid())
            {
                var ret = _original.Invoke(instance, inputs, out outputs);
                return ret;
            }
            else
            {
                outputs = new object[] {};

                var patientRecord = (PatientRecord) inputs[0];

                var returnMessage = new AddPatientRecordResponse
                                        {
                                            patientName = patientRecord.patientName,
                                            recordID = patientRecord.recordID,
                                            status = "Validation Failed"
                                        };
                return returnMessage;    
            }


        }

        public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
           return _original.InvokeBegin(instance, inputs, callback, state);
        }

        public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
        {
            return _original.InvokeEnd(instance, out outputs, result);
        }

        public bool IsSynchronous
        {
            get { return _original.IsSynchronous; }
        }
    }

实质是,我们从未援引这项服务要求,因为它永远不会因为验证错误而获得服务。 我们还能够恢复客户的遭遇。

定级和客户电话(完整):

public class ValidatePatientRecord
    {
        private readonly PatientRecord _patientRecord;

        public ValidatePatientRecord(PatientRecord patientRecord)
        {
            _patientRecord = patientRecord;
        }

        public bool IsValid()
        {
            return _patientRecord.patientName != "Stack Overflow";
        }
    }

客户:

class Program
    {
        static void Main(string[] args)
        {
            var patient = new PatientRecord { SSN = "123", recordID = Guid.NewGuid(), patientName = "Stack Overflow" };

            var proxy = new ServiceReference1.Service1Client();

            var result = proxy.AddPatientRecord(patient);

            Console.WriteLine(result.status);
            Console.ReadLine();
        }
    }
问题回答

暂无回答




相关问题
WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

Access WCF service on same server

I have a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...but that seems ...

WCF binding error

So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...

The service operation requires a transaction to be flowed

I am facing strange issue with our WCF service. The same code was working fine until recently we added more OperationContracts(Web Methods). We have common 3 tier architecture. DAL (WCF) BLL Web ...

热门标签