Still struggling with the server part of my wcf application.
The problem is that the creating a "callback" object within the server class cased a "System.NullReferenceException was unhandled" error.
If I understand right, it happens when I create this server object - ServerClass myServer = new ServerClass();
So I guess I should somehow create a list for server objects and create & add this objects automatically whenever a client makes a connection. Please suggest, what would be the best way of doing that?
Here's my code so far:
namespace server2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
myServer.eventHappened += new EventHandler(eventFunction);
}
ServerClass myServer = new ServerClass();
// here I instance a new server object.
// yet I believe that I need to make a list, that would store this objects, so that multiple clients would be able to connect,
// and I would be able to pick, to whom I want to send a callback.
void eventFunction(object sender, EventArgs e)
{
label1.Text = myServer.clientName;
}
private ServiceHost duplex;
private void Form2_Load(object sender, EventArgs e) /// once the form loads, create and open a new ServiceEndpoint.
{
duplex = new ServiceHost(typeof(ServerClass));
duplex.AddServiceEndpoint(typeof(IfaceClient2Server), new NetTcpBinding(), "net.tcp://localhost:9080/service");
duplex.Open();
this.Text = "SERVER *on-line*";
}
}
class ServerClass : IfaceClient2Server
{
public event EventHandler eventHappened;
IfaceServer2Client callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();
// ERROR: System.NullReferenceException was unhandled
public string clientName = "noname";
public void StartConnection(string name)
{
clientName = name;
MessageBox.Show(clientName + " has connected!");
eventHappened(this, new EventArgs());
// ERROR: System.NullReferenceException was unhandled :(
callback.Message_Server2Client("Welcome, " + clientName);
}
public void Message_Cleint2Server(string msg)
{
}
public void Message2Client(string msg)
{
}
}
[ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)]
public interface IfaceClient2Server ///// what comes from the client to the server.
{
[OperationContract(IsOneWay = true)]
void StartConnection(string clientName);
[OperationContract(IsOneWay = true)]
void Message_Cleint2Server(string msg);
}
public interface IfaceServer2Client ///// what goes from the sertver, to the client.
{
[OperationContract(IsOneWay = true)]
void AcceptConnection();
[OperationContract(IsOneWay = true)]
void RejectConnection();
[OperationContract(IsOneWay = true)]
void Message_Server2Client(string msg);
}
}
Thanks!
This can not be done that way. First of all, the Callback Channel is only available within an operation, not when the instance of the service class is created.
Secondly, the instances of your ServerClass are created by the WCF service host depending on your WCF configuration. For example, there may be one instance per call (!). So creating an instance yourself and attaching an event handler does not affect the automatically created instances. That's why you get an exception in StartConnection.
What I'd do in that case is:
Create a singleton class that publishes the desired event
Attach a handler to the event from within your main code. This will be the code that listens to events
Create a public method (like ConnectionStarted) which raises the event
Call this method from your ServerClass
If you don't need to wait for the event handler to finish, you can also raise the event asynchronously in a separate thread. You then have to make sure that the event handler attached in step 2) handles thread contexts properly using for example this.Invoke (Forms) or this.Dispatcher.BeginInvoke (WPF).
Try putting that line in the class constructor:
class ServerClass : IfaceClient2Server
{
public event EventHandler eventHappened;
IfaceServer2Client callback;
public ServerClass ()
{
callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();
}
...
}
If still no luck it probably means you can use OperationContext.Current only inside some operation.
Related
I'm followed this example to create a WCF interface between two C# projects:
https://dopeydev.com/wcf-interprocess-communication/
as you can see in the "TestService.cs" code, there is an implementation of this function:
public void SendMessage(string message)
{
MessageBox.Show(message);
}
which actually shows the received message on the server. I know the name is quite confusing, but it's the same name the client would use.
My goal is to change this function in order to fire an event to be consumed in another class.
This is what I tried to do:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class IServer : Interfaces.IService
{
public void Connect()
{
Callback = OperationContext.Current.GetCallbackChannel<Interfaces.ICallbackService>();
}
public static Interfaces.ICallbackService Callback { get; set; }
public void SendMessage(string message)
{
MessageReceivedEventArgs args = new MessageReceivedEventArgs();
args.json = message;
OnMessageReceived(this, args);
}
public event EventHandler<MessageReceivedEventArgs> MessageReceived;
protected virtual void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
MessageReceived?.Invoke(this, e);
}
}
public class MessageReceivedEventArgs : EventArgs
{
public string json;
}
then in my other class:
class Comm
{
IServer m_server = new IServer();
public Engine()
{
var host = new ServiceHost(typeof(IServer), new Uri("net.pipe://localhost"));
host.AddServiceEndpoint(typeof(IService), new NetNamedPipeBinding(), "ipc");
host.Open();
m_server.MessageReceived += Server_MessageReceived;
}
private void Server_MessageReceived(object sender, MessageReceivedEventArgs e)
{
// this event handler is never executed!
}
}
As far as I understand, the problem is I create a new instance of the IServer class with the event handler added, but the ServiceHost which actually uses the IServer code is another instance.
Am I wrong?
I need to retrieve the instance used by ServiceHost to get the event working?
ServiceHost has an overload where you can provide your own instance for the service:
var host = new ServiceHost(m_server, new Uri("net.pipe://localhost"));
Also, you should bind the event before starting the service, otherwise you could miss requests:
m_server.MessageReceived += Server_MessageReceived;
host.Open();
Class:
[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);
}
}
Form:
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);
}
}
The mainForm use the class like this:
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();
}
}
In the main form, i just pass the class, no initializing, but in the class when ShowChat() called i need to show the chat form and get to this class method so i can send messages.
.NET is an object oriented language. In fact, every class is an object.
The error you are getting is because you're instantiating an object with "this" on the global level.
UPDATE
Based on your update you could do the following and it will work. You might want to refactor this some more to ensure that it's not going to break any business rules etc.
[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);
}
}
This is just a personal pet peeve, but having a function parameter the same name as a global variable is... well for me a no no. I noticed this on your Chat.Chat(MainService) function.
Of course it is, just create a method that takes this class of yours as a parameter and call it...
As other posts have suggested, you'll want to re-consider how you instantiate your chat field within your example class. I would consider lazy loading the property, like so...
private ChatForm _Chat = null;
private ChatForm Chat
{
get
{
if (this._Chat == null)
{
this._Chat = new ChatForm(this);
}
return this._Chat;
}
set { this._Chat = value; }
}
Using lazy-loading will ensure you're able to use the keyword this upon request.
I am trying to modify a TextBox that belongs to Form2 from within a WCF object.
namespace server2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private ServiceHost duplex;
private void Form2_Load(object sender, EventArgs e) /// once the form loads, create and open a new ServiceEndpoint.
{
duplex = new ServiceHost(typeof(ServerClass));
duplex.AddServiceEndpoint(typeof(IfaceClient2Server), new NetTcpBinding(), "net.tcp://localhost:9080/service");
duplex.Open();
this.Text = "SERVER *on-line*";
}
}
class ServerClass : IfaceClient2Server
{
IfaceServer2Client callback;
public ServerClass()
{
callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();
}
public void StartConnection(string name)
{
var myForm = Form.ActiveForm as Form2;
myForm.textBox1.Text = "Hello world!"; /// <- this one trows “System.NullReferenceException was unhandled”
/// unless Form2 is selected when this fires.
callback.Message_Server2Client("Welcome, " + name );
}
public void Message_Cleint2Server(string msg)
{
}
public void Message2Client(string msg)
{
}
}
[ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)]
public interface IfaceClient2Server ///// what comes from the client to the server.
{
[OperationContract(IsOneWay = true)]
void StartConnection(string clientName);
[OperationContract(IsOneWay = true)]
void Message_Cleint2Server(string msg);
}
public interface IfaceServer2Client ///// what goes from the sertver, to the client.
{
[OperationContract(IsOneWay = true)]
void AcceptConnection();
[OperationContract(IsOneWay = true)]
void RejectConnection();
[OperationContract(IsOneWay = true)]
void Message_Server2Client(string msg);
}
}
Yet the "myForm.textBox1.Text = "Hello world!";" line throws System.NullReferenceException was unhandled"...
Any ideas, thanks!
myForm might not be of the Form2 type, or it might not contain a textBox1 field. Make sure to check for null for both those cases.
As discussed in the comments of the initial question, the problem is you're referring to ActiveForm, when the form you want is not active. Whenever attempting to cast using the as keyword, the result will be null if the cast is invalid. Since you grabbed a form that could not be cast to Form2 (because it was a different kind of form), you correctly received a null reference exception.
Assuming you have enforced singleton rules on Form2 and you haven't played with the form's name, you can access it by way of the Application.OpenForms collection like so:
(Form2)Application.OpenForms["Form2"];
In your code sample that could look like this:
public void StartConnection(string name)
{
//var myForm = Form.ActiveForm as Form2;
var myForm = Application.OpenForms["Form2"] as Form2;
myForm.textBox1.Text = "Hello world!"; /// <- this one trows “System.NullReferenceException was unhandled”
/// unless Form2 is selected when this fires.
callback.Message_Server2Client("Welcome, " + name );
}
That said, I don't think I'd give responsibility of modifying form controls to a WCF service. I'd much sooner consume events fired by the service inside my form.
I'm trying to name a server-client messaging application using wcf.
Here's the server part so far.
namespace server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) /// once the form loads, create and open a new ServiceEndpoint.
{
ServiceHost duplex = new ServiceHost(typeof(ServerWCallbackImpl));
duplex.AddServiceEndpoint(typeof(IServerWithCallback), new NetTcpBinding(), "net.tcp://localhost:9080/DataService");
duplex.Open();
this.Text = "SERVER *on-line*";
}
private void buttonSendMsg_Click(object sender, EventArgs e)
{
Message2Client(textBox2.Text); /// The name 'Message_Server2Client' does not exist in the current context :(
}
class ServerWCallbackImpl : IServerWithCallback /// NEED TO SOMEHOW MERGE THIS ONE WITH THE FORM1 CLASS
{
IDataOutputCallback callback = OperationContext.Current.GetCallbackChannel<IDataOutputCallback>();
public void StartConnection(string name)
{
/// client has connected
}
public void Message_Cleint2Server(string msg)
{
TextBox1.text += msg; /// 'TextBox1' does not exist in the current context :(
}
public void Message2Client(string msg)
{
callback.Message_Server2Client(msg);
}
}
[ServiceContract(Namespace = "rf.services",
CallbackContract = typeof(IDataOutputCallback),
SessionMode = SessionMode.Required)]
public interface IServerWithCallback ///// what comes from the client to the server.
{
[OperationContract(IsOneWay = true)]
void StartConnection(string clientName);
[OperationContract(IsOneWay = true)]
void Message_Cleint2Server(string msg);
}
public interface IDataOutputCallback ///// what goes from the sertver, to the client.
{
[OperationContract(IsOneWay = true)]
void AcceptConnection();
[OperationContract(IsOneWay = true)]
void Message_Server2Client(string msg);
}
}
}
I just can't figure out, how do I merge "class Form1:Form" and "class ServerWCallbackImpl : IServerWithCallback", so that I would be able to induce the Message2Client function from a buttonclick, as well as add TextBox1.text += msg when the *Message_Cleint2Server* happens.
Thanks!
You don't need to merge your class with the form: you need to create an instance of it. Something like this:
IServerWCallback server = new ServerWCallbackImpl();
server.Message2Client("hello world");
However (from the structure of your code so far), you'll probably need to have created an instance of the class earlier. This allows you to connect that instance and keep it around for later operation.
You may also want to read the MSDN pages on classes and objects (instances of classes) to make sure you've understood the concepts fully before you continue - this stuff is pretty fundamental to .NET programming.
What is the Need for merging why can't You Use Inheritance?
Solving Multiple Inheritance in C#.net
Click this for more details (stackoverflow)
i think that will give you the answer
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);
}
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?