Link to home
Start Free TrialLog in
Avatar of pclarke7
pclarke7

asked on

VS2013 WCF C# application 32/64-bit issues

Hello,
I have a c# WCF service library project which is trying to read data from an excel file as follows:

             string con = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                                    @"Data Source=c:\temp\Book2.xlsx;" +
                                    @"Extended Properties='Excel 8.0;HDR=Yes; IMEX=1;'";

            using (OleDbConnection connection = new OleDbConnection(con))
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                var TranArr = new string[999, 20];
                using (OleDbDataReader dr = command.ExecuteReader())
                { etc...

This results in an error  "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine."

When I copy the same code into a c# console application it works fine. I am running Windows 7 64-bit and 32-bit Office 2007 excel. In the Build properties the platform is set to "Any CPU". In the WCF project the 32-bit preferred is unchecked and greyed out but in the Console application it is ticked. I have tried downloading Microsoft Access Database Engine 2010 Redistributable , but this has not helped.

Has any one come across this before and more importantly does anyone have a fix ?

regards
Pat
Avatar of Kalpesh Chhatrala
Kalpesh Chhatrala
Flag of India image

you need to set Platform Target = x86.

Sample screenshot attached.

User generated image
Avatar of pclarke7
pclarke7

ASKER

I have already tried setting the platform Target to X86 but get the same result. Can this have anything to do with the fact that office 2007 is 32bit ?
x86 is 32-bit. If you have a solution with multiple projects, if has to be set for all projects.

You say that you have downloaded the database engine. Did you download the correct version? Did you remove any previous version before installing the one you downloaded as suggested in the install instructions?
Actually I was wrong,
when I set platform target to X86 and run the application I get the following error:  Any ideas

regards
Pat

WcfSvcHost encountered a critical error and must exit. This may be caused by invalid configuration file. Please inspect additional information below for detail.

System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\User\documents\visual studio 2013\Projects\DCRules2\DCRules2\bin\Debug\DCRules2.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'file:///C:\Users\User\documents\visual studio 2013\Projects\DCRules2\DCRules2\bin\Debug\DCRules2.dll'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   at Microsoft.Tools.SvcHost.ServiceHostHelper.LoadServiceAssembly(String svcAssemblyPath)

=== Pre-bind state information ===
LOG: Where-ref bind. Location = C:\Users\User\documents\visual studio 2013\Projects\DCRules2\DCRules2\bin\Debug\DCRules2.dll
LOG: Appbase = file:///C:/Users/User/documents/visual studio 2013/Projects/DCRules2/DCRules2/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Users\User\documents\visual studio 2013\Projects\DCRules2\DCRules2\bin\Debug\DCRules2.dll.config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///C:/Users/User/documents/visual studio 2013/Projects/DCRules2/DCRules2/bin/Debug/DCRules2.dll.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
can you create small sample project and send here ?
It's probably that DCRules2.dll or one of the dlls that it uses itself is 64-bit.

You cannot use 64-bit dlls with 32-bit applications.
Hi Kalpish,
I will send on an example later this evening of a working console application and the same code failing as a WCF applications.

Hi James,
My understanding is that VS 2013 is 64-bit and Windows 7 is 64-bit. Therefore I am creating a 64-bit application which is failing to read a MS Excel 2007 spreadsheet when it is a WCF library application. However when the same code is compiled as a Console application in VS 2013 it reads the same MS Excel 2007 spreadsheet successfully.

VS Studio               64-bit
Windows 7            64-bit
MS Office 2007    32-bit

WCF library application  - Fails to read excel file
Consle application          - Successfully reads file

regards
Pat
Hi Kalpish,
as promised, below are two examples. The 1st is a simple VS 2013 Console application. which will read an excel file  c:\temp\book2.xlsx (just populate 1st two columns with anything). The 2nd example uses the same code but within a VS 2013 WCF library application. The console applications runs successfully. The WCF application fails with  "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine." on the connection.open() statement.

regards
Pat


Example 1 - Create a console application in VS 2013

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string con = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                                    @"Data Source=c:\temp\Book2.xlsx;" +
                                    @"Extended Properties='Excel 8.0;HDR=Yes; IMEX=1;'";

            using (OleDbConnection connection = new OleDbConnection(con))
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);

                var TranArr = new string[999, 20];
                int i = 0;

                using (OleDbDataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        //TranArr[i, 1] = dr[0].ToString();
                        //TranArr[i, 2] = dr[1].ToString();
                        TranArr[i, 1] = dr.GetString(0);
                        TranArr[i, 2] = dr.GetString(1);


                        Console.WriteLine(TranArr[i, 1]);
                        Console.WriteLine(TranArr[i, 2]);
                    }
                }
            }

            Console.ReadKey();

        }
    }
}




Example 2 - Create a WCF Library project in VS 2013

Service1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.OleDb;             //Added this

namespace WcfServiceLibrary4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
//ADDED FROM HERE                                      
            string con = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                                        @"Data Source=c:\temp\Book2.xlsx;" +
                                        @"Extended Properties='Excel 8.0;HDR=Yes; IMEX=1;'";

                using (OleDbConnection connection = new OleDbConnection(con))
                {
                    connection.Open();
                    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);

                    var TranArr = new string[999, 20];
                    int i = 0;

                    using (OleDbDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            //TranArr[i, 1] = dr[0].ToString();
                            //TranArr[i, 2] = dr[1].ToString();
                            TranArr[i, 1] = dr.GetString(0);
                            TranArr[i, 2] = dr.GetString(1);


                            Console.WriteLine(TranArr[i, 1]);
                            Console.WriteLine(TranArr[i, 2]);
                        }
                    }
                }

                Console.ReadKey();
//ADDED TO HERE                
           return string.Format("You entered: {0}", value);
            }
       
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}
send me Projects File. so i can identify exact issue in your application.
both console & wcf solutions uploaded
I think Kalpesh meant to receive the whole solution, not just the solution files.
Hi Kalpesh,

for this sample project I have
folders Bin, Object, properties and
Iservice1.cs,
Service1.cs,
WcfServiceLibrary4 project file and
WcfServiceLibrary4.csproj

which of these do you want me attach ?

By the way, I uninstalled Office 2007 and installed Office 2010 and the same WfcServiceLibrary4 WCF application can now read the excel file. So, whilst it is now working I am still no wiser as to why office 2007 would not allow WCF to read excel file.

regards
Pat
ASKER CERTIFIED SOLUTION
Avatar of Kalpesh Chhatrala
Kalpesh Chhatrala
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Issue remains un-resolved. However a work around of moving to Office 2010 has resolved the problem. Won't help others experiencing this issue with office 2007. Giving points to Kalpesh for his input in trying to resolve.