i am currently developing a wcf publish subscribe service. On my windows form app, I have a Connect button that attempts to subscribe to the service. the code is as follows
WSDualHttpBinding binding = (WSDualHttpBinding)client.Endpoint.Binding;
string clientcallbackaddress = binding.ClientBaseAddress.AbsoluteUri;
clientcallbackaddress += Guid.NewGuid().ToString();
binding.ClientBaseAddress = new Uri(clientcallbackaddress);
InstanceContext site = new InstanceContext(null, new mainForm(backgroundFormObject));
PostingContractClient client = new PostingContractClient(site);
client.Subscribe();
MessageBox.Show("Service has been connected!", "Connected");
as for the exception handling, this is what i have done,
try
{
//refer to codes above
}
catch (EndpointNotFoundException)
{
MessageBox.Show("WCF Service has not been started.", "Error");
}
catch (CommunicationException)
{
MessageBox.Show("Error");
}
if my service is not activated, upon clicking the Connect button, it will load for awhile before sending the first error, “WCF Service has not been started.” , after which I activate the service, clicking the Connect button shows me the second error, which is CommunicationException despite my service being on.
Any idea how to fix this?
The error message for the error
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
Not sure if this is what you want, but from what I can understand from your question this seems like what you need:
Since you need the variable to be available throughout the whole project, you can declare the InstanceContext and the Client at the start of the the class.
follow by in the form_Load method,
and finally in your “Connect” button,