2012-12-03 26 views
7

我已經創建了一個Windows應用程序。當我手動執行我的可執行文件時它工作正常,但是當我使用Windows服務運行我的exe時,它顯示提供失敗錯誤。我正在使用實體框架。實體框架有任何問題嗎?Exe文件沒有從我的Windows服務與數據庫連接運行?

這裏是我的代碼:

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; 
    } 

我檢查我的web服務和打印異常消息給我的記事本,發現了這個錯誤:

The underlying provider failed on Open.

但運行時,我只得到這個錯誤作爲Windows服務。如果我手動運行我的exe,它工作正常。有沒有必要在Windows服務中添加引用?

+2

嘗試看到窗口logs.windows服務在這裏寫了例外 – Frank59

+0

是的,我發現我的Windows日誌中的錯誤描述,但我怎麼能解決這個問題。 –

+0

你可以在你的文章中添加錯誤描述嗎? – Frank59

回答

1

我也通過windows服務啓動我的應用程序。看看我的代碼可以幫助你

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; 
    } 

} 

將下面的代碼在你的方法

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 
    } 

這將創建在當前用戶帳戶的過程。此代碼已啓動並正在運行文件。

我希望這將幫助!歡呼!

+0

無法獲取windowsapi的命名空間? –

+0

你現在可以得到!我編輯了答案:-) –

+0

我檢查我的Web服務並將異常消息打印到我的記事本中,發現此錯誤:「底層提供程序在打開時失敗。」但只有當我使用窗口服務時纔會出現此錯誤。如果我手動運行我的exe文件,其工作正常 –

相關問題