1

Environment: .net 4.6.1 on Windows 10 x64

Problem: 1. WCF Contract implemented with an event. 2. Service host will fire the event every time an event log entry is logged and called by the client. 3. The host application triggers properly however the object (EventLogEntry) does not get posted properly and is always null. 4. I have checked during debug the client side Winforms application passes a instantiated object. This somehow doesn't make it to the server side. 5. at this Stage both the client and the server are on the same machine.

Question: Why is object null and how can I fix this?

WCF Service Code:

[ServiceContract]
public interface IloggerWCFServiceContract
{

    [OperationContract(IsOneWay = true)]
    void WriteEvent(EntryWrittenEventArgs e);

    [OperationContract(IsOneWay = true)]
    [ServiceKnownType(typeof(EventLogEntry))]
    void OnEventWritten(EntryWrittenEventArgs e);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class wcf_logger_servicebase : IloggerWCFServiceContract
{
    public event EventHandler<EntryWrittenEventArgs> eventwritten;
    public void OnEventWritten(EntryWrittenEventArgs e)
    {
        if (eventwritten != null)
            this.eventwritten(this, e);
    }

    public void WriteEvent(EntryWrittenEventArgs e)
    {
        OnEventWritten(new EntryWrittenEventArgs(e.Entry));

    }

}

Wcf Winforms Server code:

wcf_logger_servicebase loggerWCFService = new wcf_logger_servicebase();
ServiceHost loggerServiceHost = new ServiceHost(loggerWCFService, new Uri(loggerGlobalStatics.namedPipeServerAddress));

loggerWCFService.eventwritten += new EventHandler<EntryWrittenEventArgs>(eventhandler_entryWritten);
.....    
                loggerServiceHost.AddServiceEndpoint(typeof(IloggerWCFServiceContract), new NetNamedPipeBinding(), loggerGlobalStatics.namedPipeServerAddress);
                loggerServiceHost.Open();
......
private void eventhandler_entryWritten(object sender, EntryWrittenEventArgs e)
{
--->textBox1.Text += e.Entry.ToString() + "\r\n" ;
}

WCF Winforms Client code:

ChannelFactory<IloggerWCFServiceChannel> loggerChannelFactory;
IloggerWCFServiceChannel loggerServiceChannel = null;
wcf_logger_servicebase loggerWCFService = new wcf_logger_servicebase();
EndpointAddress serverEndpoint;
System.ServiceModel.Channels.Binding binding;

public client()
{
InitializeComponent();
serverEndpoint = new EndpointAddress(new Uri(loggerGlobalStatics.namedPipeServerAddress));
                binding = new NetNamedPipeBinding();
                loggerChannelFactory = new ChannelFactory<IloggerWCFServiceChannel>(binding, serverEndpoint);
                loggerChannelFactory.Open();
                loggerServiceChannel = loggerChannelFactory.CreateChannel();
                loggerServiceChannel.Open();

    ....

private void eventLog1_EntryWritten(object sender, System.Diagnostics.EntryWrittenEventArgs e)
            {
                loggerServiceChannel.WriteEvent(e);
            }

1 Answers1

0

The reason is that EntryWrittenEventArgs has no setter for Entry.

See this SO question for more details.

Why not just send Entry directly without encapsulating it in EntryWrittenEventArgs?

Community
  • 1
  • 1
Grzegorz W
  • 3,487
  • 1
  • 21
  • 21
  • Thank you very much, this was the deal. Instead of passing in the EntryWrittenEventArgs, I changed this to Entry only and passed that along and voila :) Thank you! – Oğuzhan Filizlibay May 07 '16 at 23:09