473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Adding @Assembly references programmatically

Hi,

Is there any way to add the @Assembly reference to the aspx files
programmatically from inside a custom control (when it gets dropped on to
the page from the toolbox)?

I have a custom control - MyControl that implements an interface in another
custom assembly - InterfaceAssembly. When MyControl gets dropped on to the
page and run, it results in a "InterfaceAssembly not found" exception. I am
aware that this can be fixed by manually adding a @Assembly directive
refering to the InterfaceAssembly in the GAC (or add a reference to it in
the web.config/machine.config, etc) - but that is not good enough. The
MyControl is a control that will be distributed to a whole lot of people and
we do not want them to do this manual step every time they drag and drop
this control to a page - not very elegant.

I see that Steven Cheng from MS has responded to some related issues in the
newsgroups. Hope to hear from him or someone else from MS.

Thanks in advance,
Praveen
Nov 19 '05 #1
18 3730
If it's a user control, then you can add the @Assembly directive to the ASCX
file. If it's a custom control, then add the reference to the assembly via
project references and then compiling your project. If it's in the App_Code
directory, then add an assembly reference via web.config:

http://msdn.microsoft.com/library/de...asp?frame=true

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi,

Is there any way to add the @Assembly reference to the aspx files
programmatically from inside a custom control (when it gets dropped on
to the page from the toolbox)?

I have a custom control - MyControl that implements an interface in
another custom assembly - InterfaceAssembly. When MyControl gets
dropped on to the page and run, it results in a "InterfaceAssembly not
found" exception. I am aware that this can be fixed by manually adding
a @Assembly directive refering to the InterfaceAssembly in the GAC (or
add a reference to it in the web.config/machine.config, etc) - but
that is not good enough. The MyControl is a control that will be
distributed to a whole lot of people and we do not want them to do
this manual step every time they drag and drop this control to a page
- not very elegant.

I see that Steven Cheng from MS has responded to some related issues
in the newsgroups. Hope to hear from him or someone else from MS.

Thanks in advance,
Praveen


Nov 19 '05 #2
If it's a user control, then you can add the @Assembly directive to the ASCX
file. If it's a custom control, then add the reference to the assembly via
project references and then compiling your project. If it's in the App_Code
directory, then add an assembly reference via web.config:

http://msdn.microsoft.com/library/de...asp?frame=true

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi,

Is there any way to add the @Assembly reference to the aspx files
programmatically from inside a custom control (when it gets dropped on
to the page from the toolbox)?

I have a custom control - MyControl that implements an interface in
another custom assembly - InterfaceAssembly. When MyControl gets
dropped on to the page and run, it results in a "InterfaceAssembly not
found" exception. I am aware that this can be fixed by manually adding
a @Assembly directive refering to the InterfaceAssembly in the GAC (or
add a reference to it in the web.config/machine.config, etc) - but
that is not good enough. The MyControl is a control that will be
distributed to a whole lot of people and we do not want them to do
this manual step every time they drag and drop this control to a page
- not very elegant.

I see that Steven Cheng from MS has responded to some related issues
in the newsgroups. Hope to hear from him or someone else from MS.

Thanks in advance,
Praveen


Nov 19 '05 #3
Brock,

Thanks for the reply. But, like I mentioned I do not want to add the
reference manually, I want my custom control to INSERT this AUTOMATICALLY
into the aspx file in which it was added, if possible.

Thanks
-Praveen
Nov 19 '05 #4
Brock,

Thanks for the reply. But, like I mentioned I do not want to add the
reference manually, I want my custom control to INSERT this AUTOMATICALLY
into the aspx file in which it was added, if possible.

Thanks
-Praveen
Nov 19 '05 #5
Thanks for Brock's inputs.

Hi Praveen,

From the description of your problem, it is a VS.NET's design-time issue
which I've met in some former issues. And is the following issue the one
you found on the web search ?

#Adding namespaces to code behind automatically (C#)
http://groups.google.com.sg/groups?h...2752%40cpmsftn
gxa06.phx.gbl&rnum=2

I think that's exactly the same problem as yours. When we developing a
custom control and use VS.NET's ToolBox to drag it onto our page or
winform. The VS.NET ide will automatcally add assembly reference of the
control's Main Assembly(dll). However, if the control still have other
assembly dependecy, VS.NET won't add them into references automatically.
Then, if we didn't manually add them (in our project) or in our control's
design-time code, there will occur assembly not found exception at runtime.
That's just what you 've encountered, yes?

Also, have you tried the sample code in the above thread?

====================
protected override IComponent[] CreateComponentsCore(IDesignerHost host)
{

ITypeResolutionService service1;
Assembly assembly1,assembly2;

IComponent component1;
IComponent[] componentArray1;

IContainer container1 = host.Container;
service1 = ((ITypeResolutionService)
host.GetService(typeof(ITypeResolutionService)));

assembly1 = typeof(ClassLib.UserName).Module.Assembly;
assembly2 = typeof(ComponentLib.SimpleComponent).Module.Assemb ly;
service1.ReferenceAssembly(assembly1.GetName());
service1.ReferenceAssembly(assembly2.GetName());

component1 = new ComponentLib.SimpleComponent();
container1.Add(component1);
componentArray1 = new IComponent[]{component1};

return componentArray1;

}
==================

We can override the CreateComponents method of the ToolboxItem and manually
add the necessary assembly dependecy in it. The following two statements
are just the adding reference code:

service1.ReferenceAssembly(assembly1.GetName());
service1.ReferenceAssembly(assembly2.GetName());

Please have a look to see whether it helps. Thanks,
Steven Cheng
Microsoft Online Support

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

Nov 19 '05 #6
Thanks for Brock's inputs.

Hi Praveen,

From the description of your problem, it is a VS.NET's design-time issue
which I've met in some former issues. And is the following issue the one
you found on the web search ?

#Adding namespaces to code behind automatically (C#)
http://groups.google.com.sg/groups?h...2752%40cpmsftn
gxa06.phx.gbl&rnum=2

I think that's exactly the same problem as yours. When we developing a
custom control and use VS.NET's ToolBox to drag it onto our page or
winform. The VS.NET ide will automatcally add assembly reference of the
control's Main Assembly(dll). However, if the control still have other
assembly dependecy, VS.NET won't add them into references automatically.
Then, if we didn't manually add them (in our project) or in our control's
design-time code, there will occur assembly not found exception at runtime.
That's just what you 've encountered, yes?

Also, have you tried the sample code in the above thread?

====================
protected override IComponent[] CreateComponentsCore(IDesignerHost host)
{

ITypeResolutionService service1;
Assembly assembly1,assembly2;

IComponent component1;
IComponent[] componentArray1;

IContainer container1 = host.Container;
service1 = ((ITypeResolutionService)
host.GetService(typeof(ITypeResolutionService)));

assembly1 = typeof(ClassLib.UserName).Module.Assembly;
assembly2 = typeof(ComponentLib.SimpleComponent).Module.Assemb ly;
service1.ReferenceAssembly(assembly1.GetName());
service1.ReferenceAssembly(assembly2.GetName());

component1 = new ComponentLib.SimpleComponent();
container1.Add(component1);
componentArray1 = new IComponent[]{component1};

return componentArray1;

}
==================

We can override the CreateComponents method of the ToolboxItem and manually
add the necessary assembly dependecy in it. The following two statements
are just the adding reference code:

service1.ReferenceAssembly(assembly1.GetName());
service1.ReferenceAssembly(assembly2.GetName());

Please have a look to see whether it helps. Thanks,
Steven Cheng
Microsoft Online Support

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

Nov 19 '05 #7
Hi Praveen,

After some further checking , I'm thinking the problem is likely
environment specific or project specific. Based on my local test, (a
control with another dependency), I didn't get the assembly not load error.
I've tested using strong-named or not strong-named control assembly. As I
also mentioned in another thread, if possible, you can generate a
simplified version of your problem control and let me do some tests on my
local side.

Looking forward to your response. Thanks,

Steven Cheng
Microsoft Online Support

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


Nov 19 '05 #8
Hi Praveen,

After some further checking , I'm thinking the problem is likely
environment specific or project specific. Based on my local test, (a
control with another dependency), I didn't get the assembly not load error.
I've tested using strong-named or not strong-named control assembly. As I
also mentioned in another thread, if possible, you can generate a
simplified version of your problem control and let me do some tests on my
local side.

Looking forward to your response. Thanks,

Steven Cheng
Microsoft Online Support

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


Nov 19 '05 #9
Steven,

Glad to hear from you!

Yes, the thread you posted in the other message is what I had referred to.
The other message I posted is also, as you might have figured, related to
resolving this issue.

Before I generate a sample for you, could you please test the following. My
custom control implements an interface defined in this other assembly -
which is what is causing the runtime dll not found error. Does your test
case do something similar (implementing an interface)? If not, please try
that quickly.

Now, if you can reproduce the above issue, then does providing a custom
ToolBoxItem resolve the issue (by forcing a @assembly reference into the
aspx page)?

I will send a sample if you cannot reproduce the above.

Thanks
Praveen

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:6t**************@TK2MSFTNGXA02.phx.gbl...
Hi Praveen,

After some further checking , I'm thinking the problem is likely
environment specific or project specific. Based on my local test, (a
control with another dependency), I didn't get the assembly not load
error.
I've tested using strong-named or not strong-named control assembly. As I
also mentioned in another thread, if possible, you can generate a
simplified version of your problem control and let me do some tests on my
local side.

Looking forward to your response. Thanks,

Steven Cheng
Microsoft Online Support

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

Nov 19 '05 #10
Oh, I see what you're saying. VS.NET adds the incorrect version in the directive?
Well, that's unfortunate. I rarely use the designer in VS.NET. Given these
sorts of quirks from VS.NET, it's good to know how to do these things manually.
-Brock
DevelopMentor
http://staff.develop.com/ballen
Brock,

Thanks for the reply. But, like I mentioned I do not want to add the
reference manually, I want my custom control to INSERT this
AUTOMATICALLY into the aspx file in which it was added, if possible.

Thanks
-Praveen


Nov 19 '05 #11
Thanks for your response Praveen,

From your further description, there seems still something difference from
your test and mine. AS you mentioned, you put the interfaceLib into GAC and
the control's assembly not in GAC yes? Also, at compile time , there is no
error and the control's assembly is correctly copied into the application's
private bin folder, yes? And is the interfacelib also copied into private
bin folder?

And at runtime, you got the error in your message, yes?

I'll assume the above steps and perform some tests on my side. If there is
anything incorrect, please feel free to let me know. Thanks,

Steven Cheng
Microsoft Online Support

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


Nov 19 '05 #12
Steven,

Yes, to all your questions. Except, no, the InterfaceLib is not copied to
the bin folder - copy local false - as discussed. I wonder what our
differences might be!

I also, tested this on 2 other systems - 1 with just 1.0 and one with just
1.1. Same problem there!

Hope you can reproduce.

Regards,
Praveen
Nov 19 '05 #13
Hi Praveen,

After some further testing, I think I've found the problem you encountered.
Yes, when making the interfaceLib strong-named and put in GAC, at runtime
(have draged the control onto page and compiled the project), we'll get
assembly not found. Also, if we put the interfacelib assembly in the
application's private bin directory(copy local), that issue not occur. (And
copy local is bydefault when we not strong-named the interfacelib that's
why I didn't repro it in former test). The cause of this seems like below:

the aspx is dynamic compiled at runtime, and when he found that there is a
DsControlLib dll referenced in page, it 'll try to find its dependent
assemly (interfacelib). However, it will treat the interfacelib as a
private assembly(not strong-named) , so it didn't lookup the GAC and
direclty search the private bin folder, if not found , the exception
thrown. That's why if we manually add the "@Assembly" reference which
specify the Full Assembly name of the interfacelib will make it work.

However, I'm not sure why the dynamic compiled page class will referencing
the strong-named assembly as private one, I'll consult some further experts
on this to see whether they have any further ideas. Anyway, I think this
problem is different from the one in my orginal post(use toolboxiitem to
manually add reference).

I'll update you if I got any new info. Thanks.

Steven Cheng
Microsoft Online Support

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

Nov 19 '05 #14
Steven,

Glad you could reproduce this now!

Actually, my reasoning is a bit different. When the runtime compiler
compiles the dynamically generated code, it encounters the "new
MathsControl();" line and figures that it needs access to the dscontrollib
where it's defined AND the interfacelib where the "ICalculate" is defined.
The dscontrollib is referenced through the @register tag but there is no
reference to the interfacelib assembly (it doesn't look up the
dscontrollib's manifest to determine where to find the interfacelib) - so it
chokes.

Like I mentioned before, this can be worked around by inserting assembly
referencing entries in the web.config or other config files, or by asking
the user to insert @assembly directives manually in the aspx files, but I
would like to avoid it. If I could insert "@assembly" directives
automatically from my control code, that would be awesome!

-Praveen
Nov 19 '05 #15
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. Programmatically 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><assemblies>.. like below:

<compilation
defaultLanguage="c#"
debug="true"

<assemblies>
<add
assembly="InterfaceLib,Version=1.0.0.0,Culture=Neu tral,PublicKeyToken=2bfe02
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.)

Nov 19 '05 #16
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-******@online.microsoft.com> wrote in message
news:dk*****************@TK2MSFTNGXA01.phx.gbl...
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. Programmatically 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><assemblies>.. like below:

<compilation
defaultLanguage="c#"
debug="true"
>

<assemblies>
<add
assembly="InterfaceLib,Version=1.0.0.0,Culture=Neu tral,PublicKeyToken=2bfe02
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.)

Nov 19 '05 #17
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(IComponent component)
{

base.Initialize (component);

IDesignerHost host = component.Site.Container as IDesignerHost;
IDesigner designer = host.GetDesigner(host.RootComponent);

// Calling GetHTMLFromWebControlTool with the following custom toolboxitem
will insert the
// Register directives for the type associated with that .
MethodInfo mi = designer.GetType.GetMethod("GetHTMLFromWebControlT ool",
BindingFlags.NonPublic | BindingFlags.Instance);
if(mi != null)
{
// DependantType is a custom type defined in DependantAssembly.dll
mi.Invoke(designer, new object[]{new
WebControlToolboxItem(typeof(SomeNamespace.Dependa ntType))});
}
}

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="SomeNamespace"
Assembly="DependantAssembly, Version=2.0.0.1, Culture=neutral,
PublicKeyToken=3d6dfsd1fdsd44c89" %>

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

Nov 19 '05 #18
Great Work Praveen!

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

IDesigner designer = host.GetDesigner(host.RootComponent);

MethodInfo mi = designer.GetType.GetMethod("GetHTMLFromWebControlT ool",

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.)

Nov 19 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: V. Srinivas | last post by:
Hi, I tried to install my strong named assembly to GAC. But I get the following error message - 1. If I drag and drop the assembly to GAC using windows explorer --------------------------- The...
1
by: Chuck Bowling | last post by:
Right now I'm trying to get a little deeper into C# and I'm exploring MSIL. I previously installed VS 2002. Later I uninstalled and upgraded to VS 2003. Now, in exploring my \Windows\assembly...
0
by: Praveen Ramesh | last post by:
Hi, Is there any way to add the @Assembly reference to the aspx files programmatically from inside a custom control (when it gets dropped on to the page from the toolbox)? I have a custom...
2
by: Brad | last post by:
I have one of those seemingly simple questions that evades/confuses me. I've created an assembly with bass classes (classes meant to be inherited in other assemblys). In a secondary assembly (my...
11
by: Juande | last post by:
Hello, My application needs load some dll's at runtime, Is there any way to do it?, can you show me a working example code? Many thanks
3
by: User1013 | last post by:
What is the recommended method of referencing libraries in Visual Studio 2005. Project references or Assembly references?
2
AHayes
by: AHayes | last post by:
This is actually my first post, so please forgive any forum "no-nos" in this post. _The Background For a work project, I need to port an ASP.Net (C#) search application to a SharePoint webpart....
2
by: chris fellows | last post by:
Can someone tell me if .NET 2.0 framework has a namespace to enable project references to be changed programmatically? My C# project files are stored in source control with DLL references but I...
0
by: Simon429 | last post by:
Hi, I got this really annoying problem. When I add a crystal report viewer into my asp.net Form, it automatically added duplicate assembly in my web.config as follows: <add...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.