背景:最近想要在C#项目里使用python的第三方库(numpy、scipy),直接引用的时候出现各种错误,听别人的建议就转用http调用的方法。
问题:但是我在采用与C#发布的服务相同的方法去调用spyne发布的服务的时候就各种报错。
希望各位大神能结合spyne的helloworld服务的例子给我讲一下引用的过程,非常感谢。
/*--------------------------引用界面及报错内容如下-------------------------------*/
这是添加服务引用的界面:
这是我引用的代码的片段:
实例化:HelloTest.ApplicationClient HW=new HelloTest.ApplicationClient();
引用:string[] RecMSG=HW.say_hello("World",3);
报错:"say_hello"方法没有采用两个参数的重载
已经解决了,谢谢各位。
以图片中Sum为例,
看下vs生成的say_hello的函数签名
大侠,能帮忙看看吗?这传入的是啥???(((φ(◎ロ◎;)φ)))
@淡水鱼徜徉在大海: 需要传一个HelloTest.say_hello对象
@jello chen: 能否结合Sum这个例子说明一下?
【另外一位大佬提到了先new 一个对象,比如:var hello=HelloTest.say_hello(),再设置hello的属性。针对Sum怎么设置属性呢,值如何传进Sum呢?大佬,你能码几行代码吗?小白很迷茫啊)
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); }
谢谢你的回答,容我慢慢消化一下你的例子。