/// <summary> /// Create WCF service proxy client /// </summary> /// <typeparam name="TServiceInterface">service contract interface</typeparam> /// <typeparam name="TBaseClass">channel base class that inherits from ClientBase</typeparam> /// <param name="beforeOperationCallback">delegate that will be executed before each operation</param> /// <param name="afterOperationCallback">delegate that will be executed after each operation</param> /// <param name="constructorParams">constructor parameters for TBaseClass</param> /// <returns>wcf proxy client with channel class inherited from <see cref="TBaseClass">TBaseClass</see></returns> public static TServiceInterface CreateInstance <TServiceInterface, TBaseClass>( Action <OperationContext> beforeOperationCallback, Action <OperationContext> afterOperationCallback, params object[] constructorParams) where TBaseClass : ClientBase <TServiceInterface> where TServiceInterface : class { if (!typeof(TServiceInterface).Attributes(typeof(ServiceContractAttribute)).Any()) { throw new ArgumentException("Provided service interface must have 'ServiceContract' attribute. Cannot create service proxy."); } var clientChannelType = WcfClientBaseTypeFactory <TServiceInterface, TBaseClass> .GenerateType(); var proxyClientType = WcfClientProxyTypeFactory <TServiceInterface, TBaseClass> .Create() .WithConstructorParameters(constructorParams) .GenerateType(); var clientInstance = (TServiceInterface)Activator.CreateInstance(proxyClientType, constructorParams); var initCallbacksMethod = proxyClientType.Method(WcfClientProxyTypeFactory <TServiceInterface, TBaseClass> .INIT_CALLBACKS_METHOD_NAME); if (initCallbacksMethod != null) { initCallbacksMethod.Invoke(clientInstance, new[] { beforeOperationCallback, afterOperationCallback }); } return(clientInstance); }
private WcfClientProxyTypeFactory() { m_ProxyClientChannelType = WcfClientBaseTypeFactory <TServiceInterface, TBaseClass> .GenerateType(); m_ClientChannelPropertyInfo = m_ProxyClientChannelType.Property(WcfClientBaseTypeFactory <TServiceInterface, TBaseClass> .ClientChannelPropertyName, Flags.AllMembers); m_ClientProxyTypeInfo = IL.NewType(CLIENT_PROXY_TYPE_NAME) .Implements <TServiceInterface>() .WithField(SERVICE_CLIENT_CONSTRUCTOR_PARAMETERS_FIELD_NAME, typeof(object[])) .WithField(BEFORE_OPERATION_CALLBACK_FIELD_NAME, typeof(Action <OperationContext>)) .WithField(AFTER_OPERATION_CALLBACK_FIELD_NAME, typeof(Action <OperationContext>)) .WithMethod(INIT_CALLBACKS_METHOD_NAME, m => m.WithParameter(typeof(Action <OperationContext>)) .WithParameter(typeof(Action <OperationContext>)) .Returns(typeof(void)) .Ldarg(0) .Ldarg(1) .Stfld(BEFORE_OPERATION_CALLBACK_FIELD_NAME) .Ldarg(0) .Ldarg(2) .Stfld(AFTER_OPERATION_CALLBACK_FIELD_NAME) .Ret()); m_ConstructorParameters = new object[] { }; ImplementConstructor(m_ClientProxyTypeInfo, new Type[0]); //implement empty constructor }