• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1207
  • Last Modified:

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
0
pclarke7
Asked:
pclarke7
  • 9
  • 4
  • 2
  • +1
1 Solution
 
Kalpesh ChhatralaSoftware ConsultantCommented:
you need to set Platform Target = x86.

Sample screenshot attached.

WCF Platform Target
0
 
pclarke7Author Commented:
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 ?
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pclarke7Author Commented:
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.
0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
can you create small sample project and send here ?
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
 
pclarke7Author Commented:
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
0
 
pclarke7Author Commented:
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;
        }
    }
}
0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
send me Projects File. so i can identify exact issue in your application.
0
 
pclarke7Author Commented:
0
 
pclarke7Author Commented:
0
 
pclarke7Author Commented:
both console & wcf solutions uploaded
0
 
käµfm³d 👽Commented:
I think Kalpesh meant to receive the whole solution, not just the solution files.
0
 
pclarke7Author Commented:
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
0
 
Kalpesh ChhatralaSoftware ConsultantCommented:
Please check your connection on below link

http://www.connectionstrings.com/excel-2007/

and may be you need not installed Office 2007 ODBC Driver properly.

please check also below sample project.

http://www.codeproject.com/Articles/21473/How-to-Read-Microsoft-Excel-Using-a-Connectio
0
 
pclarke7Author Commented:
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 9
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now