Connection string in app.config in a class library - wcf

I am creating solution and inside I have three projects:
A WCF Service Library Project
A DataAccess Project (Class Library)
A Web site for hosting WCF service
The implementation of the service is on the project # 1, but in order to access the DataBase I use a second project that implements the data access using a class library project.
That problem is in order to get data access I need to configure a connection string, but that connection string must be configurable in a production environment, I meant in production I am going to deploy the site, which is a very simple project that contains only a reference WCF Service Library Project then a guy from database department will configure the connection string.
In development I have an app.config on the data access project but when I do the release that app.config is embedded on the dll.
Any ideas how can we achieve our purpose

The connection string must be in the application configuration file of the executing assembly. This means that you can provided the configuration file for your assembly along with the assembly itself but anyone who wants to use your assembly must update their configuration file to include the values that your assembly relies on.

The connection string in your app.config (data layer) is not embedded in the dll.
If you look in the app.config file in your data layer project, you will probably have a connectionStrings section. you need to put the connectionStrings in the web.config of your WCF service website.
This can be configured in your production environment.

I had a mistake, I was using a different name on the web.config of the WCF site, I just copy the the exact part of the app.config to the web.config and its working now.
Thanks for your help

Related

Connection String not found in Config File

I am new to ADO.NET, I am using MYSQL with WCF.
I have a WCF project with ADO.NET, the SalesSolLIB project has reference of SalesSolWCF, and SalesSolView has reference of SalesSolLib project.
The problem is ADO.Net entities are created in WCF and when run, the error comes as although the name is present in web.config file.
"No connection string named 'SalesSolEntities' could be found in the application config file."
WebConfig File
<connectionStrings>
<add name="SalesSolEntities" connectionString="metadata=res://*/SalesSolDBModel.csdl|res://*/SalesSolDBModel.ssdl|res://*/SalesSolDBModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=surpavan;persist security info=True;database=salessol;old guids=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
Code in WCF file:
Dim DB As New SalesSolEntities()
DB.Database.Connection.Open()
DB.Database.Connection.Close()
Return "test"
Code in SalesSolLib:
Dim service As New SalesSolWCF.ContactService
Dim errormsg As String = service.SaveContact(wcfcontact, IsNewContact)
However, through browsing, I found that connection string must be all the projects, however, why would I need to use WCF if connection string is needed in all projects, please advice how this can be solved, I don't want to put connection string in all projects or did I do wrong?
I am using Entity Framework 5.0 and the WCF Service Application as wcf project type.
You are calling your WCF services the wrong way. You do not add the reference by simply adding reference to DLL, you need to expose WCF endpoints via MEX, and then add reference by using "Add Service Reference".
What you've done now is that you're using your WCF not as a a service but just as a simple class that you make an instance off and then call it's methods. That is not how WCF services should be used, and that is why you get "connection string" missing error. Method in your class talks to database, so when you create instance of that class in other project, and call that method, that project must also have matching connection string. Basically it is all wrong, should not be done this way.
WCF services must be hosted either as self-hosted in your application or within IIS. I'd suggest that you go and take some basic WCF tutorial, otherwise what I just wrote will not make much of a sense to you.

Unity Configuration in Referenced Assembly not recognized

I have a rather expensive library which I've inherited from another project. This assembly is using interception via Unity and requires an elaborate Web.config for design time configuration. I am attempting to use this assembly from within a MSMQ WCF Service, and I'm receiving errors when the code inside the assembly attempts to open DB connections that are hosted by Enterprise Library.
Resolution of the dependency failed, type =
"ServicesImplementation.EntityMaster.IEntityRepository", name =
"(none)". Exception occurred while: while resolving. Exception is:
InvalidOperationException - The current type,
ServicesImplementation.EntityMaster.IEntityRepository, is an interface
and cannot be constructed. Are you missing a type mapping?
My question, is it required that I assume all configuration responsibilities from my WCF Service, or is this configuration encapsulated within the referenced assembly.
You must include all configuration in your .config file. It is defalult source for application to get it config data. Also notice, that if you host in IIS, you should use web.config, if not(Windows Service or app) - use app.config. It is possible to extract section configuration to another file and reference it from your main config.
For example:
Assembly1 contains service definition and host logic.
Assembly2 actualy hosts Assembly1 service.
In this case all service configuration must reside in Assembly2 *.config file.

WCF service reference in class library

I added wcf service (exposed by biztalk) proxy and app.config file in VS 2005 class library project and i am calling this class library methods from windows application. In this case getting error while creating service instance, so i moved app.config file from class library to window applcation, now working fine.
Question: If i will change service url from machine001 to machine002 in config file (from bin folder but not from application) and run the app from exe file. Will it work without build.
Class library configuration always depends on configuration file (web.config / app.config) of parent application that is really using it. And Parent application should be a console / winform / ASP.NET application and can be a windows service. Any change in WinForm's app.config will change the behavior of your class library.
To Answer your question, Yes if you change service url from machine001 to machine002 in config file of windows application it will work if machine002 is hosting the WCF service.
Hope it clears your doubt.

WCF- how to add multiple services to Service.svc?

I have 4 services running via a service host project, which communicate fine with my asp.net application when the ASP.NET development server hosts them through VS for debugging.
I am trying to deploy these to IIS on a windows server 2008 machine, using WAS.
I have the project set up as an application in IIS, and have copied the entire config section from app.config in servicehost project to web.config of the IIS site.
After a few compliation issues, I now get a directory listing when i navigate to http://localhost:8000/Services
I have also created a Service.svc file, which contains
<%#ServiceHost Service=MyApp.AddressService %>
When I navigate to localhost:8000/Services/AddressService, I get a message that i've created a service, and appending ?wsdl gives me the xml to create a client.
Problem is, I get an error when I try to add any more services to the .svc file.
Should I be using service.svc to configure multiple services, or is there a different way using WAS?
How can I expose my other three services through the same application?
Thank you!
You cannot add multiple service to a SVC file. One SVC file = one service class. No way to change that.
However: you can definitely implement multiple service interfaces on your service class:
public class YourService : IService1, IService2, IService3
{
...
}
and then you have one SVC file = one service (implementation) class = 3 service contracts.
In .NET 4 / WCF 4, you'll be able to define URL's for service in your web.config, and you don't need the SVC files anymore.
See this blog post here or this one here for more info, if .NET 4 is an option for you.

How to use a WSDL file to create a WCF service (not make a call)

I have an old WSDL file and I want to create a server based on this WSDL file.
The WSDL is generated from a ASMX (I suppose but I am not sure).
How can I achieve this ?
original question where the OP thought he needed to create a client based on the WSDL.
Using svcutil, you can create interfaces and classes (data contracts) from the WSDL.
svcutil your.wsdl (or svcutil your.wsdl /l:vb if you want Visual Basic)
This will create a file called "your.cs" in C# (or "your.vb" in VB.NET) which contains all the necessary items.
Now, you need to create a class "MyService" which will implement the service interface (IServiceInterface) - or the several service interfaces - and this is your server instance.
Now a class by itself doesn't really help yet - you'll need to host the service somewhere. You need to either create your own ServiceHost instance which hosts the service, configure endpoints and so forth - or you can host your service inside IIS.
There are good resources out there if you know what to search for. Try "Contract First" and WCF. or "WSDL First" and WCF.
Here is a selection:
Basic overview of WSDL-First development with WCF and SvcUtil.exe.
WSCF - A free add-in to Visual Studio enabling Contract-First design with WCF
Introduction to WSCF
A walkthrough of using WSCF
The WSCF project page on CodePlex (WSCF is now open source)
Article on how to design "WCF-Friendly" WSDL
Use svcutil.exe with the /sc switch to generate the WCF contracts. This will create a code file that you can add to your project. It will contain all interfaces and data types you need to create your service. Change the output location using the /o switch, or you can find the file in the folder where you ran svcutil.exe. The default language is C# but I think (I've never tried it) you should be able to change this using /l:vb.
svcutil /sc "WSDL file path"
If your WSDL has any supporting XSD files pass those in as arguments after the WSDL.
svcutil /sc "WSDL file path" "XSD 1 file path" "XSD 2 file path" ... "XSD n file path"
Then create a new class that is your service and implement the contract interface you just created.
You could use svcutil.exe to generate client code. This would include the definition of the service contract and any data contracts and fault contracts required.
Then, simply delete the client code: classes that implement the service contracts. You'll then need to implement them yourself, in your service.
Using the "Add Service Reference" tool in Visual Studio, you can insert the address as:
file:///path/to/wsdl/file.wsdl
And it will load properly.