ProtoOperationBehavior.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #if FEAT_SERVICEMODEL && PLAT_XMLSERIALIZER
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel.Description;
  6. using System.Xml;
  7. using ProtoBuf.Meta;
  8. namespace ProtoBuf.ServiceModel
  9. {
  10. /// <summary>
  11. /// Describes a WCF operation behaviour that can perform protobuf serialization
  12. /// </summary>
  13. public sealed class ProtoOperationBehavior : DataContractSerializerOperationBehavior
  14. {
  15. private TypeModel model;
  16. /// <summary>
  17. /// Create a new ProtoOperationBehavior instance
  18. /// </summary>
  19. public ProtoOperationBehavior(OperationDescription operation) : base(operation)
  20. {
  21. #if !NO_RUNTIME
  22. model = RuntimeTypeModel.Default;
  23. #endif
  24. }
  25. /// <summary>
  26. /// The type-model that should be used with this behaviour
  27. /// </summary>
  28. public TypeModel Model
  29. {
  30. get { return model; }
  31. set
  32. {
  33. model = value ?? throw new ArgumentNullException(nameof(value));
  34. }
  35. }
  36. //public ProtoOperationBehavior(OperationDescription operation, DataContractFormatAttribute dataContractFormat) : base(operation, dataContractFormat) { }
  37. /// <summary>
  38. /// Creates a protobuf serializer if possible (falling back to the default WCF serializer)
  39. /// </summary>
  40. public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
  41. {
  42. if (model == null) throw new InvalidOperationException("No Model instance has been assigned to the ProtoOperationBehavior");
  43. return XmlProtoSerializer.TryCreate(model, type) ?? base.CreateSerializer(type, name, ns, knownTypes);
  44. }
  45. }
  46. }
  47. #endif