When trying to create an application that uses dynamic ports, instead of a hard-coded port value, I ran across this error:
System.InvalidOperationException: The contract type MLITS.Pulse.Pulse is not attributed with ServiceContractAttribute. In order to define a valid contract, the specified type (either contract interface or service class) must be attributed with ServiceContractAttribute.
My code that is having issues follow:
public static void DynamicAddress()
{
int sessionIdentification = 0;
int portNumber = 0;
int newPort = 0;
string uriString = string.Empty;
sessionIdentification = Process.GetCurrentProcess().SessionId;
portNumber = 14613;
newPort = portNumber + sessionIdentification;
uriString = "net.tcp://localhost:" + newPort + "/PulseService";
Uri uri = new Uri(uriString);
//ServiceHost objServiceHost = new ServiceHost(typeof(Pulse), uri);
ServiceHost objServiceHost = new ServiceHost(typeof(Pulse));
objServiceHost.Description.Endpoints.Clear();
objServiceHost.AddServiceEndpoint(typeof(Pulse), new NetTcpBinding(), uri);
}
The commented out section is what I had before; however, I realized I needed to clear the Endpoints set up in the config file; which is why the later code exists, and where I am having troubles. The reason for the config files is explained here, in another question that I had posted to help solve the problem I was experiencing before. Once I find a solution I will post my results on both question and close them.
Does anyone know what I am doing wrong, and know how to fix the error I am receiving?
It seems like your ServiceContract attribute isn't in your concrete class, try this.
ServiceHost objServiceHost = new ServiceHost(typeof(Pulse));
objServiceHost.Description.Endpoints.Clear();
objServiceHost.AddServiceEndpoint(typeof(IPulse), new NetTcpBinding(), uri);