Unable to find the requested Net Framework Data Provider - c#

Its a very specific error message, but I can't find the problem.
I have a windows service that needs to communicate with a database, for that I do the following:
string provider = "Devart.Data.PostgreSql";
DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
But it crashes, giving me this error:
System.ArgumentException: Unable to find the requested .Net Framework
Data Provider. It may not be installed. at
System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
I tried to move the Devart.Data.PostgreSql.dll to the bin folder, but the results are the same.
Then I checked my machine.config, and I have this:
<add name="dotConnect for PostgreSQL"
invariant="Devart.Data.PostgreSql"
description="Devart dotConnect for PostgreSQL"
type="Devart.Data.PostgreSql.PgSqlProviderFactory, Devart.Data.PostgreSql, Version=7.4.506.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
So it looks right, I didn't found any empty tags, found that in other SO posts, saying that removing those tags when empty could solve the problem, but I didnt had any.
So I don't know what is the problem.
I also created a console project, which uses the same .Net version, and with the exact code:
string provider = "Devart.Data.PostgreSql";
DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
And it works. Does anyone knows what could it be?

Related

SqlClientFactory error with EF 6 - Unable to determine the provider name for provider factory

I'm getting this error when I attempt access a collection within the context:
Unable to determine the provider name for provider factory of type 'Microsoft.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
My config has the standard connection string:
<add name="someDb" connectionString="Data Source=****;Initial Catalog=****;Integrated Security=SSPI;MultipleActiveResultSets=True;" providerName="Micorsoft.Data.SqlClient" />
This has been working fine for years (the providerName was System.Data.SqlClient), now trying to upgrade to Standard2.0 with EF 6.2.
Hoping there's a fix I can make to the connection string or in the DbContext, but I'm stumped.
Do you have NuGet installed if not goto NuGet package manager and install then
Simply add a NuGet dependency on Microsoft.Data.SqlClient and update
this may help you.

Store provider error with EF

I'm currently working on a MVC application which uses Entity Framework to interact with an Oracle Database.
Creating the edmx, adding and updating tables, all those actions are done without raising any error since I've installed all the Oracle providers I needed to make it work with .NET.
However, here's the thing : when I'm trying to run my app (which is done without errors), when the following line is executed, Visual Studio raises an error.
return PartialView("_GridViewRecruitmentPartial", model.Where(e => e.NON_ACTIVE != 1).OrderByDescending(e => e.EMPL_ID).ToList());
The error message :
The specified store provider cannot be found in the configuration, or
is not valid.
And the inner message:
Unable to find the requested .Net Framework Data Provider. It may not
be installed
I'm aware that something's wrong with EF and Oracle but can't see what. Plus, I've plenty of other projects using those 2 and everything went well.
Any guess?
I am not sure what are you using for data access. There are multiple Oracle providers... Anyway, you should probably use official Oracle Managed driver from Nuget.
When you install it from Nuget it should add itself to your web.config so everything works..
<entityFramework>
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client"
type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</providers>
</entityFramework>

Entity Framework 6: Unable to load the specified metadata resource

First, this is related to another question here on SO:
I've read and debugged my issue with the following SO article & blog:
MetadataException: Unable to load the specified metadata resource
and
http://blogs.teamb.com/craigstuntz/2010/08/13/38628/
BUT...I'm still having questions beyond just this 'fix'
I have a WebAPI (2.1), the connection string in my WebAPI is as so:
<connectionStrings>
<add name="ProjectEntities" connectionString="
metadata=res://*/ProjectModel.csdl|
res://*/ProjectModel.ssdl|
res://*/ProjectModel.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=192.168.0.1;
initial catalog=Project;
persist security info=True;
user id=***;
password=***;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
When I call ToList() on a DbSet in my WebAPI (pseudo code):
DbContext _DbContext = new ProjectEntities();
DbSet<TEntity> _dbSet = _DbContext.Set<TEntity>();
_dbSet.ToList();
It works great!
When I call the same from within a WINDOWS SERVICE, I get the following error:
The app.config entry for the connection string is exactly the same as the web.config:
<connectionStrings>
<add name="ProjectEntities" connectionString="
metadata=res://*/ProjectModel.csdl|
res://*/ProjectModel.ssdl|
res://*/ProjectModel.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=192.168.0.1;
initial catalog=Project;
persist security info=True;
user id=***;
password=***;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
Now, the blog shows to reference the dll manually as so:
<connectionStrings>
<add name="ProjectEntities" connectionString="
metadata=res://Project.Data.dll/ProjectModel.csdl|
res://Project.Data.dll/ProjectModel.ssdl|
res://Project.Data.dll/ProjectModel.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=192.168.0.1;
initial catalog=Project;
persist security info=True;
user id=***;
password=***;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
This does NOT work/fix the issue
The only way I've been able to fix it, is to use the fully qualified name:
<connectionStrings>
<add name="ProjectEntities" connectionString="
metadata=res://Project.Data, Version=1.6.0.0, Culture=neutral, PublicKeyToken=null/ProjectModel.csdl|
res://Project.Data, Version=1.6.0.0, Culture=neutral, PublicKeyToken=null/ProjectModel.ssdl|
res://Project.Data, Version=1.6.0.0, Culture=neutral, PublicKeyToken=null/ProjectModel.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=192.168.250.125\sqlexpress;
initial catalog=Project;
persist security info=True;
user id=***;
password=***;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Why does this work like this? Why would this work in a web project, but not a windows service project?? I recently changed from EF5 to EF6, and this error has popped up - all this code worked previous to upgrading EF. Does anyone have any insight as to why and how/if I can just use * for the dll name in my connection string?
I thought it was an issue of where the service .exe was running and a file wasn't copied locally, but nope, the Project.Data.dll is there and it's the right version.
I used FusionLog to try and find the error, and no luck there. I'm pretty confused.
Why this happens?
The issue you are having is just a result of extra security measures to prevent binary planting or DLL hi-jacking attack (read more) when running your application as as windows service.
Why should I care?
As you probably know, there is a specific, well documented order in what every referenced DLL file is looked up. Usually it starts to search DLL in current application directory and then goes away to more "public" locations like PATH folders, GAC, etc.
Main idea of binary planting is to plant malicious DLL file in a folder which is checked before folder of the legit DLL. Loading such malicious DLL would allow attacker to gain control over the system.
Usually windows services run under elevated account (LocalSystem, LocalService, NetworkService, etc) therefore windows services are good target for binary planting attacks.
What can I do?
Microsoft have taken extra precaution steps to reduce security risks and there is a good reason for that. But you can try to work around you issues.
1) Current directory is not what you expect
Windows service starts in system folder (usually something like C:\Windows\System32)
Good news are that it is very easy to fix. You just have to change current directory on services startup.
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
See blog post from Phil Haack;
2) Read documentation thoroughtly
According to EF documentation, wildcard character has special meaning and it limits places where runtime will look for DLL files:
If you specify a wildcard (*) for assemblyFullName, the Entity
Framework runtime will search for resources in the following
locations, in this order:
1) The calling assembly.
2) The referenced assemblies.
3) The assemblies in the bin directory of an application.
As your working folder is set to system folder and you references probably are not there, EF might end up looking in wrong places and your assemblies containing resources might not be loaded.
3) Stay safe with fully qualified assembly names
Although I am not completely sure about this and haven't tested, but Microsoft just might have disallowed Windows services to load DLL without providing fully qualified assembly name to reduce risk of injecting malicious DLL files;
Good read on securing your Windows services here (specially chapter 5).
4) Debug it!
EF6 happens to be open source project. This means that you can get full source of it and debug it. You can find project on CodePlex here.
Copy the dll containing ProjectEntities to different path and then reference the same in your service project.
I'm afraid I wasn't able to reproduce the error that you received, or answer why you needed to change the metadata.
That said, I did learn that, for the EF connection string, the Windows Service required a different provider connection string than the WebApi did.
The following are the steps to reproduce your error. The only difference is that I'm using localdb not SQLExpress.
The resultant code from my steps-to-reproduce is online at GitHub here: https://github.com/bigfont/EntityFrameworkWindowsServiceWebApi.
Here are those steps:
Create Web API Project
Create ASP.NET Web API 2 Empty Project (MyWebApi)
With NuGet, Install-Package EntityFramework -ProjectName MyWebApi
Add a new ADO.NET Entity Data Model called MyProjectModel.
Add an Entity called Entity1.
Generate the database from the model, calling it MyProject and using localdb.
Run the db creation script on (localdb)\v11.0
Add a new WebApi Controller named ValuesController with a Get method that queries the database.
Test by running in Visual Studio and going to localhost:123456/api/get
See: https://msdn.microsoft.com/en-us/data/jj205424.aspx
Create Windows Service Project
Create Windows Service (MyWindowsService)
Use NuGet, Install-Package EntityFramework -ProjectName MyWindowsService
Add a new ADO.NET Entity Data Model called MyProjectModel.
Add an Entity called Entity1.
Generate the database from the model, calling it MyService, using localdb.
Run the db creation script on (localdb)\v11.0
Add to the OnStart method some code that queries the database.
Add NT AUTHORITY\SYSTEM as a localdb Login and as a MyService db User.
Test by installing, starting, and writing to file:
PowerShell Installation, Startup, and Uninstall
Release> installutil .\MyWindowsService.exe
Release> Start-Service MyService
Release> installutil .\MyWindowsService.exe /u
localdb connection string in the Windows Service
In the connection string for the Windows Service, I wasn't able to use (localdb)\v11.0. Instead, I needed to use the named pipe. I found the named pipe with this command line:
> SqlLocalDB.exe info v11.0
Name: v11.0
Version: 11.0.2100.60
Shared name:
Owner: MY_COMPUTER\Shaun.Luttin
Auto-create: Yes
State: Running
Last start time: 2015-04-09 5:54:34 PM
Instance pipe name: np:\\.\pipe\LOCALDB#1010101\tsql\query
The resultant connection string, using the Instance pipe name, looked like this.
<connectionStrings>
<add name="MyProjectModelContainer"
connectionString="
metadata=
res://*/MyProjectModel.csdl|
res://*/MyProjectModel.ssdl|
res://*/MyProjectModel.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=np:\\.\pipe\LOCALDB#4BCE6D95\tsql\query;
initial catalog=MyService;
Integrated Security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Whereas the WebApi connection string looked like this:
<add name="MyProjectModelContainer"
connectionString="
metadata=
res://*/MyProjectModel.csdl|
res://*/MyProjectModel.ssdl|
res://*/MyProjectModel.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=(localdb)\v11.0;
initial catalog=MyProject;
integrated security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
See also: http://www.connectionstrings.com/sql-server-2012/
Needing to use a different connection string with a Windows Service that we do with a WebApi project is a similar problem to what you found. From Sql Server Management Studio, from Visual Studio, and from the WebApi, we can connect by calling the data source (localdb)\v.11 whereas from a Web Service we need to call it by it's instance named pipe.
Here's a suspicion: It might be that there are multiple instance of localdb on the computer, and that we needed to absolutely specify which one we want to use. Unfortunately, this doesn't help answer why you needed to change the metadata.
This is a similar though different problem than what you faced, because you needed to change the Entity Framework metadata whereas I needed to change the provider connection string. Coincidence?
Please follow the steps bellow:
1.Write click on edmx file and then click open with of the related entity.
2.Select xml editor and click open.
3.Scroll from top to bottom of the .edmx xml file and look for any error marks.
4.If you mind errors then fix that.
5.Rebuild the solution and if no errors found then congratulations :)

Deploying Oracle Instant Client alongside existing Oracle installations

I have developed a new application that uses Entity Framework to access an Oracle database. This is working as expected locally, using the latest version of ODP.NET. I am now trying to deploy this application on a production server running many other legacy applications. Ideally I would like my new application to make use of its own ODP.NET / Oracle dlls and not have to change the existing Oracle install on the prod server.
I followed this guide:
http://jeremybranham.wordpress.com/2011/04/25/oracle-instant-client-with-odp-net/
Which seems to have had some success based on the comments.
However, I get the following exception when attempting to create the entities object:
Outer Exception
Exception has been thrown by the target of an invocation.
Inner Exception
The type initializer for 'Oracle.DataAccess.Client.OracleClientFactory' threw an exception.
at System.RuntimeFieldHandle.GetValue(RtFieldInfo field, Object instance, RuntimeType fieldType, RuntimeType declaringType, Boolean& domainInitialized)
at System.Reflection.RtFieldInfo.InternalGetValue(Object obj, Boolean doVisibilityCheck, Boolean doCheckConsistency)
at System.Reflection.RtFieldInfo.GetValue(Object obj)
at System.Data.Common.DbProviderFactories.GetFactory(DataRow providerRow)
at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
at MyAppMVC.Models.DataModels.STSProcedureEntities..ctor()
at MyAppMVC.Services.MyService.GetPersons(String lastName)
Although you appear to have resolved your problem by installing an additional client you could possible have added the following config in you app / web.config file.
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.DataAccess.Client" />
<add name="Oracle Data Provider for .NET" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET"
type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.112.3.0, Culture=neutral,
PublicKeyToken=89b483f429c47342"/>
</DbProviderFactories>
</system.data>
And dropped the binaries in your local bin folder.
Answering my own question here. We ultimately installed a second version of Oracle, using the default settings which put the new version into a client_2 folder.
D:\oracle\product\11.2.0\client (old version)
D:\oracle\product\11.2.0\client_2 (new version)
The only additional install step was dropping in the previous TNSNAMES.ORA file into the new client_2. This file is located in client\network\admin using the file path above.
Because all of our applications were set to specifically seek out their correct Oracle versions (SpecificVersion=true) everything mostly worked without modification. For those that did not we had to drop in the older version of Oracle.DataAccess.dll (from client, not client_2) into that application's bin folder.

Configuring Enterprise Library 5.0 Data Access Application Block

I'm trying to figure out how to configure the enterprise library 5.0 Data Access Application Block.
When running my unittest, I get the following error:
Microsoft.Practices.ServiceLocation.ActivationException was caught
Message=Activation error occured while trying to get instance of type Database, key "PokerAdviserProvider"
InnerException: Microsoft.Practices.Unity.ResolutionFailedException
Message=Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "PokerAdviserProvider".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.
The line of code where I get this:
var db = DatabaseFactory.CreateDatabase("PokerAdviserProvider");
App.config:
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
</configSections>
<dataConfiguration defaultDatabase="PokerAdviserProvider" />
<connectionStrings>
<add name="PokerAdviserProvider" connectionString="server=(localhost);Initial Catalog=PokerAdviser;uid=abc;pwd=xyz"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I've been googling around a bit and found some answers that these settings should also be put in the app.Config of my unittest-project, but that didn't make a difference.
I'm a bit stuck here, so any help is highly appreciated.
Edit:
I referenced the correct dll's (the ones from Program Files, not from the source code), so that isn't the problemneither.
I finally fixed this problem:
Error: Activation error occured while trying to get instance of type Database, key "<database name>"
Inner Exception: Resolution of the dependency failed, type = Microsoft.Practices.EnterpriseLibrary.Data.Database
I was running VS 2010 on windows 7, Enlib 5.0. The following worked for me. Wanted to spread the word around
Make sure you have proper reference to Microsoft.Practices.Unity.dll
Get the latest service pack for VS 2010
Finally figured it out. I use the DAAB in a class-library of my webservice and thought I had to create an app.config in that library. Should have know that this could not work. My mind was probably far far away when doing this...
I did the configuration in the web.config of the webservice and all runs smoothly now.
Refer to these two good posts post1 & post2 about Enterprise Library Configuration

Categories

Resources