7

I have created a Windows application. When I execute my executable manually it is working fine, but when I run my exe using a Windows service it showing provide fail error. I am using Entity Framework. Is there any problem with Entity Framework?

Here is my code:

   private void Threadfun()
    {
        try
        {               
            System.Diagnostics.Process.Start(@"D:\V-Tec\bin\Debug\VibrantIndexerForm.exe");
            if (System.IO.File.Exists(@"D:\VibrantIndexerSetup\MarcExport1.txt"))
            {
            }
            else
            {
                System.IO.File.Create(@"D:\VibrantIndexerSetup\MarcExport1.txt").Dispose();
            }

            System.IO.File.WriteAllText(@"D:\VibrantIndexerSetup\MarcExport1.txt", System.DateTime.Now.ToString());
            System.Threading.Thread.Sleep(100);
        }
        catch (Exception ex)
        {
        }     
    } 

 private void time_Elapsed(object sender, ElapsedEventArgs e)
    {

        m_thread = new System.Threading.Thread(new System.Threading.ThreadStart(Threadfun));
        if (m_thread.IsAlive)
        {
        }
        else
        {
            m_thread.Start();
        }
    }

    protected override void OnStart(string[] args)
    {
        if (time.Enabled == false)
        {    
            time.Elapsed += new ElapsedEventHandler(time_Elapsed);
            time.Interval = 2000;
            time.Enabled = true;
        }
    }

    protected override void OnStop()
    {
        time.Enabled = false;  
    }

I checked my web service and printed the exception message to my notepad, and found this error:

The underlying provider failed on Open.

But I only get this error when running as a Windows service. If I run my exe manually it works fine. Is there any need to add references in Windows services?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Kaps Hasija
  • 235
  • 1
  • 2
  • 8

1 Answers1

1

I also start the my application via windows service. See if my code can help you

 public class WindowsApi
{

    [DllImport("Wtsapi32.dll", EntryPoint = "WTSQueryUserToken", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WTSQueryUserToken(uint SessionId, ref IntPtr phToken);

    [DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUserW", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool CreateProcessAsUser([InAttribute()]IntPtr hToken, InAttribute(), MarshalAs(UnmanagedType.LPWStr)]string lpApplicationName, [InAttribute(), MarshalAs(UnmanagedType.LPWStr)] string lpCommandLine, [InAttribute()] IntPtr pProcessAttributes, [InAttribute()] IntPtr lpThreadAttributes, MarshalAs(UnmanagedType.Bool)] bool bInheritHandles, uint dwCreationFlags, [InAttribute()] IntPtr lpEnvironment, [InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)] string pCurrentDirectory, ref STARTUPINFOW lpStartupInfo, ref PROCESS_INFORMATION lpProcessInformation);

    [StructLayout(LayoutKind.Sequential)]
    public struct SECURITY_ATTRIBUTES
    {
        public uint nLength;
        public IntPtr lpSecurityDescriptor;
        [MarshalAs(UnmanagedType.Bool)]
        public bool bInheritHandle;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct STARTUPINFOW
    {
        public uint cb;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpReserved;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpDesktop;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpTitle;
        public uint dwX;
        public uint dwY;
        public uint dwXSize;
        public uint dwYSize;
        public uint dwXCountChars;
        public uint dwYCountChars;
        public uint dwFillAttribute;
        public uint dwFlags;
        public ushort wShowWindow;
        public ushort cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public uint dwProcessId;
        public uint dwThreadId;
    }

} 

Place the following code in your method

try
     {
         IntPtr UserTokenHandle = IntPtr.Zero;
         WindowsApi.WTSQueryUserToken(WindowsApi.WTSGetActiveConsoleSessionId(), ref UserTokenHandle);
         WindowsApi.PROCESS_INFORMATION ProcInfo = new WindowsApi.PROCESS_INFORMATION();
         WindowsApi.STARTUPINFOW StartInfo = new WindowsApi.STARTUPINFOW();
         StartInfo.cb = Convert.ToUInt32(System.Runtime.InteropServices.Marshal.SizeOf(StartInfo));
         string arguments = " nonGUI";
         WindowsApi.CreateProcessAsUser(UserTokenHandle, pathToExe + "\\YourAppName.exe", arguments, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref StartInfo, ref ProcInfo);

     catch (Exception ex)
     {
         //Catch excpetion
     } 

This will create a process in current user account. This code is up and running file.

I wish it will help !! cheers !

jparthj
  • 1,606
  • 3
  • 20
  • 44
  • i check my web service and print exception message to my notepad, found this error: "The underlying provider failed on Open." but this error coming only when i use window service. if i run my exe manually its working fine – Kaps Hasija Dec 03 '12 at 13:46
  • my above comment may help you because now i found my exe is running but getting this exception,"The underlying provider failed on Open." – Kaps Hasija Dec 03 '12 at 13:47
  • Please try the solution.. http://stackoverflow.com/questions/2475008/the-underlying-provider-failed-on-open – jparthj Dec 03 '12 at 13:49
  • i tried above solution but still same error:The underlying provider failed on Open. – Kaps Hasija Dec 03 '12 at 13:56
  • Its some kinda connection error. Please try this http://blogs.msdn.com/b/dataaccesstechnologies/archive/2012/08/09/error-quot-the-underlying-provider-failed-on-open-quot-in-entity-framework-application.aspx – jparthj Dec 03 '12 at 13:58
  • yes, but it comes when i execute my exe with window service only, if i execute my exe alone it works perfect and my connection string in exe itself, so it should work with service. – Kaps Hasija Dec 03 '12 at 14:01
  • 1
    http://kromey.us/2011/06/microsoft-sql-server-2008-times-out-on-first-connection-attempt-447.html – jparthj Dec 03 '12 at 14:05
  • what i need to do now? problem still there – Kaps Hasija Dec 04 '12 at 04:25