C# 简单COM+;服务器引发意外异常

C# 简单COM+;服务器引发意外异常,c#,.net,com+,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(ClassInterfaceT

我编写了一个简单的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上,在应用程序属性上,安全选项卡I降低了调用None的身份验证级别,降低了调用identification的模拟级别,并取消选中该选项卡,在授权时对此应用程序强制执行访问检查

我一直收到一个ServicedComponentException异常,说

方法级基于角色的安全性要求为 类方法


对此有什么想法吗?

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

[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!, ";          
    }     
}

感谢用户957902,我也做了这个更改,但运气不好。您可能需要明确指定类接口。我将向代码中添加ComDefaultInterfaceAttribute。我还看到了一些参考资料,其中您可能需要将ClassInterface属性值更改为ClassInterfaceType.None。