C# ServicedComponent Singleton

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

    C# ServicedComponent Singleton

    Hello,
    I'm writing a C# class library that may be called by non-.Net
    applications. The main class of this class libray is a ServicedCompone nt
    so that non-.Net application can call this class as a standard COM+
    component. Now, I want that class to be a singleton. Once loaded, the
    values moslty never change.
    I tried using this pattern:

    public sealed class Singleton
    {
    private static readonly Singleton instance = new Singleton();

    private Singleton(){}

    public static Singleton Instance
    {
    get
    {
    return instance;
    }
    }
    }

    However, it throws an exception complaining about ServicedCompone nt
    thats needs a public constructor.
    The error raised is:

    An unhandled exception of type
    'System.Enterpr iseServices.Reg istrationExcept ion' occurred in
    system.enterpri seservices.dll

    Additional information: Invalid ServicedCompone nt-derived classes were
    found in the assembly.
    (Classes must be public, concrete, have a public default constructor,
    and meet all other ComVisibility requirements)


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • David Browne

    #2
    Re: C# ServicedCompone nt Singleton


    "David" <nospam@sympati co.ca> wrote in message
    news:OBK3vt92EH A.2012@TK2MSFTN GP15.phx.gbl...[color=blue]
    > Hello,
    > I'm writing a C# class library that may be called by non-.Net
    > applications. The main class of this class libray is a ServicedCompone nt
    > so that non-.Net application can call this class as a standard COM+
    > component. Now, I want that class to be a singleton. Once loaded, the
    > values moslty never change.
    > I tried using this pattern:
    >
    > public sealed class Singleton
    > {
    > private static readonly Singleton instance = new Singleton();
    >
    > private Singleton(){}
    >
    > public static Singleton Instance
    > {
    > get
    > {
    > return instance;
    > }
    > }
    > }
    >[/color]

    There are two different "kinds" of singletons. There's the kind of
    singleton
    [color=blue]
    > However, it throws an exception complaining about ServicedCompone nt
    > thats needs a public constructor.
    > The error raised is:
    >
    > An unhandled exception of type
    > 'System.Enterpr iseServices.Reg istrationExcept ion' occurred in
    > system.enterpri seservices.dll
    >
    > Additional information: Invalid ServicedCompone nt-derived classes were
    > found in the assembly.
    > (Classes must be public, concrete, have a public default constructor,
    > and meet all other ComVisibility requirements)
    >
    >[/color]

    Right. Singletons cannot be exposed as COM components. The easiest fix for
    this is to publish a facade class for your COM clients.

    public sealed class SingletonFacade
    {
    public SingletonFacade () {}
    public void Foo()
    {
    Singleton.insta nce.Foo();
    }
    //etc
    }

    David


    Comment

    • Willy Denoyette [MVP]

      #3
      Re: C# ServicedCompone nt Singleton

      Implement your component as a pooled object with a min/max of 1 objects in
      the pool and enable JITA.
      Don't forget to SetComplete to true when returning after a call else you
      will block further calls.

      Willy.

      "David" <nospam@sympati co.ca> wrote in message
      news:OBK3vt92EH A.2012@TK2MSFTN GP15.phx.gbl...[color=blue]
      > Hello,
      > I'm writing a C# class library that may be called by non-.Net
      > applications. The main class of this class libray is a ServicedCompone nt
      > so that non-.Net application can call this class as a standard COM+
      > component. Now, I want that class to be a singleton. Once loaded, the
      > values moslty never change.
      > I tried using this pattern:
      >
      > public sealed class Singleton
      > {
      > private static readonly Singleton instance = new Singleton();
      >
      > private Singleton(){}
      >
      > public static Singleton Instance
      > {
      > get
      > {
      > return instance;
      > }
      > }
      > }
      >
      > However, it throws an exception complaining about ServicedCompone nt
      > thats needs a public constructor.
      > The error raised is:
      >
      > An unhandled exception of type
      > 'System.Enterpr iseServices.Reg istrationExcept ion' occurred in
      > system.enterpri seservices.dll
      >
      > Additional information: Invalid ServicedCompone nt-derived classes were
      > found in the assembly.
      > (Classes must be public, concrete, have a public default constructor,
      > and meet all other ComVisibility requirements)
      >
      >
      > *** Sent via Developersdex http://www.developersdex.com ***
      > Don't just participate in USENET...get rewarded for it![/color]


      Comment

      • Anders Borum

        #4
        Re: C# ServicedCompone nt Singleton

        Hello!
        [color=blue]
        > The easiest fix for this is to publish a facade class for your COM[/color]
        clients.

        Couldn't one argue that it's an adapter class also? (I'm talking patterns
        here :-)

        --
        venlig hilsen / with regards
        anders borum
        --


        Comment

        • David Browne

          #5
          Re: C# ServicedCompone nt Singleton


          "Anders Borum" <anders@spherew orks.dk> wrote in message
          news:%23FTKmQE3 EHA.1392@tk2msf tngp13.phx.gbl. ..[color=blue]
          > Hello!
          >[color=green]
          >> The easiest fix for this is to publish a facade class for your COM[/color]
          > clients.
          >
          > Couldn't one argue that it's an adapter class also? (I'm talking patterns
          > here :-)
          >[/color]

          I agree that this is more of the adapter pattern. Calling it a facade class
          is really J2EE-speak.

          David


          Comment

          • David

            #6
            Re: C# ServicedCompone nt Singleton

            Hello,
            I'm not familiar with JITA pooled objects.
            Setting min/max to 1 will ensure only one single instance will always be
            loaded into memory? What about variables set into the component?

            Let say I have 2 methods in this component, SetValues and GetValues.
            Another application that launches at startup of Windows sets the values
            in the singleton passing the values to the SetValues method. This method
            stores values into static variables in the singleton component and
            issues a SetComplete (or the component can be set to "autocomplete") .
            Then many other applications can call the GetValues to get the values
            previously provided to the singleton. Will this senario work?

            Thanks,
            David.



            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Willy Denoyette [MVP]

              #7
              Re: C# ServicedCompone nt Singleton


              "David" <nospam@sympati co.ca> wrote in message
              news:u5kBAFN3EH A.3416@TK2MSFTN GP09.phx.gbl...[color=blue]
              > Hello,
              > I'm not familiar with JITA pooled objects.
              > Setting min/max to 1 will ensure only one single instance will always be
              > loaded into memory? What about variables set into the component?
              >
              > Let say I have 2 methods in this component, SetValues and GetValues.
              > Another application that launches at startup of Windows sets the values
              > in the singleton passing the values to the SetValues method. This method
              > stores values into static variables in the singleton component and
              > issues a SetComplete (or the component can be set to "autocomplete") .
              > Then many other applications can call the GetValues to get the values
              > previously provided to the singleton. Will this senario work?
              >[/color]

              Sure, global state is preserved between calls, per client state however is
              not.
              You can assign a value to your (private) statics (your global state) in the
              constructor, so that they are initialized once at object creation time. Once
              assigned they remain for the lifetime of the application, and you can use
              propety get to retrieve your global state.

              Willy.



              Comment

              • David Browne

                #8
                Re: C# ServicedCompone nt Singleton


                "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
                news:OFnvlaU3EH A.824@TK2MSFTNG P11.phx.gbl...[color=blue]
                >
                > "David" <nospam@sympati co.ca> wrote in message
                > news:u5kBAFN3EH A.3416@TK2MSFTN GP09.phx.gbl...[color=green]
                >> Hello,
                >> I'm not familiar with JITA pooled objects.
                >> Setting min/max to 1 will ensure only one single instance will always be
                >> loaded into memory? What about variables set into the component?
                >>
                >> Let say I have 2 methods in this component, SetValues and GetValues.
                >> Another application that launches at startup of Windows sets the values
                >> in the singleton passing the values to the SetValues method. This method
                >> stores values into static variables in the singleton component and
                >> issues a SetComplete (or the component can be set to "autocomplete") .
                >> Then many other applications can call the GetValues to get the values
                >> previously provided to the singleton. Will this senario work?
                >>[/color]
                >
                > Sure, global state is preserved between calls, per client state however is
                > not.
                > You can assign a value to your (private) statics (your global state) in
                > the constructor, so that they are initialized once at object creation
                > time. Once assigned they remain for the lifetime of the application, and
                > you can use propety get to retrieve your global state.
                >[/color]

                But this is not just a singleton, this is a serialized singleton. There's
                no need to serialize all your clients just to get access to global state.

                David


                Comment

                • David

                  #9
                  Re: C# ServicedCompone nt Singleton

                  What do you mean by serialized singleton?

                  *** Sent via Developersdex http://www.developersdex.com ***
                  Don't just participate in USENET...get rewarded for it!

                  Comment

                  • David Browne

                    #10
                    Re: C# ServicedCompone nt Singleton


                    "David" <nospam@sympati co.ca> wrote in message
                    news:Ox6G6JV3EH A.3452@TK2MSFTN GP14.phx.gbl...[color=blue]
                    > What do you mean by serialized singleton?[/color]

                    If you have two different threads, or two different processes trying to
                    access the object, they will be serialized. One will wait until the other
                    has released the reference to the object. Not just the method calls will be
                    serialized, but whole sequence of client code using the object

                    obj = CreateObject(.. .)
                    obj.MethodA
                    obj.MethodB
                    set obj = null

                    will be strictly serialized. A second thread or process will block on
                    CreateObject until the previous thread has released the reference with set
                    obj=null.


                    See

                    From "Everyone Into the Pool" by Rockford Lhotka


                    In either case, if a client requests an object but we already have the
                    maximum number loaded and in use, the client request is blocked. When an
                    object does become free, the object is used to service the blocked client
                    request.

                    See also



                    David


                    Comment

                    • Willy Denoyette [MVP]

                      #11
                      Re: C# ServicedCompone nt Singleton


                      "David Browne" <davidbaxterbro wne no potted meat@hotmail.co m> wrote in
                      message news:uLDIgaV3EH A.3908@TK2MSFTN GP12.phx.gbl...[color=blue]
                      >
                      > "David" <nospam@sympati co.ca> wrote in message
                      > news:Ox6G6JV3EH A.3452@TK2MSFTN GP14.phx.gbl...[color=green]
                      >> What do you mean by serialized singleton?[/color]
                      >
                      > If you have two different threads, or two different processes trying to
                      > access the object, they will be serialized. One will wait until the other
                      > has released the reference to the object. Not just the method calls will
                      > be serialized, but whole sequence of client code using the object
                      >
                      > obj = CreateObject(.. .)
                      > obj.MethodA
                      > obj.MethodB
                      > set obj = null
                      >
                      > will be strictly serialized. A second thread or process will block on
                      > CreateObject until the previous thread has released the reference with set
                      > obj=null.[/color]

                      This is not exactly true, that's the purpose of the JITA mechanism and
                      ContextUtil.Set Complete (or [AutoComplete(tr ue)]), as soon as a method ends
                      the object returns to the pool where it waits to be re-activated.
                      Any number of clients (subject to scalability) can CREATE and KEEP a
                      reference a COM+ singleton object.
                      Each client creation call will get a reference to the same instance provided
                      no single client is currently executing a method, when that's the case the
                      activation request will be suspended for a max. of the configured
                      "activation time-out".
                      Method calls are automatically synchronized (implied by JITA), each method
                      call will be suspended (activation time-out) whenever another client is
                      executing a method.

                      Willy.


                      Comment

                      • David Browne

                        #12
                        Re: C# ServicedCompone nt Singleton


                        "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
                        news:uXQyMGW3EH A.1300@TK2MSFTN GP14.phx.gbl...[color=blue]
                        >
                        > "David Browne" <davidbaxterbro wne no potted meat@hotmail.co m> wrote in
                        > message news:uLDIgaV3EH A.3908@TK2MSFTN GP12.phx.gbl...[color=green]
                        >>
                        >> "David" <nospam@sympati co.ca> wrote in message
                        >> news:Ox6G6JV3EH A.3452@TK2MSFTN GP14.phx.gbl...[color=darkred]
                        >>> What do you mean by serialized singleton?[/color]
                        >>
                        >> If you have two different threads, or two different processes trying to
                        >> access the object, they will be serialized. One will wait until the
                        >> other has released the reference to the object. Not just the method
                        >> calls will be serialized, but whole sequence of client code using the
                        >> object
                        >>
                        >> obj = CreateObject(.. .)
                        >> obj.MethodA
                        >> obj.MethodB
                        >> set obj = null
                        >>
                        >> will be strictly serialized. A second thread or process will block on
                        >> CreateObject until the previous thread has released the reference with
                        >> set obj=null.[/color]
                        >
                        > This is not exactly true, that's the purpose of the JITA mechanism and
                        > ContextUtil.Set Complete (or [AutoComplete(tr ue)]), as soon as a method
                        > ends the object returns to the pool where it waits to be re-activated.
                        > Any number of clients (subject to scalability) can CREATE and KEEP a
                        > reference a COM+ singleton object.
                        > Each client creation call will get a reference to the same instance
                        > provided no single client is currently executing a method, when that's the
                        > case the activation request will be suspended for a max. of the configured
                        > "activation time-out".
                        > Method calls are automatically synchronized (implied by JITA), each method
                        > call will be suspended (activation time-out) whenever another client is
                        > executing a method.
                        >[/color]

                        Ok. So the serialization is at the method level. It's still absolutely
                        unnecessary.

                        David


                        Comment

                        • Willy Denoyette [MVP]

                          #13
                          Re: C# ServicedCompone nt Singleton


                          "David Browne" <davidbaxterbro wne no potted meat@hotmail.co m> wrote in
                          message news:OBbX24W3EH A.1300@TK2MSFTN GP14.phx.gbl...[color=blue]
                          >
                          > "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
                          > news:uXQyMGW3EH A.1300@TK2MSFTN GP14.phx.gbl...[color=green]
                          >>
                          >> "David Browne" <davidbaxterbro wne no potted meat@hotmail.co m> wrote in
                          >> message news:uLDIgaV3EH A.3908@TK2MSFTN GP12.phx.gbl...[color=darkred]
                          >>>
                          >>> "David" <nospam@sympati co.ca> wrote in message
                          >>> news:Ox6G6JV3EH A.3452@TK2MSFTN GP14.phx.gbl...
                          >>>> What do you mean by serialized singleton?
                          >>>[/color][/color][/color]
                          [color=blue]
                          > Ok. So the serialization is at the method level. It's still absolutely
                          > unnecessary.[/color]

                          Why? There is only one single object instance, if you allow concurrent
                          callers how are you going to protect you state variables?
                          This singleton patterns is typically used when you want to restrict the
                          access to an expensive external resource to a single ( or limtted number of)
                          caller(s) at a time.

                          Implementing it as a COM+ singleton offers the following important
                          advantages:
                          - Lazy instantiation (JITA)
                          - Auto synchronization
                          - Lifetime control
                          - Transaction control
                          - Security (access and instantiation)

                          Sure there are drawback (inherent to the singleton pattern), all depends on
                          the requirements and usage pattern, but as OP's original question referred
                          to ES, I assume he knows his requirements better than we do ;-).

                          Willy.





                          Comment

                          • David Browne

                            #14
                            Re: C# ServicedCompone nt Singleton


                            "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
                            news:uc41dGX3EH A.3376@TK2MSFTN GP12.phx.gbl...[color=blue]
                            >
                            > "David Browne" <davidbaxterbro wne no potted meat@hotmail.co m> wrote in
                            > message news:OBbX24W3EH A.1300@TK2MSFTN GP14.phx.gbl...[color=green]
                            >>
                            >> "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
                            >> news:uXQyMGW3EH A.1300@TK2MSFTN GP14.phx.gbl...[color=darkred]
                            >>>
                            >>> "David Browne" <davidbaxterbro wne no potted meat@hotmail.co m> wrote in
                            >>> message news:uLDIgaV3EH A.3908@TK2MSFTN GP12.phx.gbl...
                            >>>>
                            >>>> "David" <nospam@sympati co.ca> wrote in message
                            >>>> news:Ox6G6JV3EH A.3452@TK2MSFTN GP14.phx.gbl...
                            >>>>> What do you mean by serialized singleton?
                            >>>>[/color][/color]
                            >[color=green]
                            >> Ok. So the serialization is at the method level. It's still absolutely
                            >> unnecessary.[/color]
                            >
                            > Why? There is only one single object instance, if you allow concurrent
                            > callers how are you going to protect you state variables?[/color]

                            lock(obj)
                            {
                            }
                            [color=blue]
                            > This singleton patterns is typically used when you want to restrict the
                            > access to an expensive external resource to a single ( or limtted number
                            > of) caller(s) at a time.[/color]

                            Not so. You protect state variables using normal syncronization mechanisms.
                            And the singleton pattern is typically used to _share_ the resource. You
                            can have global shared resources which permit highly concurrent access (eg a
                            read-only DataTable), or mostly concurrent access (eg a Hashtable which you
                            lock during writes and iterations). You need to make it thread-safe, but
                            what you don't need is a big clunky framework that thinks it can do your job
                            for you.
                            [color=blue]
                            >
                            > Implementing it as a COM+ singleton offers the following important
                            > advantages:
                            > - Lazy instantiation (JITA)[/color]

                            C# singletons are initialized lazilly too. They are only initialized when
                            the class is first used. And you can even make them lazier by making
                            "instance" a property and creating the singleton on the first call.
                            [color=blue]
                            > - Auto synchronization
                            > - Lifetime control
                            > - Transaction control
                            > - Security (access and instantiation)
                            >[/color]

                            COM+ does offer some useful services. If you want to use them, do. But
                            where the same end is more easilly acomplished with pure C#, COM+ isn't woth
                            the extra intelectual effort to worry about.

                            David


                            Comment

                            • David

                              #15
                              Re: C# ServicedCompone nt Singleton

                              I would love using pure C# but my component must be "callable" from non
                              .Net compliant languages, VB6 and Deplhi 6 to name a few. So my
                              understanding is that for this reason I need COM+. Now, if there is a
                              better way to create C# components callable from non .net languages, I
                              would like to know because I really don't need any of the services
                              provided by COM+.



                              *** Sent via Developersdex http://www.developersdex.com ***
                              Don't just participate in USENET...get rewarded for it!

                              Comment

                              Working...