Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

อัปเกรด SAP .NET Connector 3.0 ไม่ทำงานใน Visual Studio 2008/2010

ปัญหา: SAP .NET Connector 3.0 ไม่ทำงานใน VS 2008/2010

หลายองค์กรเจอปัญหาเมื่ออัปเกรดจาก SAP .NET Connector 2.0 เป็น 3.0 ใน Visual Studio 2008 และ 2010 เนื่องจากมีการเปลี่ยนแปลงใหญ่โครงสร้าง (Breaking Changes)

ความแตกต่างหลัก: Connector 2.0 vs 3.0

ด้าน Connector 2.0 Connector 3.0
Architecture Proxy-based (ต้อง generate proxies) Proxy-less / Dynamic (ไม่ต้อง generate proxies)
Visual Studio Dependency ผูกมัดกับ VS version เฉพาะ ✅ ไม่ผูกมัด VS version (Standalone)
Deployment Complex (ต้อง register GAC, proxy DLLs) ง่ายกว่า (xcopy deployable)
Programming Model Early binding (Strongly typed proxies) Late binding (Dynamic, RFC metadata-driven)
Memory/Performance สูงกว่า ✅ ลด Memory consumption
Security มาตรฐานเก่า ✅ Robust และ Secure มากขึ้น

ข้อดีของการอัปเกรดเป็น 3.0

  • ✅ ไม่ผูกมัด Visual Studio version - ทำงานได้กับ VS 2012, 2013, 2015, 2017, 2019, 2022
  • ✅ เสถียร มั่นคง ปลอดภัยกว่า (More stable, robust, secure)
  • ✅ Design ดีกว่า - แยก Connection Handling ออกจาก Business Logic (Separation of Concerns)
  • ✅ ลด Memory Consumption
  • ✅ Dynamic Programming - เรียก RFC/BAPI โดยไม่ต้อง generate proxies
  • ✅ ไม่ต้อง Regenerate proxies และ Recompile** ทุกครั้งที่ SAP เปลี่ยน Metadata

การแก้ไขปัญหาใน VS 2008/2010

สาเหตุหลัก

  1. Target Framework - Connector 3.0 ต้องการ .NET Framework 4.0+ (VS 2010 รองรับ .NET 4.0 แต่ default เป็น 3.5)
  2. Assembly References - ต้องอ้างอิง sapnco.dll และ sapnco_utils.dll ใหม่
  3. Architecture Mismatch - 32-bit vs 64-bit (Connector 3.0 มีทั้ง x86 และ x64)
  4. Missing RFC SDK - ต้องติดตั้ง SAP RFC SDK แยกต่างหาก

ขั้นตอนแก้ไข

1. ตรวจสอบ Target Framework → ตั้งเป็น .NET Framework 4.0 หรือสูงกว่า
   Project Properties → Application → Target Framework

2. เพิ่ม Reference ให้ถูกต้อง:
   - sapnco.dll (Managed assembly)
   - sapnco_utils.dll (Utilities)
   - ไม่ต้อง add proxy DLLs แล้ว!

3. ตรวจสอบ Platform Target:
   - ถ้า SAP เป็น 64-bit → ตั้ง Platform Target = x64
   - ถ้า SAP เป็น 32-bit → ตั้ง Platform Target = x86
   - อย่าใช้ "Any CPU" หากไม่แน่ใจ

4. Copy SAP RFC DLLs (librfc32.dll, icudtXX.dll, ฯลฯ) ไปใน:
   - Output folder (bin\Debug, bin\Release)
   - หรือ System32/SysWOW64

ตัวอย่างโค้ด Connector 3.0 (Proxy-less)

using SAP.Middleware.Connector;

public class SapConnector
{
    public void CallBapi()
    {
        // 1. สร้าง Destination Configuration
        var config = new RfcConfigParameters
        {
            { RfcConfigParameters.Name, "SAP_PROD" },
            { RfcConfigParameters.AppServerHost, "sap-server.company.com" },
            { RfcConfigParameters.SystemNumber, "00" },
            { RfcConfigParameters.Client, "100" },
            { RfcConfigParameters.User, "username" },
            { RfcConfigParameters.Password, "password" },
            { RfcConfigParameters.Language, "EN" },
            { RfcConfigParameters.PoolSize, "5" }
        };
        
        // 2. สร้าง Destination
        RfcDestination dest = RfcDestinationManager.GetDestination(config);
        
        // 3. สร้าง Repository (Dynamic metadata)
        RfcRepository repo = dest.Repository;
        
        // 4. สร้าง Function (BAPI/RFC) แบบ Dynamic
        IRfcFunction bapi = repo.CreateFunction("BAPI_USER_GET_DETAIL");
        
        // 5. Set Input Parameters
        bapi.SetValue("USERNAME", "TESTUSER");
        
        // 6. Invoke
        bapi.Invoke(dest);
        
        // 7. อ่าน Output
        string userType = bapi.GetString("USERTYPE");
        IRfcStructure address = bapi.GetStructure("ADDRESS");
        string fullName = address.GetString("FULLNAME");
        
        Console.WriteLine($"User: {fullName}, Type: {userType}");
    }
}

ลิงก์เอกสารอ้างอิง

สรุป

  • Connector 3.0 เปลี่ยน'architecture จาก Proxy-based เป็น Dynamic/Proxy-less
  • VS 2008/2010 ต้องตั้ง Target Framework .NET 4.0+ และแก้ Platform Target
  • ไม่ต้อง generate proxies ลดความซับซ้อนการ Deploy
  • แนะนำให้ยกระดับ Visual Studio ให้ทันสมัย เพื่อรองรับ .NET ใหม่ๆ