コード例 #1
0
 private void cboEndPoint_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (txtServiceURL.Text.Trim().Length > 0)
     {
         try
         {
             cboMethodName.Items.Clear();
             if (factoryCache == null || factoryCache.WsdlUri != txtServiceURL.Text)
             {
                 factoryCache = new DynamicProxyFactory(txtServiceURL.Text);
             }
             ServiceEndpoint endpoint = (from ep in factoryCache.Endpoints
                                         where ep.Name == cboEndPoint.Text
                                         select ep).FirstOrDefault();
             if (endpoint != null)
             {
                 string       contractName = endpoint.Contract.Name;
                 DynamicProxy proxy        = factoryCache.CreateProxy(contractName);
                 Type         proxyType    = proxy.ProxyType;
                 foreach (OperationDescription operation in endpoint.Contract.Operations)
                 {
                     cboMethodName.Items.Add(operation.Name);
                 }
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
コード例 #2
0
 private void cboMethodName_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (txtServiceURL.Text.Trim().Length > 0 && cboEndPoint.Text.Trim().Length > 0 && cboMethodName.Text.Trim().Length > 0)
     {
         try
         {
             lblParameterCount.Text      = "Parameters info...";
             lblParameterCount.BackColor = SystemColors.Control;
             if (factoryCache == null || factoryCache.WsdlUri != txtServiceURL.Text)
             {
                 factoryCache = new DynamicProxyFactory(txtServiceURL.Text);
             }
             ServiceEndpoint endpoint = (from ep in factoryCache.Endpoints
                                         where ep.Name.ToLower() == cboEndPoint.Text.ToLower()
                                         select ep).FirstOrDefault();
             if (endpoint != null)
             {
                 string               contractName = endpoint.Contract.Name;
                 DynamicProxy         proxy        = factoryCache.CreateProxy(contractName);
                 Type                 proxyType    = proxy.ProxyType;
                 OperationDescription operation    = (from OperationDescription m in endpoint.Contract.Operations
                                                      where m.Name.ToLower() == cboMethodName.Text.ToLower()
                                                      select m).FirstOrDefault();
                 if (operation != null)
                 {
                     System.Reflection.MethodInfo method = (from m in proxyType.GetMethods()
                                                            where m.Name == operation.Name
                                                            select m).FirstOrDefault();
                     lblParameterCount.Text = "Count: " + method.GetParameters().Length.ToString() + " ";
                     foreach (System.Reflection.ParameterInfo parInfo in method.GetParameters())
                     {
                         if (!parInfo.ParameterType.IsValueType && parInfo.ParameterType.Name != "String" && parInfo.ParameterType.Name != "Object")
                         {
                             lblParameterCount.BackColor = Color.Yellow;
                         }
                         lblParameterCount.Text += string.Format("<{0}:{1}>,", parInfo.Name, parInfo.ParameterType.Name);
                     }
                     lblParameterCount.Text = lblParameterCount.Text.TrimEnd(',');
                 }
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
コード例 #3
0
        public object RunMethod()
        {
            object obj = null;

            if (proxy == null && serviceBaseURL.Length > 0 && serviceBindingName.Length > 0)
            {
                DynamicProxyFactory factory  = new DynamicProxyFactory(serviceBaseURL);
                ServiceEndpoint     endpoint = (from ep in factory.Endpoints
                                                where ep.Name == serviceBindingName
                                                select ep).FirstOrDefault();
                proxy = factory.CreateProxy(endpoint.Contract.Name);
                //attempt to load data types so parameters can be formatter properly...
                parameterTypes = new List <System.Reflection.ParameterInfo>();
                OperationDescription operation = (from OperationDescription m in endpoint.Contract.Operations
                                                  where m.Name.ToLower() == methodName.ToLower()
                                                  select m).FirstOrDefault();
                if (operation != null) //get method
                {
                    Type proxyType = proxy.ProxyType;
                    System.Reflection.MethodInfo method = (from m in proxyType.GetMethods()
                                                           where m.Name == operation.Name
                                                           select m).FirstOrDefault();
                    foreach (System.Reflection.ParameterInfo parInfo in method.GetParameters())
                    {
                        parameterTypes.Add(parInfo);
                    }
                }
            }
            else if (proxy == null)
            {
                throw new Exception("Web service proxy could not be created! Check service URL or binding details");
            }
            try
            {
                if (Parameters.Count > 0)
                {
                    List <object> runParameters = new List <object>();
                    for (int i = 0; i < Parameters.Count; i++)
                    {
                        if (i < parameterTypes.Count)
                        {
                            Type pt = parameterTypes[i].ParameterType;

                            switch (pt.Name)
                            {
                            case "Object":
                                runParameters.Add(Parameters[i]);
                                break;

                            //    case "Int32":
                            //        runParameters.Add(int.Parse(Parameters[i]));
                            //        break;
                            //    case "Int16":
                            //        runParameters.Add(Int16.Parse(Parameters[i]));
                            //        break;
                            //    case "Double":
                            //        runParameters.Add(Double.Parse(Parameters[i]));
                            //        break;
                            //    case "Boolean":
                            //        runParameters.Add(bool.Parse(Parameters[i]));
                            //        break;
                            default:
                                var converter = System.ComponentModel.TypeDescriptor.GetConverter(pt);
                                runParameters.Add(converter.ConvertFrom(Parameters[i]));
                                break;
                            }
                        }
                        else
                        {
                            runParameters.Add(Parameters[i]);
                        }
                    }
                    obj = proxy.CallMethod(methodName, runParameters.ToArray());
                }
                else
                {
                    obj = proxy.CallMethod(methodName, null);
                }
            }
            catch (Exception ex)
            {
                proxy = null; //set null so it can be retried from scratch
                if (ex.Message.Contains("There was an error downloading"))
                {
                    throw new Exception("Specified web service invalid or not available!", ex);
                }
                if (ex.Message.Contains("Method") && ex.Message.Contains("not found"))
                {
                    throw new Exception("Method '" + methodName + "' not found or parameters invalid!");
                }
                else
                {
                    throw;
                }
            }
            return(obj);
        }