2010-05-19 46 views
4

我的問題是非常相似,這兩個:將C#COM服務器事件德爾福的客戶端應用

C# component events?

C# - writing a COM server - events not firing on client

然而,是什麼讓他們的工作是不是爲我工作。類型庫文件沒有任何事件定義提示,所以Delphi沒有看到它。正如您所期望的,該類對其他C#應用程序正常工作。

COM服務器工具:

  • Visual Studio 2010中
  • .NET 4.0

Delphi應用程序:

  • 德爾福2010
  • 德爾福7

下面的代碼的簡化版本:

/// <summary> 
/// Call has arrived delegate. 
/// </summary> 
[ComVisible(false)] 
public delegate void CallArrived(object sender, string callData); 

/// <summary> 
/// Interface to expose SimpleAgent events to COM 
/// </summary> 
[ComVisible(true)] 
[GuidAttribute("1FFBFF09-3AF0-4F06-998D-7F4B6CB978DD")] 
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 
public interface IAgentEvents 
{ 
    ///<summary> 
    /// Handles incoming calls from the predictive manager. 
    ///</summary> 
    ///<param name="sender">The class that initiated this event</param> 
    ///<param name="callData">The data associated with the incoming call.</param> 
    [DispId(1)] 
    void OnCallArrived(object sender, string callData); 
} 

/// <summary> 
/// Represents the agent side of the system. This is usually related to UI interactions. 
/// </summary> 
[ComVisible(true)] 
[GuidAttribute("EF00685F-1C14-4D05-9EFA-538B3137D86C")] 
[ClassInterface(ClassInterfaceType.None)] 
[ComSourceInterfaces(typeof(IAgentEvents))] 
public class SimpleAgent 
{ 
    /// <summary> 
    /// Occurs when a call arrives. 
    /// </summary> 
    public event CallArrived OnCallArrived; 

    public SimpleAgent() {} 

    public string AgentName { get; set; } 

    public string CurrentPhoneNumber { get; set; } 

    public void FireOffCall() 
    { 
     if (OnCallArrived != null) 
     { 
      OnCallArrived(this, "555-123-4567"); 
     } 
    } 
} 

類型庫文件具有的屬性和方法的定義,但沒有事件是可見的。我甚至在Delphi的查看器中打開了類型庫以確保。 Delphi應用程序可以很好地查看和使用任何屬性,方法和函數。它只是沒有看到事件。

我會很感激任何指針或文章閱讀。

謝謝!

回答

5

經過多次試驗和錯誤,我終於得到了解決。有兩件事我需要在C#代碼中進行更改。

1)[ClassInterface(ClassInterfaceType.None)]需要被改變爲[ClassInterface(ClassInterfaceType.AutoDual)]

2)類,它是事件的源需要自MarshalByRefObject繼承。這有助於在源類中完成任何線程。

我只在德爾福方面需要一件事。我需要確保選中「生成組件包裝器」複選框。這是德爾福方面實際構建事件腳手架的原因。

這是你如何做到這一點在Delphi 7:

  1. 從菜單中選擇項目 - >導入類型庫
  2. 確保「生成部件的包裝材料」被選中
  3. 選擇COM類從列表中
  4. 點擊「添加設備」按鈕

新的單位將有你的COM事件的定義。

Step-By-Step blog post on how to do this

+0

該鏈接似乎損壞;文章仍然在線嗎?非常感謝! – 2010-10-08 13:12:34

+0

我的網站死了,備份沒有那個博客文章。我會看看我是否可以在這個週末重新創建這個帖子。 – 2010-10-08 23:12:39