private static void DynamicServiceInvoke(string wsdlUri, string operationName, object[] operationParameters, HeaderData headerData) { var factory = new DynamicProxyFactory(wsdlUri); var serviceContract = factory.Contracts.FirstOrDefault(); // create the DynamicProxy for the contract and perform operations #if DEBUG Console.WriteLine("Creating DynamicProxy to GenericJobScheduler Service"); #endif if (serviceContract != null) { DynamicProxy dynamicProxy = factory.CreateProxy(serviceContract.Name); var innerChannel = dynamicProxy.GetProperty("InnerChannel") as IClientChannel; if (innerChannel != null) { using (new OperationContextScope(innerChannel)) { OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(headerData.HeaderName, headerData.HeaderNamespace, headerData.DataAsXmlString)); var result = dynamicProxy.CallMethod(operationName, operationParameters); } } } }
public static void Test() { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(SkipCertValidation); string baseAddress = "https://" + Environment.MachineName + ":8888/Service"; ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); host.AddServiceEndpoint(typeof(ITest), GetBinding(), ""); host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpsGetEnabled = true }); host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom; host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new MyPasswordValidator(); host.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.UseAspNetRoles; host.Open(); Console.WriteLine("Host opened"); DynamicProxyFactory factory = new DynamicProxyFactory(baseAddress + "?wsdl"); DynamicProxy proxy = factory.CreateProxy("ITest"); ClientCredentials credentials = proxy.GetProperty("ClientCredentials") as ClientCredentials; credentials.UserName.UserName = "******"; credentials.UserName.Password = "******"; Console.WriteLine(proxy.CallMethod("Hello")); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); }
/// <summary> /// This method is a run-time method executed dtsexec.exe /// </summary> /// <param name="connections"></param> /// <param name="variableDispenser"></param> /// <param name="componentEvents"></param> /// <param name="log"></param> /// <param name="transaction"></param> /// <returns></returns> public override DTSExecResult Execute(Connections connections, VariableDispenser variableDispenser, IDTSComponentEvents componentEvents, IDTSLogging log, object transaction) { bool refire = false; componentEvents.FireInformation(0, "SSISWCFTask", "Prepare variables", string.Empty, 0, ref refire); GetNeededVariables(variableDispenser, componentEvents); try { componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Initialize the WCF Service: {0}", EvaluateExpression(ServiceUrl, variableDispenser)), string.Empty, 0, ref refire); var dynamicProxyFactory = new DynamicProxyFactory(EvaluateExpression(ServiceUrl, variableDispenser).ToString()); int count = 0; foreach (ServiceEndpoint endpoint in dynamicProxyFactory.Endpoints.Where(endPoint => endPoint.Contract.Name == EvaluateExpression(ServiceContract, variableDispenser).ToString())) { componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Service Endpoint[{0}]", count++), string.Empty, 0, ref refire); componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Address: {0}", endpoint.Address), string.Empty, 0, ref refire); componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Contract: {0}", endpoint.Contract.Name), string.Empty, 0, ref refire); componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Binding: {0}", endpoint.Binding.Name), string.Empty, 0, ref refire); } componentEvents.FireInformation(0, "SSISWCFTask", string.Format("InvokeRemoteMethod: {0} => {1}", EvaluateExpression(ServiceContract, variableDispenser), EvaluateExpression(OperationContract, variableDispenser)), string.Empty, 0, ref refire); componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Creating DynamicProxy to {0} ServiceContract", EvaluateExpression(ServiceContract, variableDispenser)), string.Empty, 0, ref refire); DynamicProxy dynamicProxy = dynamicProxyFactory.CreateProxy(EvaluateExpression(ServiceContract, variableDispenser).ToString()); if (dynamicProxy == null) { throw new Exception("Cannot create the proxy"); } object result = null; componentEvents.FireInformation(0, "SSISWCFTask", "Creating the Client Channel", string.Empty, 0, ref refire); using (IClientChannel innerChannel = dynamicProxy.GetProperty("InnerChannel") as IClientChannel) { if (innerChannel != null) { using (new OperationContextScope(innerChannel)) { componentEvents.FireInformation(0, "SSISWCFTask", "Creating the Message Header", string.Empty, 0, ref refire); OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SSISDynamicWCF", "", Guid.NewGuid().ToString())); componentEvents.FireInformation(0, "SSISWCFTask", "The used headers:", string.Empty, 0, ref refire); foreach (var header in ((MappingHeaders)MappingHeaders)) { var headerValue = Convert.ChangeType(EvaluateExpression(header.Value, variableDispenser).ToString(), Type.GetType(header.Type)); componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Name: {0} | Type: {1} | Value: {2}", header.Name, header.Type, headerValue), string.Empty, 0, ref refire); OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(header.Name, "", headerValue)); } componentEvents.FireInformation(0, "SSISWCFTask", "The used params:", string.Empty, 0, ref refire); foreach (var param in ((MappingParams)MappingParams)) { componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Name: {0} | Type: {1} | Value: {2}", param.Name, param.Type, Convert.ChangeType(EvaluateExpression(param.Value, variableDispenser).ToString(), Type.GetType(param.Type))), string.Empty, 0, ref refire); } componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Call [{0}] OperationContract with parameters:", EvaluateExpression(OperationContract, variableDispenser)), string.Empty, 0, ref refire); int countParam = ((MappingParams)MappingParams).Count; var objParams = new object[countParam]; for (int i = 0; i < countParam; i++) { objParams[i] = Convert.ChangeType(EvaluateExpression(((MappingParams)MappingParams)[i].Value, variableDispenser).ToString(), Type.GetType(((MappingParams)MappingParams)[i].Type)); } result = dynamicProxy.CallMethod(EvaluateExpression(OperationContract, variableDispenser).ToString(), objParams); //new object[] // { // (from parameters in ((MappingParams)MappingParams) // select Convert.ChangeType(EvaluateExpression(parameters.Value, variableDispenser).ToString(), Type.GetType(parameters.Type))).ToArray() // }); } } else { componentEvents.FireInformation(0, "SSISWCFTask", "Cannot create the Inner Channel", string.Empty, 0, ref refire); throw new Exception("Cannot Create the Inner Channel"); } } if (result != null) { if (IsValueReturned == Keys.TRUE) { componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Get the Returned Value to: {0}", ReturnedValue), string.Empty, 0, ref refire); string val = ReturnedValue.Split(new[] { "::" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim(); componentEvents.FireInformation(0, "SSISWCFTask", string.Format("Get the Returned Value to {0} and convert to {1}", val.Substring(0, val.Length - 1), _vars[val.Substring(0, val.Length - 1)].DataType), string.Empty, 0, ref refire); _vars[val.Substring(0, val.Length - 1)].Value = Convert.ChangeType(result, _vars[val.Substring(0, val.Length - 1)].DataType); componentEvents.FireInformation(0, "SSISWCFTask", string.Format("The String Result is {0} ", _vars[val.Substring(0, val.Length - 1)].Value), string.Empty, 0, ref refire); } else { componentEvents.FireInformation(0, "SSISWCFTask", "Execution without return or no associated return variable", string.Empty, 0, ref refire); } } } catch (Exception ex) { componentEvents.FireError(0, "SSISWCFTask", string.Format("Problem: {0}", ex.Message + "\n" + ex.StackTrace), "", 0); } finally { if (_vars.Locked) { _vars.Unlock(); } } return(base.Execute(connections, variableDispenser, componentEvents, log, transaction)); }
/// <summary> /// Se encarga de invocar un metodo de un webservice /// </summary> /// <param name="strMetodo"></param> /// <param name="objParametros"></param> /// <returns></returns> private object InvocarWebService(string strMetodo, object[] objParametros) { var objRetorno = new object(); try { //Crear Factory DynamicProxyFactory factory = new DynamicProxyFactory(this.strUrl, this.strUsuario, this.strPassword); //Crear Bindings switch (factory.Bindings.ElementAt(0).GetType().Name) { case "WSHttpBinding": WSHttpBinding bdWS = (WSHttpBinding)factory.Bindings.ElementAt(0); if (bdWS.Scheme.ToLower().Equals("http")) { bdWS.Security.Mode = SecurityMode.None; bdWS.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; } else { bdWS.Security.Mode = SecurityMode.Transport; bdWS.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; } break; case "BasicHttpBinding": BasicHttpBinding bdBasic = (BasicHttpBinding)factory.Bindings.ElementAt(0); if (bdBasic.Scheme.ToLower().Equals("http")) { bdBasic.Security.Mode = BasicHttpSecurityMode.None; bdBasic.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; } else { bdBasic.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; bdBasic.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; } break; } string strContratoWebService = string.Empty; foreach (ContractDescription contract in factory.Contracts) { strContratoWebService = contract.Name; break; } //Crear Proxy DynamicProxy proxyAutenticacion = factory.CreateProxy(strContratoWebService); //Asignar credenciales de acceso al servicio web ClientCredentials credentials = proxyAutenticacion.GetProperty("ClientCredentials") as ClientCredentials; credentials.UserName.UserName = this.strUsuario; credentials.UserName.Password = this.strPassword; //Retornar dato por parte del servicio web objRetorno = proxyAutenticacion.CallMethod <object>(strMetodo, objParametros); } catch (Exception ex) { throw new Exception(ex.Message, ex.InnerException); } return(objRetorno); }