简体   繁体   English

ASMX ASP.NET,按服务方法返回的怪异类型

[英]ASMX ASP.NET, weird return type by service method

I'm new in ASP.NET. 我是ASP.NET的新手。 I wanted to create a simple Web Service containing of one method: 我想创建一个包含一个方法的简单Web服务:

Here's the code: 这是代码:

    [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;
            }
        }
    }

Then I've got a client class that gives me an error "Cannot implicitly convert type 'WinFormsConsumer.localhost.Point' to 'System.Drawing.Point" . 然后,我得到了一个客户端类,该类给我一个错误"Cannot implicitly convert type 'WinFormsConsumer.localhost.Point' to 'System.Drawing.Point"

localhost is the name of the web service which I've added to WinFormsConsumer class as Add Service Refference... , by choosing Web Refference and passing http://localhost:2540/PaintService.asmx?wsdl . localhost是我通过选择Web Refference并传递http://localhost:2540/PaintService.asmx?wsdl添加到WinFormsConsumer类中的Web服务的名称,作为Add Service Refference...

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();
                }
            }
      }

Why do I get this error? 为什么会出现此错误? I don't understand it, why it seems like the type of Point returned from Web Service is "different" that those defined in System? 我不明白,为什么从Web Service返回的Point类型看起来与System中定义的Point类型“不同”? What am I missing? 我想念什么?

Visual Studio will not reuse all types exposed by web services by default. 默认情况下,Visual Studio不会重用Web服务公开的所有类型。 It is treating System.Drawing.Point as a third-party type and defining a "proxy" that matches it's signature. 它将System.Drawing.Point视为第三方类型,并定义与其签名匹配的“代理”。 You have to tell it which types you want to "reuse" in the proxy. 您必须告诉它要在代理中“重用”哪些类型。

Edit the service reference and check the box that says "Reuse types in specified referenced assemblies." 编辑服务引用,然后选中“在指定的引用程序集中重用类型”框。 Then make sure "System.Drawing.DLL" is available and selected 然后确保“ System.Drawing.DLL”可用并被选中

More info on MSDN . 有关MSDN的更多信息。

I've made some research and the answer is: it's impossible. 我做了一些研究,答案是:不可能。 This is the weak side of ASMX and one of reasons why the WCF was introduced. 这是ASMX的弱点,也是引入WCF的原因之一。 I've found some work arounds, like creating 'SwallowCopy' class etc., but it's not really good thing to do and works with classes only. 我已经找到了一些解决方法,例如创建“ SwallowCopy”类等,但这并不是一件好事,只能与类一起使用。

So assuming what I've said, plus it's just a project and I have to use it, I've decided to just use the proxy object. 因此,假设我已说过,再加上它只是一个项目,并且必须使用它,我决定只使用代理对象。 The client doesn't have to know about it and it fits my needs right now. 客户不必知道它,它现在可以满足我的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM