Solved

VS2013 WCF C# application 32/64-bit issues

Posted on 2014-10-07
16
890 Views
Last Modified: 2014-10-15
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
Comment
Question by:pclarke7
  • 9
  • 4
  • 2
  • +1
16 Comments
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 40368060
you need to set Platform Target = x86.

Sample screenshot attached.

WCF Platform Target
0
 

Author Comment

by:pclarke7
ID: 40368577
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
 
LVL 40
ID: 40368644
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
 

Author Comment

by:pclarke7
ID: 40369322
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
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 40369337
can you create small sample project and send here ?
0
 
LVL 40
ID: 40369431
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
 

Author Comment

by:pclarke7
ID: 40370281
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
 

Author Comment

by:pclarke7
ID: 40371506
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 40371543
send me Projects File. so i can identify exact issue in your application.
0
 

Author Comment

by:pclarke7
ID: 40371661
0
 

Author Comment

by:pclarke7
ID: 40371664
0
 

Author Comment

by:pclarke7
ID: 40371668
both console & wcf solutions uploaded
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40371968
I think Kalpesh meant to receive the whole solution, not just the solution files.
0
 

Author Comment

by:pclarke7
ID: 40372358
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
 
LVL 16

Accepted Solution

by:
Kalpesh Chhatrala earned 500 total points
ID: 40372399
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
 

Author Closing Comment

by:pclarke7
ID: 40382763
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now