ProtoOperationBehavior.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #if FEAT_SERVICEMODEL && PLAT_XMLSERIALIZER && !NO_GENERICS
  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. /// The type-model that should be used with this behaviour
  18. /// </summary>
  19. public TypeModel Model
  20. {
  21. get { return model; }
  22. set {
  23. if (value == null) throw new ArgumentNullException(nameof(value));
  24. model = value;
  25. }
  26. }
  27. /// <summary>
  28. /// Create a new ProtoOperationBehavior instance
  29. /// </summary>
  30. public ProtoOperationBehavior(OperationDescription operation) : base(operation)
  31. {
  32. #if !NO_RUNTIME
  33. model = RuntimeTypeModel.Default;
  34. #endif
  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, System.Xml.XmlDictionaryString name, System.Xml.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