Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Imported namespaces

Reply
 
Old 08-20-2008   #1 (permalink)
MCM


 
 

Imported namespaces

What is the difference between importing a namespace at the:

1. Project level
2. web.config level
3. Page level

What are the requirements/benefits of each?

If I import a namespace such as System.Collections at the Project level and
the user calls a page that does not use that namespace, is that wasting
resources? Am I better off specifying namespaces for individual pages only?

When exactly are the namespaces in web.config used? Clearly if I do not
include a namespace for a page (or at the Project level) I will get a
compilation error. But why would a namespace ever have to be imported in
web.config? How is that used?

My System SpecsSystem Spec
Old 08-20-2008   #2 (permalink)
Steven Cheng [MSFT]


 
 

RE: Imported namespaces

Hi MCM,

As for the "project level" importing, do you mean the "Imported namespaces"
for visual basic projects? This is one facility provided by VB.NET project
so that you do not need to explcitly import those namespaces in each code
file. this just help add namespace import , but it still rely on those
assemblies you have "referenced", this is the ultimate things that will
affect your application's generated binary code.


For ASP.NET, the <assemblies> section in web.config is not simply importing
namespace, but reference assemblies. For example, the following section
means the four assemblies will be automatically referenced when each of the
ASP.NET aspx page get dynamic compiled(ASP.NET use dynamic compilation)

============
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
========================

Yes, if you add two much useless assemblies here, it will impact your page
assemblies(generated dynamically or via precompilation)'s size. And you can
manually customize the list to minimize it(If you're sure how many ones are
exact necessary).

For page level Import, it is just like code file level import. It make the
certain namespace visible in that page's aspx template scope.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Quote:

>From: =?Utf-8?B?TUNN?= <MCM@xxxxxx>
>Subject: Imported namespaces
>Date: Wed, 20 Aug 2008 14:59:01 -0700
>
>What is the difference between importing a namespace at the:
>
>1. Project level
>2. web.config level
>3. Page level
>
>What are the requirements/benefits of each?
>
>If I import a namespace such as System.Collections at the Project level
and
Quote:

>the user calls a page that does not use that namespace, is that wasting
>resources? Am I better off specifying namespaces for individual pages only?
>
>When exactly are the namespaces in web.config used? Clearly if I do not
>include a namespace for a page (or at the Project level) I will get a
>compilation error. But why would a namespace ever have to be imported in
>web.config? How is that used?
>
My System SpecsSystem Spec
Old 08-21-2008   #3 (permalink)
MCM


 
 

RE: Imported namespaces

> As for the "project level" importing, do you mean the "Imported namespaces"
Quote:

> for visual basic projects?
Yes.

Quote:

> This is one facility provided by VB.NET project
> so that you do not need to explcitly import those namespaces in each code
> file.
But whch is better? Import at the project level or import at the page level?
If I have 10 pages and only 3 pages use a certain namespace, will I be
slowing down the other 7 pages by importing at the project level?

Quote:

> this just help add namespace import , but it still rely on those
> assemblies you have "referenced", this is the ultimate things that will
> affect your application's generated binary code.
Referenced at the project level or in web.config? They are different -
should I keep them syncronized?

Quote:

> For ASP.NET, the <assemblies> section in web.config is not simply importing
> namespace, but reference assemblies. For example, the following section
> means the four assemblies will be automatically referenced when each of the
> ASP.NET aspx page get dynamic compiled(ASP.NET use dynamic compilation)
>
> ============
> <assemblies>
> <add assembly="System.Core, Version=3.5.0.0, Culture=neutral,
> PublicKeyToken=B77A5C561934E089"/>
> <add assembly="System.Web.Extensions, Version=3.5.0.0,
> Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
> <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0,
> Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
> <add assembly="System.Xml.Linq, Version=3.5.0.0,
> Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
> </assemblies>
> ========================
>
> Yes, if you add two much useless assemblies here, it will impact your page
> assemblies(generated dynamically or via precompilation)'s size. And you can
> manually customize the list to minimize it(If you're sure how many ones are
> exact necessary).
Same question as above... What is the difference between assemblies
referenced at the project level and assemblies referenced in web.config?

My System SpecsSystem Spec
Old 08-21-2008   #4 (permalink)
Steven Cheng [MSFT]


 
 

RE: Imported namespaces

Thanks for your reply MCM,

For your further questions, here is the answer inline:

But whch is better? Import at the project level or import at the page
level?
If I have 10 pages and only 3 pages use a certain namespace, will I be
slowing down the other 7 pages by importing at the project level?
===============================================
For your code behind page class/code, I suggest you use project level
imports since that can help you save the work to declare them in every code
file. However, sometimes (for ASP.NET app), you will write server-side code
in aspx template, in such cases, I think you should always explicitly
import namespace in aspx page, otherwise, you'll need to reference class
via full namespace+classname. Importing namespace will not make page
running slowly, it just help the compiler find classes at compile time(even
for dynamic compilation, its impact is negligible). It won't add assembly
size either.


Referenced at the project level or in web.config? They are different -
should I keep them syncronized?
=================================
For ASP.NET 2.0/vs 2005/2008, there are two web project type(web site
project and web application project). Project Level reference are useful
for web applicaiton project while for web site project, it is
projectless(no much project setting like normal .net project). I suggest
you have a look at this two different project types first:

http://msdn.microsoft.com/en-us/libr...80(VS.80).aspx

http://www.google.com/search?hl=en&q...ication+projec
t

http://webproject.scottgu.com/


As for "web site project", I suggest you always use web.config's reference
list while for "web application project", you can simply use project level
reference. BTW, if you're using Visual studio IDE, you simply use "Add
Reference" menu to reference assemblies and visual studio IDE will help you
do it correctly(according to different project model).



Same question as above... What is the difference between assemblies
referenced at the project level and assemblies referenced in web.config?
============================================
Same as above, the difference is due to the different project type.
Generally, project level reference is used when the certain project build
and generate assemblies at design-time(when you build the project). However
for ASP.NET 2.0 application, it by default use a "web site project" model
which use dynamic compilation(do not generate assembly when you build
project at design-time). Thus, it is necessary to tell the application
which assemblies to reference when it perform dynamic compilation at
runtime(and the web.config assemblies list is used to tell the compiler for
dynamic compilation at runtime).

Some other reference about ASP.NET 2.0 compilation:

http://www.west-wind.com/presentatio...tCompilation.a
sp

http://odetocode.com/blogs/scott/arc...1/15/2464.aspx

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.






--------------------
Quote:

>From: =?Utf-8?B?TUNN?= <MCM@xxxxxx>
>References: <D4F88C53-3669-4FED-9737-847A61734A28@xxxxxx>
<lVcUzE0AJHA.1688@xxxxxx>
Quote:

>Subject: RE: Imported namespaces
>Date: Thu, 21 Aug 2008 11:39:00 -0700
Quote:

>
Quote:

>> As for the "project level" importing, do you mean the "Imported
namespaces"
Quote:
Quote:

>> for visual basic projects?
>
>Yes.
>
>
Quote:

>> This is one facility provided by VB.NET project
>> so that you do not need to explcitly import those namespaces in each
code
Quote:
Quote:

>> file.
>
>But whch is better? Import at the project level or import at the page
level?
Quote:

>If I have 10 pages and only 3 pages use a certain namespace, will I be
>slowing down the other 7 pages by importing at the project level?
>
>
Quote:

>> this just help add namespace import , but it still rely on those
>> assemblies you have "referenced", this is the ultimate things that will
>> affect your application's generated binary code.
>
>Referenced at the project level or in web.config? They are different -
>should I keep them syncronized?
>
>
Quote:

>> For ASP.NET, the <assemblies> section in web.config is not simply
importing
Quote:
Quote:

>> namespace, but reference assemblies. For example, the following section
>> means the four assemblies will be automatically referenced when each of
the
Quote:
Quote:

>> ASP.NET aspx page get dynamic compiled(ASP.NET use dynamic compilation)
>>
>> ============
>> <assemblies>
>> <add assembly="System.Core, Version=3.5.0.0,
Culture=neutral,
Quote:
Quote:

>> PublicKeyToken=B77A5C561934E089"/>
>> <add assembly="System.Web.Extensions, Version=3.5.0.0,
>> Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
>> <add assembly="System.Data.DataSetExtensions,
Version=3.5.0.0,
Quote:
Quote:

>> Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
>> <add assembly="System.Xml.Linq, Version=3.5.0.0,
>> Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
>> </assemblies>
>> ========================
>>
>> Yes, if you add two much useless assemblies here, it will impact your
page
Quote:
Quote:

>> assemblies(generated dynamically or via precompilation)'s size. And you
can
Quote:
Quote:

>> manually customize the list to minimize it(If you're sure how many ones
are
Quote:
Quote:

>> exact necessary).
>
>Same question as above... What is the difference between assemblies
>referenced at the project level and assemblies referenced in web.config?
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Creating a cmdlet using csc.exe - missing namespaces PowerShell
Puzzled about namespaces PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46