2017-04-18 46 views
1

我在同一個解決方案下有兩個Windows窗體應用程序。c#windows微軟pointofservice System.TypeInitializationException

A.EXE框架是針對x86的4.5.2。

B.EXE框架是針對x86的3.5。

點擊A.EXE上的按鈕,打開B.EXE。

在B.EXE中有打印到EPSON TMT81的功能。

在B.EXE形式負載我初始化打印機對象...

m_Printer = new ThermalPrinter(); 

ThermalPrinter類:

string strLogicalName = "PosPrintTMT81"; 

try 
{ 
    //Create PosExplorer 

    PosExplorer posExplorer = new PosExplorer(); 

    DeviceInfo deviceInfo = null; 

    try 
    { 
     deviceInfo = posExplorer.GetDevice(DeviceType.PosPrinter, strLogicalName); 
     m_Printer = (PosPrinter)posExplorer.CreateInstance(deviceInfo); 
    } 
    catch (Exception ex) 
    { 
     string strErrorMsg = ex.Message.ToString() 
      + "\n\nPlease rectify the error and try again."; 
     LogException(ex); 
     return strErrorMsg; 
    } 

PosExplorer posExplorer = new PosExplorer();返回一個錯誤的線...

System.TypeInitializationException 'Microsoft.PointOfService.Management.Expl的類型初始值設定項俄勒爾「拋出了一個例外。

回答

0

一個共同的解決這個問題是通過添加以下到您的app.config恢復到傳統的.NET代碼訪問安全(CAS)策略:

<configuration> 
    <runtime> 
     <NetFx40_LegacySecurityPolicy enabled="true"/> 
    </runtime> 
</configuration> 

here & here更多信息。

你可以嘗試添加這兩個應用程序的app.config,看看它是否可以解決問題。但是,這僅適用於上述運行在.NET 4 &中的應用程序。

如果更改此設置對您而言不是一個長期的解決方案,那麼您的B應用程序確實必須僅在.NET 3.5中運行。因此,您需要調查是什麼導致它在.NET 4中運行。

也許是由您從A打開應用程序B的特定方式引起的 - 您可以提供該代碼。

或者你有這樣的事情在你的乙的app.config下面?...

<startup> 
    <supportedRuntime version="v4.0"/> 

你也可以提供在app.config。