首页 新闻 赞助 找找看

C#如何引用由spyne实现webservice

0
悬赏园豆:50 [已解决问题] 解决于 2018-06-11 22:28

背景:最近想要在C#项目里使用python的第三方库(numpy、scipy),直接引用的时候出现各种错误,听别人的建议就转用http调用的方法。

问题:但是我在采用与C#发布的服务相同的方法去调用spyne发布的服务的时候就各种报错。

希望各位大神能结合spyne的helloworld服务的例子给我讲一下引用的过程,非常感谢。

/*--------------------------引用界面及报错内容如下-------------------------------*/

这是添加服务引用的界面:

这是我引用的代码的片段:

实例化:HelloTest.ApplicationClient HW=new HelloTest.ApplicationClient();

引用:string[] RecMSG=HW.say_hello("World",3);

报错:"say_hello"方法没有采用两个参数的重载

淡水鱼徜徉在大海的主页 淡水鱼徜徉在大海 | 初学一级 | 园豆:180
提问于:2018-06-10 21:15
< >
分享
最佳答案
0

已经解决了,谢谢各位。

  1. 先实例化一个对象,var <i示例名>=HelloTest.<函数名>();
  2. 再设置<实例>的属性;
  3. 最关键的是,调用函数、传入实例,后面要加上[.<函数名>Result]

以图片中Sum为例,

  • HelloTest.ApplicationClient HW=new HelloTest.ApplicationClient();//实例化
  • var Num = new HelloTest.Sum();  //构建对象,不能直接往Sum里面传参数
  • Num.x1 = 1;
  • Num.x2 = 2;
  • var Result = HW.Sum(Num).SumResult   //返回结果‘3’
淡水鱼徜徉在大海 | 初学一级 |园豆:180 | 2018-06-11 16:53
其他回答(2)
0

看下vs生成的say_hello的函数签名

收获园豆:30
jello chen | 园豆:7306 (大侠五级) | 2018-06-10 22:05

大侠,能帮忙看看吗?这传入的是啥???(((φ(◎ロ◎;)φ)))

支持(0) 反对(0) 淡水鱼徜徉在大海 | 园豆:180 (初学一级) | 2018-06-10 22:29

@淡水鱼徜徉在大海: 需要传一个HelloTest.say_hello对象

支持(0) 反对(0) jello chen | 园豆:7306 (大侠五级) | 2018-06-11 13:17

@jello chen: 能否结合Sum这个例子说明一下?

【另外一位大佬提到了先new 一个对象,比如:var hello=HelloTest.say_hello(),再设置hello的属性。针对Sum怎么设置属性呢,值如何传进Sum呢?大佬,你能码几行代码吗?小白很迷茫啊)

支持(0) 反对(0) 淡水鱼徜徉在大海 | 园豆:180 (初学一级) | 2018-06-11 14:58
0
    public abstract class QueryBase : UserControl, IReflactor
    {
        public const string SpaceName = "VMS.Views.MainPageItems.QueryItems.BussinessQuerys.";
        public static readonly Assembly CurrentAssembly = Assembly.GetExecutingAssembly();
        public static readonly string CurrentAssemblyName = CurrentAssembly.FullName;

        protected QueryBase()
        {
            ParamsQuery = new List<IParamQuery>();
            LoadingForm = new LoadingWindow();
            SetFunParamValues(CreateQueryParams());
            DownloadSucesss += OnDownloadData;
        }

        public string Data { get; private set; }

        /// <summary>
        ///     除去Host的相对Uri
        /// </summary>
        public abstract string Uri { get; }

        public abstract string Fun { get; }
        public List<IParamQuery> ParamsQuery { get; private set; }
        public LoadingWindow LoadingForm { get; private set; }

        public virtual string Caption
        {
            get { return GetType().Name; }
        }

        public virtual string FullName
        {
            get { return GetType().FullName; }
        }

        public event Action<string> DownloadSucesss;
        public virtual event Action<Exception> OnError;

        private void SetFunParamValues(IList<IParamQuery> paramsControls)
        {
            if (paramsControls == null) return; //throw new FormatException("查询参数不能为NULL");
            ParamsQuery.AddRange(paramsControls);
        }

        protected abstract IList<IParamQuery> CreateQueryParams();

        private string CreateBody()
        {
            var contentBody = new StringBuilder();
            contentBody.Append("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
            contentBody.Append(
                "<s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");

            contentBody.AppendFormat("<{0}    xmlns=\"http://tempuri.org/\">", Fun);
            foreach (IParamQuery funParam in ParamsQuery)
                contentBody.AppendFormat("<{0}>{1}</{0}>", funParam.PName, funParam.PValue);
            contentBody.AppendFormat("</{0}>", Fun);

            contentBody.Append("</s:Body>");
            contentBody.Append("</s:Envelope>");
            return contentBody.ToString();
        }

        protected abstract Exception Validate();

        protected virtual bool ValidateDealling(Exception ex)
        {
            if (ex == null)
            {
                return true;
            }
            if (OnError != null) OnError.Invoke(ex);
            return false;
        }

        public virtual void LoadData()
        {
            if (LoadingForm.IsDownloading) return;
            if (!ValidateDealling(Validate())) return;
            LoadingForm.Show();
            var webClient = new WebClient();
            webClient.UploadStringCompleted += (sender, args) =>
            {
                if (!LoadingForm.IsDownloading) return;
                if (args.Error != null)
                {
                    if (OnError != null) OnError.Invoke(new NetWorkException("网络错误", args.Error));
                }
                else if (args.Result == null)
                {
                    if (OnError != null) OnError.Invoke(new NetWorkException("数据错误", args.Error));
                }
                else
                {
                    try
                    {
                        XDocument doc = XDocument.Parse(args.Result);
                        Data = doc.Root.Value;
                        if (DownloadSucesss != null) DownloadSucesss.Invoke(Data);
                    }
                    catch (Exception ex)
                    {
                        OnError.Invoke(new NetWorkException("数据读取错误", ex));
                    }
                }
                LoadingForm.Close();
            };
            webClient.Headers["SOAPAction"] = string.Format("http://tempuri.org/{0}", Fun);
            webClient.Headers["Content-Type"] = "text/xml; charset=utf-8";
            string soapBody = CreateBody();
            webClient.UploadStringAsync(
                new Uri(string.Format("http://{0}:8001/{1}", WebServiceHelper.Host, Uri), UriKind.Absolute), soapBody);
        }

        protected abstract void OnDownloadData(string content);
    }

以前的silverlight实现soap请求,你完全可以自己调用一次,然后http模拟出来就行了。

文中异常明显是出错在调用对方接口 —— 参数不对。


    public abstract class QueryBase : UserControl, IReflactor    {        public const string SpaceName = "VMS.Views.MainPageItems.QueryItems.BussinessQuerys.";        public static readonly Assembly CurrentAssembly = Assembly.GetExecutingAssembly();        public static readonly string CurrentAssemblyName = CurrentAssembly.FullName;
        protected QueryBase()        {            ParamsQuery = new List<IParamQuery>();            LoadingForm = new LoadingWindow();            SetFunParamValues(CreateQueryParams());            DownloadSucesss += OnDownloadData;        }
        public string Data { get; private set; }
        /// <summary>        ///     除去Host的相对Uri        /// </summary>        public abstract string Uri { get; }
        public abstract string Fun { get; }        public List<IParamQuery> ParamsQuery { get; private set; }        public LoadingWindow LoadingForm { get; private set; }
        public virtual string Caption        {            get { return GetType().Name; }        }
        public virtual string FullName        {            get { return GetType().FullName; }        }
        public event Action<string> DownloadSucesss;        public virtual event Action<Exception> OnError;
        private void SetFunParamValues(IList<IParamQuery> paramsControls)        {            if (paramsControls == null) return; //throw new FormatException("查询参数不能为NULL");            ParamsQuery.AddRange(paramsControls);        }
        protected abstract IList<IParamQuery> CreateQueryParams();
        private string CreateBody()        {            var contentBody = new StringBuilder();            contentBody.Append("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");            contentBody.Append(                "<s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
            contentBody.AppendFormat("<{0}xmlns=\"http://tempuri.org/\">", Fun);            foreach (IParamQuery funParam in ParamsQuery)                contentBody.AppendFormat("<{0}>{1}</{0}>", funParam.PName, funParam.PValue);            contentBody.AppendFormat("</{0}>", Fun);
            contentBody.Append("</s:Body>");            contentBody.Append("</s:Envelope>");            return contentBody.ToString();        }
        protected abstract Exception Validate();
        protected virtual bool ValidateDealling(Exception ex)        {            if (ex == null)            {                return true;            }            if (OnError != null) OnError.Invoke(ex);            return false;        }
        public virtual void LoadData()        {            if (LoadingForm.IsDownloading) return;            if (!ValidateDealling(Validate())) return;            LoadingForm.Show();            var webClient = new WebClient();            webClient.UploadStringCompleted += (sender, args) =>            {                if (!LoadingForm.IsDownloading) return;                if (args.Error != null)                {                    if (OnError != null) OnError.Invoke(new NetWorkException("网络错误", args.Error));                }                else if (args.Result == null)                {                    if (OnError != null) OnError.Invoke(new NetWorkException("数据错误", args.Error));                }                else                {                    try                    {                        XDocument doc = XDocument.Parse(args.Result);                        Data = doc.Root.Value;                        if (DownloadSucesss != null) DownloadSucesss.Invoke(Data);                    }                    catch (Exception ex)                    {                        OnError.Invoke(new NetWorkException("数据读取错误", ex));                    }                }                LoadingForm.Close();            };            webClient.Headers["SOAPAction"] = string.Format("http://tempuri.org/{0}", Fun);            webClient.Headers["Content-Type"] = "text/xml; charset=utf-8";            string soapBody = CreateBody();            webClient.UploadStringAsync(                new Uri(string.Format("http://{0}:8001/{1}", WebServiceHelper.Host, Uri), UriKind.Absolute), soapBody);        }
        protected abstract void OnDownloadData(string content);    }

收获园豆:20
花飘水流兮 | 园豆:13560 (专家六级) | 2018-06-11 15:53

谢谢你的回答,容我慢慢消化一下你的例子。

支持(0) 反对(0) 淡水鱼徜徉在大海 | 园豆:180 (初学一级) | 2018-06-11 16:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册