The full error message:
Could not find default endpoint element that references contract
'SampleProject.ITextMessage' in the ServiceModel client configuration
section. This might be because no configuration file was found for
your application, or because no endpoint element matching this
contract could be found in the client element.
This is a console app. Can't get much simpler from this.
No app config desired in this case, just looking to get a simple sample going. I pretty much lifted the code from here: http://msdn.microsoft.com/en-us/library/ms731758.aspx
Any help would be appreciated.
public class SampleProject
{
static void Main(string[] args)
{
var baseAddr = new Uri("http://localhost:6000/TextMessageSvc.svc");
using (var localHost = new ServiceHost(typeof(TextMessageClient), baseAddr))
{
try
{
//THIS page says an endpoint is not needed, a default will be created automatically:
//http://msdn.microsoft.com/en-us/library/ms731758.aspx
localHost.AddServiceEndpoint(typeof(ITextMessage),
new WSHttpBinding(),
"TextMessageSvc");
var behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
localHost.Description.Behaviors.Add(behavior);
localHost.Open();
Console.WriteLine("Service initialized.");
//************** Blows up on this line ***********************
var x = new TextMessageClient();
x.SendTextMessage();
Console.WriteLine("Press the ENTER key to terminate service.");
Console.ReadLine();
localHost.Close();
}
catch (CommunicationException ex)
{
Console.WriteLine("Oops! Exception: {0}", ex.Message);
localHost.Abort();
}
}
}
}
public class TextMessageClient : ClientBase<ITextMessage>, ITextMessage
{
public void SendTextMessage()
{
base.Channel.SendTextMessage();
}
}
[ServiceBehavior]
public class TextMessageSvc : ITextMessage
{
public TextMessageSvc()
{
}
[OperationBehavior]
public void SendTextMessage()
{
}
}
[ServiceContract]
public interface ITextMessage
{
[OperationContract]
void SendTextMessage();
}
Best Answer
So it turns out that the issue in this example is (since I'm not using a web config), that TextMessageClient does not have an endpoint. (localhost has one, but not TextMessageClient) When I call SendTextMessage, how is it going to communicate? Who is it going to talk to??
Well heck, I could create an endpoint and pass it into TextMessageClient, or just create one in its constructor, which I am now doing.
So after a renaming of TextMessageSvc to TextMessageProxy and then a refactor. This is all I now need (If you notice the new code is only consuming a service now, rather than attempting to host one as well:
I now have a function somewhere that sets up an endpoint for TextMessageClient to use.
The TextMessage class now provides its own endpoint: