I am currently developing a WCF Publish Subscribe service. My Service has the following code,
public void PublishPost(string postSampleData)
{
PostChangeEventArgs e = new PostChangeEventArgs();
e.PostData = postSampleData;
PostChangeEvent(this, e);
}
and the code for the postChangeEvent is
public class PostChangeEventArgs : EventArgs
{
public string PostData;
}
and in my client file, i wrote this code in the main method,
class Program : IPostingContractCallback
{
static void Main()
{
InstanceContext site = new InstanceContext(null, new Program());
PostingContractClient client = new PostingContractClient(site);
WSDualHttpBinding binding = (WSDualHttpBinding)client.Endpoint.Binding;
String clientcallbackaddress = binding.ClientBaseAddress.AbsoluteUri;
clientcallbackaddress += Guid.NewGuid().ToString();
binding.ClientBaseAddress = new Uri(clientcallbackaddress);
client.Subscribe();
}
public void PostReceived(string postSampleData)
{
MessageBox.Show("PostChange(item {0})", postSampleData);
}
}
and for the code for my data source…
class Program : IPostingContractCallback
{
static void Main(string[] args)
{
InstanceContext site = new InstanceContext(new Program());
PostingContractClient client = new PostingContractClient(site);
client.PublishPost("testing");
Console.WriteLine();
Console.WriteLine("Press ENTER to shut down data source");
Console.ReadLine();
//Closing the client gracefully closes the connection and cleans up resources
client.Close();
}
public void PostReceived(string postSampleData)
{
Console.WriteLine("PostChange(item {0})",postSampleData);
}
}
After running the service, followed by the client, followed by the datasource, I’m suppose to receive a popup messagebox from my client. However there gives an error on the line
PostChangeEvent(this, e);
Object reference not set to an instance of an object.
Anyone know how to solve this?
It sounds like there’s nothing subscribed to the event. To check for this, you should use:
That will stop the
NullReferenceException, but of course it won’t address why there were no subscribers… you haven’t shown anything which subscribes to the event – what were you expecting to be subscribed?