Simple WCF sample
class Program
{
static void Main(string[] args)
{
}
}
Replace the above code with following code...
(You also need to add reference the syste.Servicemodel .Net Assembly )
///////////
[ServiceContract(Namespace = "http://www.janaganamana.net/Astrology")]
public interface IAstrology
{
[OperationContract]
string GetHoroscope (int year );
}
class AstrologyService : IAstrology
{
string[] chances = new string[7] { "Good Day", "Bad day", "HopeGoodThings", "Live like a king", "Silence please", "Give Respect ", "Take resepect" };
public string GetHoroscope(int year)
{
int i = DateTime.Now.Millisecond %7 ;
return chances[i];
}
}
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/wcfSamples/Astrology");
ServiceHost selfHost = new ServiceHost(typeof(AstrologyService), baseAddress);
try
{
selfHost.AddServiceEndpoint(typeof(IAstrology),new WSHttpBinding(),"AstrologyService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is running.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
/////////////
Browse the URL http://localhost:8000/wcfSamples/Astrology
Out of three components
1) service
2) Host
3) Client -- (your Browser NOW)
First two components are in the console application
1) Service
It has two parts interface nd implementer
This is the contract/interface
[ServiceContract(Namespace = "http://www.janaganamana.net/Astrology")]
public interface IAstrology
{
[OperationContract]
string GetHoroscope (int year );
}
This interface is implemented by a small class
2) HOST Main()
The host started a hosting environment(that is waiting for user to press a key to close)
So the host opened a service.
3) Instead of a browser , try an other windows application that uses the webservice exposed(while our HOST is running........