Adding @Assembly references programmatically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Cheng[MSFT]

    #16
    Re: Adding @Assembly references programmaticall y

    Hi Praveen,

    Well, I think you're right. For those dynamically compiled components
    (aspx, ascx ....), the referenced assembly are specified by the @Register
    @Assembly directives in template or the predefined references in
    web.config(or machine.config) 's <assembies> configuration element. And by
    default the machine.conifig 's <assemblies> settting contains the

    <assemblies>
    ....
    <add assembly="*" />
    </assemblies>

    So all the assemblies in the private bin path will be automatically
    refereced. That's why if using copylocal, that'll work.

    Currently I think we have the following options when developing our custom
    control which may meet such problem:
    1. Programmaticall y add the <@assembly > directive into aspx page. Though
    we can get the path of the editing
    aspx page in VS.NET at design-time , but I didn't suggest this approach
    since VS.NET didn't provide buidlin DOM structure or interface to let us
    programly editing it.

    2. Since we can specify the assembly reference in
    web.config/machine.config' s <compilation><a ssemblies>.. like below:

    <compilation
    defaultLanguage ="c#"
    debug="true"[color=blue]
    >[/color]
    <assemblies>
    <add
    assembly="Inter faceLib,Version =1.0.0.0,Cultur e=Neutral,Publi cKeyToken=2bfe0 2
    78943847bf"/>
    </assemblies>
    </compilation>

    We can consider adding the reference of our assembly into web.config at
    design-time. Since web.config is a xml file we can using the buildin DOM
    api to manipulate it. That'll be much more convenient.

    How do you think of this?

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)



    Comment

    • Praveen

      #17
      Re: Adding @Assembly references programmaticall y

      Steven,

      Option 1 is the preferrable option, but if that cannot be implemented, I
      will make do with option 2. Come to think of it, isn't the aspx file an xml
      file as well? Shouldn't we be able to parse it then just as the web.config
      file?

      Do you have any code samples that will help me get started on this?

      Thanks
      Praveen

      "Steven Cheng[MSFT]" <v-schang@online.m icrosoft.com> wrote in message
      news:dkMaI%23kR FHA.14072@TK2MS FTNGXA01.phx.gb l...[color=blue]
      > Hi Praveen,
      >
      > Well, I think you're right. For those dynamically compiled components
      > (aspx, ascx ....), the referenced assembly are specified by the @Register
      > @Assembly directives in template or the predefined references in
      > web.config(or machine.config) 's <assembies> configuration element. And by
      > default the machine.conifig 's <assemblies> settting contains the
      >
      > <assemblies>
      > ...
      > <add assembly="*" />
      > </assemblies>
      >
      > So all the assemblies in the private bin path will be automatically
      > refereced. That's why if using copylocal, that'll work.
      >
      > Currently I think we have the following options when developing our custom
      > control which may meet such problem:
      > 1. Programmaticall y add the <@assembly > directive into aspx page. Though
      > we can get the path of the editing
      > aspx page in VS.NET at design-time , but I didn't suggest this approach
      > since VS.NET didn't provide buidlin DOM structure or interface to let us
      > programly editing it.
      >
      > 2. Since we can specify the assembly reference in
      > web.config/machine.config' s <compilation><a ssemblies>.. like below:
      >
      > <compilation
      > defaultLanguage ="c#"
      > debug="true"[color=green]
      > >[/color]
      > <assemblies>
      > <add
      > assembly="Inter faceLib,Version =1.0.0.0,Cultur e=Neutral,Publi cKeyToken=2bfe0 2
      > 78943847bf"/>
      > </assemblies>
      > </compilation>
      >
      > We can consider adding the reference of our assembly into web.config at
      > design-time. Since web.config is a xml file we can using the buildin DOM
      > api to manipulate it. That'll be much more convenient.
      >
      > How do you think of this?
      >
      > Steven Cheng
      > Microsoft Online Support
      >
      > Get Secure! www.microsoft.com/security
      > (This posting is provided "AS IS", with no warranties, and confers no
      > rights.)
      >
      >
      >[/color]


      Comment

      • Praveen

        #18
        Re: Adding @Assembly references programmaticall y

        I actually got this working through some hacks, after going through the
        designer source using Reflector. Here are the steps:

        In the custom control's custom designer's Initialize override, do something
        like this:
        public override void Initialize(ICom ponent component)
        {

        base.Initialize (component);

        IDesignerHost host = component.Site. Container as IDesignerHost;
        IDesigner designer = host.GetDesigne r(host.RootComp onent);

        // Calling GetHTMLFromWebC ontrolTool with the following custom toolboxitem
        will insert the
        // Register directives for the type associated with that .
        MethodInfo mi = designer.GetTyp e.GetMethod("Ge tHTMLFromWebCon trolTool",
        BindingFlags.No nPublic | BindingFlags.In stance);
        if(mi != null)
        {
        // DependantType is a custom type defined in DependantAssemb ly.dll
        mi.Invoke(desig ner, new object[]{new
        WebControlToolb oxItem(typeof(S omeNamespace.De pendantType))}) ;
        }
        }

        Then when the user drags and drops the item from the toolbox, besides the
        default @ register entry it makes, it will also make an entry like this:
        <%@ Register TagPrefix="cc1" Namespace="Some Namespace"
        Assembly="Depen dantAssembly, Version=2.0.0.1 , Culture=neutral ,
        PublicKeyToken= 3d6dfsd1fdsd44c 89" %>

        The assembly will not be strong named in the above tag if it's not in the
        GAC.

        Thanks a lot for your help.

        -Praveen



        Comment

        • Steven Cheng[MSFT]

          #19
          Re: Adding @Assembly references programmaticall y

          Great Work Praveen!

          I must admit that your professional and passion really impressed me. In
          fact, I've never noticed the

          IDesigner designer = host.GetDesigne r(host.RootComp onent);

          MethodInfo mi = designer.GetTyp e.GetMethod("Ge tHTMLFromWebCon trolTool",

          before though I also use reflecter to lookup some designer's code
          sometimes. I was going to provide some former thread on reference the
          web.cofig file and modify it at design-time in control before reading your
          last message. But now I think they're unnecessary since you've found such a
          good solution.

          Anyway, I shall thank you for the persistent work and sharing your great
          effort with us. I'm sure that'll be much valuable to many other community
          members who're struggling with such problem.

          Also, if there are any problems in the future, please always feel free to
          discuss here.

          Thanks & Regards,

          Steven Cheng
          Microsoft Online Support

          Get Secure! www.microsoft.com/security
          (This posting is provided "AS IS", with no warranties, and confers no
          rights.)

          Comment

          Working...