Using custom data types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt D

    #1

    Using custom data types

    In my web service project I've imported an assembly (that I also have
    the source to) that contains structs that are serializable. I've
    created some web service methods that return and take as parameters
    some of these structures. When adding a web reference to a client that
    consumes these web services, reference.cs doesn't contain a definition
    for any of the structures in the assembly. I know I can add a
    reference to the assembly directly in the client project to make it
    work, but is there a way that I can make it work without doing this?
    How can I expose these structures so they are part of the wsdl file
    and thus have proxy stubs created for them? I would like clients to be
    able to use these web services without having to download and add a
    reference to their project. Thanks.

    Matt
  • Sahil Malik

    #2
    Re: Using custom data types

    Matt,

    Yes it's possible.

    Here is an example --- WebService --- Service1.asmx.c s file looks like ---

    using System;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Diagnost ics;
    using System.Web;
    using System.Web.Serv ices;

    namespace WebService1
    {
    /// <summary>
    /// Summary description for Service1.
    /// </summary>
    public class Service1 : System.Web.Serv ices.WebService
    {
    public Service1()
    {
    //CODEGEN: This call is required by the ASP.NET Web Services Designer
    InitializeCompo nent();
    }

    [WebMethod]
    public MyStruct HelloStruct()
    {
    MyStruct str = new MyStruct();
    str.message = "Hello World !" ;
    return str ;
    }

    #region Component Designer generated code

    //Required by the Web Services Designer
    private IContainer components = null;

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeCompo nent()
    {
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if(disposing && components != null)
    {
    components.Disp ose();
    }
    base.Dispose(di sposing);
    }

    #endregion
    }

    public struct MyStruct
    {
    public string message;
    }
    }





    .... The client application will call the above as ...

    localhost.Servi ce1 svc = new WindowsApplicat ion1.localhost. Service1() ;
    localhost.MyStr uct str = svc.HelloStruct () ;
    MessageBox.Show (str.message) ;


    If instead of a struct you wish to a class; you will have to specify set
    accessors for all public properties. Need I mention, the logic of the class
    doesn't come thru WSDL.

    Regards,

    - Sahil Malik
    You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik



    "Matt D" <mdd9024-news@yahoo.com> wrote in message
    news:is3em0pi69 qbesa18gtf34kcd uvt7ptigu@4ax.c om...[color=blue]
    > In my web service project I've imported an assembly (that I also have
    > the source to) that contains structs that are serializable. I've
    > created some web service methods that return and take as parameters
    > some of these structures. When adding a web reference to a client that
    > consumes these web services, reference.cs doesn't contain a definition
    > for any of the structures in the assembly. I know I can add a
    > reference to the assembly directly in the client project to make it
    > work, but is there a way that I can make it work without doing this?
    > How can I expose these structures so they are part of the wsdl file
    > and thus have proxy stubs created for them? I would like clients to be
    > able to use these web services without having to download and add a
    > reference to their project. Thanks.
    >
    > Matt[/color]


    Comment

    • Matt D

      #3
      Re: Using custom data types

      Sahil,
      Thanks for replying to my post. I do understand that it is possible
      for this to work if MyStruct is in the same source file as the web
      service. But is it possible for it to work if MyStruct is in a
      separate assembly referenced by the web service without requiring the
      client to download and add a reference to that same assembly? Thanks
      again.

      Matt

      On Sun, 10 Oct 2004 14:57:19 -0400, "Sahil Malik"
      <contactmethrum yblog@nospam.co m> wrote:
      [color=blue]
      >Matt,
      >
      >Yes it's possible.
      >
      >Here is an example --- WebService --- Service1.asmx.c s file looks like ---
      >
      >using System;
      >using System.Collecti ons;
      >using System.Componen tModel;
      >using System.Data;
      >using System.Diagnost ics;
      >using System.Web;
      >using System.Web.Serv ices;
      >
      >namespace WebService1
      >{
      > /// <summary>
      > /// Summary description for Service1.
      > /// </summary>
      > public class Service1 : System.Web.Serv ices.WebService
      > {
      > public Service1()
      > {
      > //CODEGEN: This call is required by the ASP.NET Web Services Designer
      > InitializeCompo nent();
      > }
      >
      > [WebMethod]
      > public MyStruct HelloStruct()
      > {
      > MyStruct str = new MyStruct();
      > str.message = "Hello World !" ;
      > return str ;
      > }
      >
      > #region Component Designer generated code
      >
      > //Required by the Web Services Designer
      > private IContainer components = null;
      >
      > /// <summary>
      > /// Required method for Designer support - do not modify
      > /// the contents of this method with the code editor.
      > /// </summary>
      > private void InitializeCompo nent()
      > {
      > }
      >
      > /// <summary>
      > /// Clean up any resources being used.
      > /// </summary>
      > protected override void Dispose( bool disposing )
      > {
      > if(disposing && components != null)
      > {
      > components.Disp ose();
      > }
      > base.Dispose(di sposing);
      > }
      >
      > #endregion
      > }
      >
      > public struct MyStruct
      > {
      > public string message;
      > }
      >}
      >
      >
      >
      >
      >
      >... The client application will call the above as ...
      >
      >localhost.Serv ice1 svc = new WindowsApplicat ion1.localhost. Service1() ;
      >localhost.MySt ruct str = svc.HelloStruct () ;
      >MessageBox.Sho w(str.message) ;
      >
      >
      >If instead of a struct you wish to a class; you will have to specify set
      >accessors for all public properties. Need I mention, the logic of the class
      >doesn't come thru WSDL.
      >
      >Regards,
      >
      >- Sahil Malik
      >You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik[/color]

      Comment

      • MIGUEL

        #4
        Re: Using custom data types

        Hi Matt,

        I share your opinion. I do not want my client to install any more
        assemblies, so I wanted to know if you solved your problem after havint
        written the enum or class in a separate assembly? Did it work?

        I'm very interested in that because I've got the same problem as you,
        instead of a enum, with a class.

        Thanks in advance.

        "Matt D" wrote:
        [color=blue]
        > Sahil,
        > Thanks for replying to my post. I do understand that it is possible
        > for this to work if MyStruct is in the same source file as the web
        > service. But is it possible for it to work if MyStruct is in a
        > separate assembly referenced by the web service without requiring the
        > client to download and add a reference to that same assembly? Thanks
        > again.
        >
        > Matt
        >
        > On Sun, 10 Oct 2004 14:57:19 -0400, "Sahil Malik"
        > <contactmethrum yblog@nospam.co m> wrote:
        >[color=green]
        > >Matt,
        > >
        > >Yes it's possible.
        > >
        > >Here is an example --- WebService --- Service1.asmx.c s file looks like ---
        > >
        > >using System;
        > >using System.Collecti ons;
        > >using System.Componen tModel;
        > >using System.Data;
        > >using System.Diagnost ics;
        > >using System.Web;
        > >using System.Web.Serv ices;
        > >
        > >namespace WebService1
        > >{
        > > /// <summary>
        > > /// Summary description for Service1.
        > > /// </summary>
        > > public class Service1 : System.Web.Serv ices.WebService
        > > {
        > > public Service1()
        > > {
        > > //CODEGEN: This call is required by the ASP.NET Web Services Designer
        > > InitializeCompo nent();
        > > }
        > >
        > > [WebMethod]
        > > public MyStruct HelloStruct()
        > > {
        > > MyStruct str = new MyStruct();
        > > str.message = "Hello World !" ;
        > > return str ;
        > > }
        > >
        > > #region Component Designer generated code
        > >
        > > //Required by the Web Services Designer
        > > private IContainer components = null;
        > >
        > > /// <summary>
        > > /// Required method for Designer support - do not modify
        > > /// the contents of this method with the code editor.
        > > /// </summary>
        > > private void InitializeCompo nent()
        > > {
        > > }
        > >
        > > /// <summary>
        > > /// Clean up any resources being used.
        > > /// </summary>
        > > protected override void Dispose( bool disposing )
        > > {
        > > if(disposing && components != null)
        > > {
        > > components.Disp ose();
        > > }
        > > base.Dispose(di sposing);
        > > }
        > >
        > > #endregion
        > > }
        > >
        > > public struct MyStruct
        > > {
        > > public string message;
        > > }
        > >}
        > >
        > >
        > >
        > >
        > >
        > >... The client application will call the above as ...
        > >
        > >localhost.Serv ice1 svc = new WindowsApplicat ion1.localhost. Service1() ;
        > >localhost.MySt ruct str = svc.HelloStruct () ;
        > >MessageBox.Sho w(str.message) ;
        > >
        > >
        > >If instead of a struct you wish to a class; you will have to specify set
        > >accessors for all public properties. Need I mention, the logic of the class
        > >doesn't come thru WSDL.
        > >
        > >Regards,
        > >
        > >- Sahil Malik
        > >You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik[/color]
        >
        >[/color]

        Comment

        Working...