日期:2014-05-17  浏览次数:20875 次

动态调用webservice时提示找不到函数
我需要动态可修改webservice地址的方式调用一个webservice
目前是引用http://121.52.210.226/Services/EcgWebService.asmx这个webservice
我所用的代理类是下面贴的这个类,namespace需要自己另外修改
引用代理类的方式如下
C# code

WebServiceInvoker invoker;
invoker = new WebServiceInvoker(new Uri(webservice));
object[] args = new object[] { 
                (object)ecgId, (object)data, (object)sampleRate, (object)adBit, (object)adRange, 
                (object)uVpb, (object)amp, (object)seconds, (object)filter, (object)deviceSn, 
                (object)clientType, (object)alias, (object)patientName, (object)patientAge,(object)patientSex };
bool result = invoker.InvokeMethod<bool>("EcgWebService", "SaveEcgData", args);
[code=C#]
现在碰到一个奇怪的错误,我之前的一个程序调用这个webservice的SaveEcgData函数是正常的。现在手头的程序调用webservice的SaveEcgData函数时,抛出异常,提示找不到这个方法。而我用代理类中的public List<string> EnumerateServiceMethods(string serviceName)方法列举函数列表时列表里是肯定包含有SaveEcgData函数的。

碰到这个问题很奇怪,我很难解释原因,请问这个找不到方法,是因为什么原因?


代理类
[code=C#]
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Xml;
using System.Web.Services.Description;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.CodeDom;

namespace TestClient
{
    class WebServiceInvoker
    {
        Dictionary<string, Type> availableTypes;

        /// <summary>
        /// Text description of the available services within this web service.
        /// </summary>
        public List<string> AvailableServices
        {
            get { return this.services; }
        }

        /// <summary>
        /// Creates the service invoker using the specified web service.
        /// </summary>
        /// <param name="webServiceUri"></param>
        public WebServiceInvoker(Uri webServiceUri)
        {
            this.services = new List<string>(); // available services
            this.availableTypes = new Dictionary<string, Type>(); // available types

            // create an assembly from the web service description
            this.webServiceAssembly = BuildAssemblyFromWSDL(webServiceUri);

            // see what service types are available
            Type[] types = this.webServiceAssembly.GetExportedTypes();

            // and save them
            foreach (Type type in types)
            {
                services.Add(type.FullName);
                availableTypes.Add(type.FullName, type);
            }
        }

        /// <summary>
        /// Gets a list of all methods available for the specified service.
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public List<string> EnumerateServiceMethods(string serviceName)
        {
            List<string> methods = new List<string>();

            if (!this.availableTypes.ContainsKey(serviceName))
                throw new Exception("Service Not Available");
            else
            {
                Type type = this.availableTypes[serviceName];

                // only find methods of this object type (the one we generated)
                // we don't want inherited members (this type inherited from SoapHttpClientProtocol)
                foreach (MethodInfo minfo in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
                    methods.Add(minfo.Name);

                return methods;
            }
        }

        /// <summary>
        /// Invokes the specified method of the named service.
        /// </summary>
        /// <typeparam name="T">The expected return type.</typeparam>
        /// <param name="serviceName">The name of the service to use.</param>