1

I created a new View (LogView) in Infrastructure.Module project. This view will be used as LogViewer like output window in VS. i want to write different status messags in this LogView from different modules.

I also created a class LogWriter which is publishing an event to write message into LogView

i am facing problem to access this LogWriter class in my whole application.. please tell me how can i use this...

public class LogWriter 
    {
        [EventPublication(EventTopicNames.WriteToLog, PublicationScope.Global)]
        public event EventHandler<EventArgs<string>> WriteToLog;

        private void OnWriteToLog(EventArgs<string> eventArgs)
        {
            if (WriteToLog != null)
            {
                WriteToLog(null, eventArgs);
            }
        }

        public void WriteMsg(string msg)
        {
            OnWriteToLog(new EventArgs<string>(msg));
        }
    }

and in LogView event subscription is

 [EventSubscription(EventTopicNames.WriteToLog, ThreadOption.UserInterface)]
        public void OnWriteToLog(object sender, EventArgs<string> eventArgs)
        {
            this.txtLogs.AppendText(eventArgs.Data + Environment.NewLine);
        }

please suggest me a solution

LogWriter class is in Infrastructure.Interface project LogViewer is in Infrastructure.Module project

In ModuleController.cs of Infrastructure.Module i Added LogWriter in WorkItem.Services Collection

 WorkItem.Services.AddNew<LogWriter>();

and in one other project i am getting it using

var logWriter = WorkItem.Services.Get(); if (logWriter != null) logWriter.WriteMsg("message");

but it is returning me null.

module loading sequence is also correct.

Mohsan
  • 2,483
  • 6
  • 47
  • 62

1 Answers1

0

Add this attribute to your LogWriter class

[Service(typeof(LogWriter), AddOnDemand=true)]
public class LogWriter
{
    ...
}

then in your code simply access it by doing this:

var logWriter = WorkItem.Service.Get<LogWriter>();
if (logWriter != null)
    logWriter.WriteMsg("message");

This will require that the Module that the LogWriter is in is loaded before the one that tries to access it. I would recommend adding a module dependency if they are seperated. The exact way to do it depends on the IModuleLoader you are using.

Tom Anderson
  • 10,807
  • 3
  • 46
  • 63
  • with the event subscriptions, be sure that your View is either BuiltUp or added to one of the ObjectCollections of the WorkItem (SmartParts or Items). – Tom Anderson Mar 11 '10 at 08:17
  • the line var logWriter = WorkItem.Service.Get(); always returning null – Mohsan Mar 11 '10 at 08:51
  • is the LogWriter class in the same assembly as a ModuleInit class? – Tom Anderson Mar 11 '10 at 08:54
  • I have 17 modules.. LogWriter class is in Infrastructure.Interface and LogView is in Infrastructure.Module project. which always loads on application startup. and i am accessing LogWriter from other modules.. but it is returning null. – Mohsan Mar 11 '10 at 08:58
  • You may have to manually add the service. WorkItem.Services.AddNew(); One caveat to that is that you will have to add a module dependency to be sure that the service is added before you use it. – Tom Anderson Mar 11 '10 at 09:00
  • one thing i can do is to create a static object of LogWriter in each module using WorkItem.Items.AddNew() and access this static object in desired classes of that module. I do not think this will be good approach. – Mohsan Mar 11 '10 at 09:24