ProtoEndpointBehavior.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #if FEAT_SERVICEMODEL && PLAT_XMLSERIALIZER
  2. using System.ServiceModel.Description;
  3. namespace ProtoBuf.ServiceModel
  4. {
  5. /// <summary>
  6. /// Behavior to swap out DatatContractSerilaizer with the XmlProtoSerializer for a given endpoint.
  7. /// <example>
  8. /// Add the following to the server and client app.config in the system.serviceModel section:
  9. /// <behaviors>
  10. /// <endpointBehaviors>
  11. /// <behavior name="ProtoBufBehaviorConfig">
  12. /// <ProtoBufSerialization/>
  13. /// </behavior>
  14. /// </endpointBehaviors>
  15. /// </behaviors>
  16. /// <extensions>
  17. /// <behaviorExtensions>
  18. /// <add name="ProtoBufSerialization" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=1.0.0.255, Culture=neutral, PublicKeyToken=257b51d87d2e4d67"/>
  19. /// </behaviorExtensions>
  20. /// </extensions>
  21. ///
  22. /// Configure your endpoints to have a behaviorConfiguration as follows:
  23. ///
  24. /// <service name="TK.Framework.Samples.ServiceModel.Contract.SampleService">
  25. /// <endpoint address="http://myhost:9003/SampleService" binding="basicHttpBinding" behaviorConfiguration="ProtoBufBehaviorConfig"
  26. /// bindingConfiguration="basicHttpBindingConfig" name="basicHttpProtoBuf" contract="ISampleServiceContract" />
  27. /// </service>
  28. /// <client>
  29. /// <endpoint address="http://myhost:9003/SampleService" binding="basicHttpBinding"
  30. /// bindingConfiguration="basicHttpBindingConfig" contract="ISampleServiceContract"
  31. /// name="BasicHttpProtoBufEndpoint" behaviorConfiguration="ProtoBufBehaviorConfig"/>
  32. /// </client>
  33. /// </example>
  34. /// </summary>
  35. public class ProtoEndpointBehavior : IEndpointBehavior
  36. {
  37. #region IEndpointBehavior Members
  38. void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
  39. {
  40. }
  41. void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
  42. {
  43. ReplaceDataContractSerializerOperationBehavior(endpoint);
  44. }
  45. void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
  46. {
  47. ReplaceDataContractSerializerOperationBehavior(endpoint);
  48. }
  49. void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
  50. {
  51. }
  52. private static void ReplaceDataContractSerializerOperationBehavior(ServiceEndpoint serviceEndpoint)
  53. {
  54. foreach (OperationDescription operationDescription in serviceEndpoint.Contract.Operations)
  55. {
  56. ReplaceDataContractSerializerOperationBehavior(operationDescription);
  57. }
  58. }
  59. private static void ReplaceDataContractSerializerOperationBehavior(OperationDescription description)
  60. {
  61. DataContractSerializerOperationBehavior dcsOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
  62. if (dcsOperationBehavior != null)
  63. {
  64. description.Behaviors.Remove(dcsOperationBehavior);
  65. ProtoOperationBehavior newBehavior = new ProtoOperationBehavior(description);
  66. newBehavior.MaxItemsInObjectGraph = dcsOperationBehavior.MaxItemsInObjectGraph;
  67. description.Behaviors.Add(newBehavior);
  68. }
  69. }
  70. #endregion
  71. }
  72. }
  73. #endif