2
0

HttpRemotingHandlerFactory.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory
  3. //
  4. // Authors:
  5. // Martin Willemoes Hansen ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // (C) 2003 Martin Willemoes Hansen
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Web;
  31. using System.IO;
  32. using System.Runtime.Remoting;
  33. using System.Runtime.Remoting.Channels;
  34. namespace System.Runtime.Remoting.Channels.Http
  35. {
  36. public class HttpRemotingHandlerFactory : IHttpHandlerFactory
  37. {
  38. static bool webConfigLoaded = false;
  39. static HttpServerTransportSink transportSink = null;
  40. public HttpRemotingHandlerFactory ()
  41. {
  42. }
  43. public IHttpHandler GetHandler (HttpContext context,
  44. string verb,
  45. string url,
  46. string filePath)
  47. {
  48. if (!webConfigLoaded)
  49. ConfigureHttpChannel (context);
  50. return new HttpRemotingHandler (transportSink);
  51. }
  52. void ConfigureHttpChannel (HttpContext context)
  53. {
  54. lock (GetType())
  55. {
  56. if (webConfigLoaded) return;
  57. string appConfig = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
  58. if (File.Exists (appConfig))
  59. RemotingConfiguration.Configure (appConfig);
  60. // Look for a channel that wants to receive http request
  61. IChannelReceiverHook chook = null;
  62. foreach (IChannel channel in ChannelServices.RegisteredChannels)
  63. {
  64. chook = channel as IChannelReceiverHook;
  65. if (chook == null) continue;
  66. if (chook.ChannelScheme != "http")
  67. throw new RemotingException ("Only http channels are allowed when hosting remoting objects in a web server");
  68. if (!chook.WantsToListen)
  69. {
  70. chook = null;
  71. continue;
  72. }
  73. //found chook
  74. break;
  75. }
  76. if (chook == null)
  77. {
  78. HttpChannel chan = new HttpChannel();
  79. ChannelServices.RegisterChannel(chan);
  80. chook = chan;
  81. }
  82. // Register the uri for the channel. The channel uri includes the scheme, the
  83. // host and the application path
  84. string channelUrl = context.Request.Url.GetLeftPart(UriPartial.Authority);
  85. channelUrl += context.Request.ApplicationPath;
  86. chook.AddHookChannelUri (channelUrl);
  87. transportSink = new HttpServerTransportSink (chook.ChannelSinkChain, null);
  88. webConfigLoaded = true;
  89. }
  90. }
  91. public void ReleaseHandler (IHttpHandler handler)
  92. {
  93. }
  94. }
  95. }