TearOffProxy.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.ComIntegration
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Reflection;
  9. using System.Runtime;
  10. using System.Runtime.InteropServices;
  11. using System.Runtime.Remoting;
  12. using System.Runtime.Remoting.Messaging;
  13. using System.Runtime.Remoting.Proxies;
  14. using System.ServiceModel;
  15. class TearOffProxy : RealProxy, IDisposable
  16. {
  17. ICreateServiceChannel serviceChannelCreator;
  18. Dictionary<MethodBase, MethodBase> baseTypeToInterfaceMethod;
  19. internal TearOffProxy(ICreateServiceChannel serviceChannelCreator, Type proxiedType)
  20. : base(proxiedType)
  21. {
  22. if (serviceChannelCreator == null)
  23. {
  24. throw Fx.AssertAndThrow("ServiceChannelCreator cannot be null");
  25. }
  26. this.serviceChannelCreator = serviceChannelCreator;
  27. baseTypeToInterfaceMethod = new Dictionary<MethodBase, MethodBase>();
  28. }
  29. public override IMessage Invoke(IMessage message)
  30. {
  31. RealProxy delegatingProxy = null;
  32. IMethodCallMessage msg = message as IMethodCallMessage;
  33. try
  34. {
  35. delegatingProxy = serviceChannelCreator.CreateChannel();
  36. }
  37. catch (Exception e)
  38. {
  39. if (Fx.IsFatal(e))
  40. throw;
  41. return new ReturnMessage(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(e.GetBaseException().Message, Marshal.GetHRForException(e.GetBaseException()))), msg);
  42. }
  43. MethodBase typeMethod = msg.MethodBase;
  44. IRemotingTypeInfo typeInfo = delegatingProxy as IRemotingTypeInfo;
  45. if (typeInfo == null)
  46. {
  47. throw Fx.AssertAndThrow("Type Info cannot be null");
  48. }
  49. if (typeInfo.CanCastTo(typeMethod.DeclaringType, null))
  50. {
  51. IMessage msgReturned = delegatingProxy.Invoke(message);
  52. ReturnMessage returnMsg = msgReturned as ReturnMessage;
  53. if ((returnMsg == null) || (returnMsg.Exception == null))
  54. return msgReturned;
  55. else
  56. return new ReturnMessage(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(returnMsg.Exception.GetBaseException().Message, Marshal.GetHRForException(returnMsg.Exception.GetBaseException()))), msg);
  57. }
  58. else
  59. {
  60. return new ReturnMessage(DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(SR.GetString(SR.OperationNotFound, typeMethod.Name), HR.DISP_E_UNKNOWNNAME)), msg);
  61. }
  62. }
  63. void IDisposable.Dispose()
  64. {
  65. serviceChannelCreator = null;
  66. }
  67. }
  68. }