.NET SOAP Common types

前端 未结 2 928
时光取名叫无心
时光取名叫无心 2020-12-10 23:02

Is there a way when creating web services to specify the types to use? Specifically, I want to be able to use the same type on both the client and server to reduce duplicati

2条回答
  •  鱼传尺愫
    2020-12-10 23:59

    If you want to have a type or structure shared between your web service and your client, add a public struct to your web service project like so:

    public struct Whatever
    {
        public string A;
        public int B;
    }
    

    then add a method to your web service that has this struct as its return type:

    [WebMethod]
    public Whatever GiveMeWhatever()
    {
        Whatever what = new Whatever();
        what.A = "A";
        what.B = 42;
        return what;
    }
    

    After you update your client's web reference, you'll be able to create structs of type Whatever in your client application like so:

    Webreference.Whatever what = new Webreference.Whatever();
    what.A = "that works?";
    what.B = -1; // FILENOTFOUND
    

    This technique lets you maintain the definition of any structures you need to pass back and forth in one place (the web service project).

提交回复
热议问题