简单的COM +服务器抛出意外的异常

时间:2012-06-05 15:39:17

标签: c# .net com+

我写了一个简单的ServicedComponent

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.EnterpriseServices;

namespace ComPlusServer
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [Guid("9C674ECA-1B71-42EA-9DB2-9A0EA57EC121")]
    [Description("Hello Server")]
    public class HelloServer : ServicedComponent
    {
        [Description("Say Hello!")]
        public String SayHello()
        {
            return "Hello!, "; 
        }
    }
}

和Windows窗体应用程序

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComPlusServer;

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HelloServer server = new HelloServer();

            MessageBox.Show(server.SayHello(), "Message from HelloServer");
        }
    }
}

在组件服务MMC上,在应用程序属性,安全选项卡上,我将呼叫的身份验证级别降低到,将模拟级别降低到识别,并且取消选中对此强制执行访问检查申请授权。

我不断收到ServicedComponentException异常

  

基于方法级角色的安全性需要接口定义   类方法。

对此有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为这意味着需要在接口中定义Component类的方法。

[ComVisable(true)]
public interface IHelloServer
{
    public String SayHello(); 
}

现在让你的componet类实现接口:

[ComVisable(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComDefaultInterface(typeof(IHelloServer))]     
[Guid("9C674ECA-1B71-42EA-9DB2-9A0EA57EC121")]     
[Description("Hello Server")]     
public class HelloServer : ServicedComponent, IHelloServer     
{         
    [Description("Say Hello!")]         
    public String SayHello()         
    {             
        return "Hello!, ";          
    }     
}