WebScriptEnablingBehavior.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // WebScriptEnablingBehavior.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.ServiceModel;
  30. using System.ServiceModel.Channels;
  31. using System.ServiceModel.Dispatcher;
  32. using System.ServiceModel.Web;
  33. using System.Web.Script.Services;
  34. namespace System.ServiceModel.Description
  35. {
  36. [ServiceContract (Namespace = "")]
  37. internal class InteropScriptService
  38. {
  39. Type type;
  40. string path;
  41. bool debug;
  42. public InteropScriptService (Type type, string path, bool debug)
  43. {
  44. this.type = type;
  45. this.path = path;
  46. this.debug = debug;
  47. }
  48. [WebGet (UriTemplate = "*")]
  49. [OperationContract]
  50. public string Get ()
  51. {
  52. return ProxyGenerator.GetClientProxyScript (type, path, debug);
  53. }
  54. }
  55. public sealed class WebScriptEnablingBehavior : WebHttpBehavior
  56. {
  57. public WebScriptEnablingBehavior ()
  58. {
  59. DefaultBodyStyle = WebMessageBodyStyle.WrappedRequest;
  60. DefaultOutgoingRequestFormat = WebMessageFormat.Json;
  61. DefaultOutgoingResponseFormat = WebMessageFormat.Json;
  62. }
  63. public override WebMessageBodyStyle DefaultBodyStyle { get; set; }
  64. public override WebMessageFormat DefaultOutgoingRequestFormat { get; set; }
  65. public override WebMessageFormat DefaultOutgoingResponseFormat { get; set; }
  66. [MonoTODO]
  67. protected override void AddClientErrorInspector (ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  68. {
  69. base.AddClientErrorInspector (endpoint, clientRuntime);
  70. }
  71. [MonoTODO]
  72. protected override void AddServerErrorHandlers (ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
  73. {
  74. base.AddServerErrorHandlers (endpoint, endpointDispatcher);
  75. }
  76. [MonoTODO]
  77. public override void ApplyClientBehavior (ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  78. {
  79. base.ApplyClientBehavior (endpoint, clientRuntime);
  80. }
  81. public override void ApplyDispatchBehavior (ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
  82. {
  83. base.ApplyDispatchBehavior (endpoint, endpointDispatcher);
  84. // doing similar to ServiceMetadataExtension
  85. BuildScriptDispatcher (endpoint, endpointDispatcher, "/js", false);
  86. BuildScriptDispatcher (endpoint, endpointDispatcher, "/jsdebug", true);
  87. }
  88. void BuildScriptDispatcher (ServiceEndpoint endpoint, EndpointDispatcher ed, string subPath, bool debug)
  89. {
  90. var instance = new InteropScriptService (endpoint.Contract.ContractType, endpoint.Address.Uri.ToString (), debug);
  91. var cdOrg = ed.ChannelDispatcher;
  92. var uri = new Uri (cdOrg.Listener.Uri, subPath);
  93. var cd = new ChannelDispatcher (endpoint.Binding.BuildChannelListener<IReplyChannel> (uri), String.Empty);
  94. cd.MessageVersion = MessageVersion.None;
  95. cd.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "InteropScriptService", String.Empty));
  96. var dr = cd.Endpoints [0].DispatchRuntime;
  97. var dop = new DispatchOperation (dr, "Get", "*", "*");
  98. dop.DeserializeRequest = false;
  99. dop.SerializeReply = false;
  100. dop.Invoker = new DummyInvoker (instance);
  101. dr.UnhandledDispatchOperation = dop;
  102. dr.InstanceContextProvider = new SingletonInstanceContextProvider (new InstanceContext (cdOrg.Host, instance));
  103. var host = ed.ChannelDispatcher.Host;
  104. host.ChannelDispatchers.Add (cd);
  105. }
  106. class DummyInvoker : IOperationInvoker
  107. {
  108. InteropScriptService instance;
  109. public DummyInvoker (InteropScriptService instance)
  110. {
  111. this.instance = instance;
  112. }
  113. public object [] AllocateInputs ()
  114. {
  115. return new object [0];
  116. }
  117. public object Invoke (object instance, object [] inputs, out object [] outputs)
  118. {
  119. outputs = new object [0];
  120. var msg = Message.CreateMessage (MessageVersion.None, "*", (object) null);
  121. var hp = new HttpResponseMessageProperty ();
  122. hp.Headers ["Content-Type"] = "text/javascript";
  123. msg.Properties.Add (HttpResponseMessageProperty.Name, hp);
  124. msg.Properties.Add (WebMessageEncoder.ScriptPropertyName, this.instance.Get ());
  125. return msg;
  126. }
  127. public IAsyncResult InvokeBegin (object instance, object[] inputs, AsyncCallback callback, object state)
  128. {
  129. throw new NotSupportedException ();
  130. }
  131. public object InvokeEnd (object instance, out object [] outputs, IAsyncResult result)
  132. {
  133. throw new NotSupportedException ();
  134. }
  135. public bool IsSynchronous {
  136. get { return true; }
  137. }
  138. }
  139. [MonoTODO]
  140. protected override QueryStringConverter GetQueryStringConverter (OperationDescription operationDescription)
  141. {
  142. return base.GetQueryStringConverter (operationDescription);
  143. }
  144. [MonoTODO ("add non-XmlSerializer-ness check (but where?)")]
  145. public override void Validate (ServiceEndpoint endpoint)
  146. {
  147. if (endpoint == null)
  148. throw new ArgumentNullException ("endpoint");
  149. ValidateBinding (endpoint);
  150. foreach (var od in endpoint.Contract.Operations) {
  151. var wga = od.Behaviors.Find<WebGetAttribute> ();
  152. if (wga != null && wga.UriTemplate != null)
  153. throw new InvalidOperationException ("UriTemplate must not be used with WebScriptEnablingBehavior");
  154. var wia = od.Behaviors.Find<WebInvokeAttribute> ();
  155. if (wia != null) {
  156. if (wia.UriTemplate != null)
  157. throw new InvalidOperationException ("UriTemplate must not be used with WebScriptEnablingBehavior");
  158. switch (wia.Method.ToUpper ()) {
  159. case "GET":
  160. case "POST":
  161. break;
  162. default:
  163. throw new InvalidOperationException ("Only GET and POST HTTP methods are valid used for WebScriptEnablingBehavior");
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }