2012-12-03 2 views
7

Windows 응용 프로그램을 만들었습니다. 내 실행 파일을 수동으로 실행하면 정상적으로 작동하지만 Windows 서비스를 사용하여 exe를 실행하면 오류가 발생합니다. Entity Framework를 사용하고 있습니다. Entity Framework에 문제가 있습니까? 여기 Exe 파일이 데이터베이스 연결을 사용하는 Windows 서비스에서 실행되고 있지 않습니까?

내 코드입니다 :

The underlying provider failed on Open.

그러나 실행할 때 나는 단지이 오류를 얻을 : 내 웹 서비스를 확인하고 내 메모장에 예외 메시지를 인쇄하고,이 오류를 발견

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

Windows 서비스로. exe를 수동으로 실행하면 정상적으로 작동합니다. Windows 서비스에 참조를 추가 할 필요가 있습니까?

+2

windows logs.windows 서비스가 여기에 예외를 작성하려고 시도했습니다. – Frank59

+0

예 Windows 로그에 오류 설명이 있지만이 문제를 어떻게 해결할 수 있습니까? –

+0

게시물에 오류 설명을 추가 할 수 있습니까? – Frank59

답변

1

또한 Windows 서비스를 통해 내 응용 프로그램을 시작합니다. 내 코드는 현재 사용자 계정에서 프로세스를 만들 것이다 방법

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 
    } 

이 당신에게

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

} 

장소에 다음 코드를 도울 수 있다면 참조하십시오. 이 코드는 실행중인 파일입니다.

나는 그것이 도움이되기를 바랍니다 !! 건배!

+0

windowsapi의 네임 스페이스를 가져올 수 없습니까? –

+0

지금 받으실 수 있습니다! 대답을 편집했습니다 :-) –

+0

메모장에 웹 서비스 및 인쇄 예외 메시지를 확인한 결과 "기본 공급자가 열리지 않았습니다."라는 오류가 발견되었습니다. 하지만이 오류는 윈도우 서비스를 사용할 때만 나타납니다. 내 exe를 수동으로 실행하는 경우 해당 작업을 잘 수행 –

관련 문제