WebScriptEnablingBehavior.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 baseUriString = endpoint.ListenUri.ToString ();
  93. var uri = new Uri (String.Concat (baseUriString, baseUriString [baseUriString.Length - 1] == '/' ? String.Empty : "/", subPath));
  94. var listener = endpoint.Binding.BuildChannelListener<IReplyChannel> (uri);
  95. var cd = new ChannelDispatcher (listener, String.Empty);
  96. cd.MessageVersion = MessageVersion.None;
  97. cd.Endpoints.Add (new EndpointDispatcher (new EndpointAddress (uri), "InteropScriptService", String.Empty)
  98. { ContractFilter = new MatchAllMessageFilter () });
  99. var dr = cd.Endpoints [0].DispatchRuntime;
  100. var dop = new DispatchOperation (dr, "Get", "*", "*");
  101. dop.DeserializeRequest = false;
  102. dop.SerializeReply = false;
  103. dop.Invoker = new DummyInvoker (instance);
  104. dr.UnhandledDispatchOperation = dop;
  105. dr.InstanceContextProvider = new SingletonInstanceContextProvider (new InstanceContext (cdOrg.Host, instance));
  106. var host = ed.ChannelDispatcher.Host;
  107. host.ChannelDispatchers.Add (cd);
  108. }
  109. class DummyInvoker : IOperationInvoker
  110. {
  111. InteropScriptService instance;
  112. public DummyInvoker (InteropScriptService instance)
  113. {
  114. this.instance = instance;
  115. }
  116. public object [] AllocateInputs ()
  117. {
  118. return new object [0];
  119. }
  120. public object Invoke (object instance, object [] inputs, out object [] outputs)
  121. {
  122. outputs = new object [0];
  123. var msg = Message.CreateMessage (MessageVersion.None, "*", (object) null);
  124. var hp = new HttpResponseMessageProperty ();
  125. hp.Headers ["Content-Type"] = "text/javascript";
  126. msg.Properties.Add (HttpResponseMessageProperty.Name, hp);
  127. msg.Properties.Add (WebMessageEncoder.ScriptPropertyName, this.instance.Get ());
  128. return msg;
  129. }
  130. public IAsyncResult InvokeBegin (object instance, object[] inputs, AsyncCallback callback, object state)
  131. {
  132. throw new NotSupportedException ();
  133. }
  134. public object InvokeEnd (object instance, out object [] outputs, IAsyncResult result)
  135. {
  136. throw new NotSupportedException ();
  137. }
  138. public bool IsSynchronous {
  139. get { return true; }
  140. }
  141. }
  142. protected override QueryStringConverter GetQueryStringConverter (OperationDescription operationDescription)
  143. {
  144. return new JsonQueryStringConverter () { CustomWrapperName = "d"};
  145. }
  146. [MonoTODO ("add non-XmlSerializer-ness check (but where?)")]
  147. public override void Validate (ServiceEndpoint endpoint)
  148. {
  149. if (endpoint == null)
  150. throw new ArgumentNullException ("endpoint");
  151. ValidateBinding (endpoint);
  152. foreach (var od in endpoint.Contract.Operations) {
  153. var wai = od.GetWebAttributeInfo ();
  154. if (wai.UriTemplate != null)
  155. throw new InvalidOperationException ("UriTemplate must not be used with WebScriptEnablingBehavior");
  156. var wia = od.Behaviors.Find<WebInvokeAttribute> ();
  157. if (wia != null) {
  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. var style = wai != null && wai.IsBodyStyleSetExplicitly ? wai.BodyStyle : DefaultBodyStyle;
  167. if (style != WebMessageBodyStyle.WrappedRequest)
  168. throw new NotSupportedException (String.Format ("WebScriptEnableBehavior only allows WrappedRequest body style, but operation '{0}' uses {1}.", od.Name, style));
  169. }
  170. }
  171. }
  172. }