The class library project uses the connection string defined in Web. config of the website.

Source: Internet
Author: User
Tags connectionstrings
First, let's explain that, after a rough experiment, the class library project and website do not need another solution. As long as the class library referenced by the website can access the connection string in Web. config. P.s is just a simple trial, so there may be errors. I hope you will testify.

In a class library project CodeTo access the connection string in the web. config file that references your website, the class library project must reference system. Web. ProgramAnd then you need to reference the system. Configuration assembly. It is a bit confusing about the latter, which will be discussed later.

Then you can use
System. Web. configuration. webconfigurationmanager. connectionstrings
Access the connection string, just like the code in the website.
Note the following: if the class library is not referenced on the website but is referenced in a project without a configuration file, the preceding statement will not run abnormally, it returns NULL.

Finally. configuration gets worse. connectionstrings is defined in this Assembly, while webconfigurationmanager is defined in system. in web, such a design requires that another assembly be referenced at the same time by using webconfigurationmanager, And the only purpose of referencing that assembly is to access a property of webconfigurationmanager, webconfigurationmanager depends on a specific class defined in other assembly, which obviously violates the interface inversion principle and brings troubles to user calls. It is inexplicable to design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.