2015-10-21 47 views
0

我是ASP.NET新手。我想製作一個包含方法的一個簡單的Web服務:ASMX ASP.NET,奇怪的返回類型按服務方法

下面的代碼:

[WebService(Namespace = "http://cstest.pl/PaintService.asmx/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    public class PaintService : WebService 
    { 
     private Random r = new Random(); 

     [WebMethod(Description =("Something"))] 
     public Nullable<Point> StartDraw(int startX, int startY, int width, int height) 
     { 
      try 
      { 
       int X = r.Next(startX, width); 
       int Y = r.Next(startY + height); 
       return (new Point(X, Y)); 
      } 
      catch (ArgumentOutOfRangeException e) 
      { 
       Console.WriteLine(e.Message); 
       return null; 
      } 
     } 
    } 

然後我有一個客戶端類,給我一個錯誤"Cannot implicitly convert type 'WinFormsConsumer.localhost.Point' to 'System.Drawing.Point"

本地主機是我已經加入到WinFormsConsumer類爲Add Service Refference...,通過選擇Web Refference並通過http://localhost:2540/PaintService.asmx?wsdl Web服務的名稱。

using System; 
using System.Collections; 
using System.Drawing; 
using System.Windows.Forms; 

    namespace WinFormsConsumer 
    { 
      public partial class Form1 : Form 
      { 
       localhost.PaintService ps = new localhost.PaintService(); 

       private Random r = new Random(); 
       private Timer timer = new Timer(); 
       private ArrayList list = new ArrayList(); 

       public Form1() 
       { 
        ... 
       } 

       private void Timer_Tick(object sender, EventArgs e) 
       { 
        //below line gives the error 
        Point temp = ps.StartDraw(ClientRectangle.X, 
     ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height); 
        this.Invalidate(); 
       } 
      } 
     } 

爲什麼我得到這個錯誤?我不明白,爲什麼看起來從Web Service返回的Point類型與System中定義的類型「不同」? 我錯過了什麼?

回答

0

我做了一些研究,答案是:這是不可能的。這是ASMX的薄弱環節,也是引入WCF的原因之一。我發現了一些解決方法,比如創建'SwallowCopy'類等,但這不是一件好事,只能用於類。

因此,假設我所說的,再加上它只是一個項目,我必須使用它,我決定只使用代理對象。客戶不必知道它,它現在適合我的需求。

0

默認情況下,Visual Studio不會重用Web服務公開的所有類型。它將System.Drawing.Point視爲第三方類型,並定義與其簽名匹配的「代理」。你必須告訴它你想在代理中「重複使用」哪些類型。

編輯服務參考並選中「重新使用指定的引用程序集中的類型」框。然後確保「System.Drawing.DLL」可用並已選中

有關MSDN的更多信息。

+0

我有選項「在所有引用的程序集中重用類型」,默認情況下,當我向服務添加引用時選中。但是,也許我不應該點擊「高級」,然後點擊「添加網站引用」?我不確定.. –

+0

我發現我無法在ASMX Web服務中重用類型。我知道我可以使用WCF,但是我的任務是執行ASMX服務。那麼,是否有任何解決這個問題的方法,以便我可以轉換類型? –