0

班级:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession)]
public class MainService : IChat
{
    IChatCallback ChatCallback = OperationContext.Current.GetCallbackChannel<IChatCallback>();
    Chat chat = new Chat(this);
    public void ShowChat()
    {
        chat.Show();
    }
    public void SendInstantMessage(string user, string message)
    {
        chat.RaiseMsgEvents(user, message);
        ChatCallback.InstantMessage(user, message);
    }
 }

形式:

public partial class Chat : Form
{
    MainService service;

    public Chat(MainService service)
    {
        InitializeComponent();
        OnMsgReceivedEvent += new OnMsgReceived(callback_OnMsgReceivedEvent);
        this.service = service;
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        service.SendInstantMessage("Admin", txtMsg.Text);
    }
 }

mainForm 使用这样的类:

 public partial class Form1 : Form
 {
    ServiceHost host;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        host = new ServiceHost(typeof(WCF_Server.MainService));
        host.Open();
    }
 }

在主窗体中,我只是传递类,没有初始化,但是在类中ShowChat()调用时,我需要显示聊天表单并访问此类方法,以便我可以发送消息。

4

3 回答 3

1

.NET 是一种面向对象的语言。事实上,每个类都是一个对象。

您得到的错误是因为您在全局级别上用“this”实例化一个对象。

更新

根据您的更新,您可以执行以下操作,它将起作用。您可能需要对其进行更多重构,以确保它不会破坏任何业务规则等。

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession)]
public class MainService : IChat
{
    IChatCallback ChatCallback = OperationContext.Current.GetCallbackChannel<IChatCallback>();
    //Changed this to be just a declaration. This will be null,
    // as there is no object yet, this is really just a pointer to nothing.
    //This tells the system that you might/are planning to use an object called 
    //chat, but it doesn't exist yet.
    Chat chat;

    // Get your default constructor going. This will create the actual chat object, allowing the rest of your class to access it.
    public MainService()
    {
         //instantiate it! (or as some of our co-ops say "We're newing it")
         chat = new Chat(this);
    }

    //now that chat is actually instantiated/created you can use it.
    public void ShowChat()
    {
        chat.Show();
    }
    public void SendInstantMessage(string user, string message)
    {
        chat.RaiseMsgEvents(user, message);
        ChatCallback.InstantMessage(user, message);
    }
 }

这只是个人的烦恼,但是有一个与全局变量同名的函数参数是……对我来说,不,不。我在您的 Chat.Chat(MainService) 函数中注意到了这一点。

于 2012-05-09T16:05:57.243 回答
0

当然是的,只要创建一个以你的这个类为参数的方法并调用它......

于 2012-05-09T16:00:36.297 回答
0

正如其他帖子所建议的那样,您需要重新考虑如何chat在课堂上实例化您的字段example。我会考虑延迟加载属性,就像这样......

private ChatForm _Chat = null;
private ChatForm Chat
{
    get
    {
        if (this._Chat == null)
        {
            this._Chat = new ChatForm(this);
        }
        return this._Chat;
    }
    set { this._Chat = value; }
}

使用延迟加载将确保您能够this根据请求使用关键字。

于 2012-05-09T16:06:25.343 回答