Recently, I have learned something about WCF techniques, and of course, I found it was excellent that I decided to learn deeper about it.
I hope someone interest it can communicate with me, or just post your idea or advices freely. The below is a sample of my testing of WCF for beginner( before go on, your need to learning the ABC concepts of WCF, the ABC is Address, Binding, Contract. The three factors needed by any Endpoint).
this sample is written without using config file, in order to simplify the demo and more easy to understand.
Contract -- Host -- Cilent
the contract is the basic thing to communicate within Host and Cilent.
and then, you can construct your host now.
Please note that I'm using Tcp binding. The next is the Client.
the Client uses the ChannelFactory to create an Endpoint and connect to Host. The metadate is ISay an interface defined by me.
and the next chapter, I will use config file to config WCF instead of custom programming.
the Contract is the same, and the Host has a little changes.
Please notice that hte host is using type of Say. You need add a new App.config file like,
In this config file, notice the service's name is [Namesapce].[ClassName] format, and it has a behavior which will enabled the httpGet for the client to get metadata, the "mex" is the Endpoint specified for geting metadata.
the Client is more easy to write, but need to add web reference using the URI mentioned above http://localhost:8000/.
I hope someone interest it can communicate with me, or just post your idea or advices freely. The below is a sample of my testing of WCF for beginner( before go on, your need to learning the ABC concepts of WCF, the ABC is Address, Binding, Contract. The three factors needed by any Endpoint).
this sample is written without using config file, in order to simplify the demo and more easy to understand.
Contract -- Host -- Cilent
the contract is the basic thing to communicate within Host and Cilent.
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace SayHello
{
[ServiceContract]
public interface ISay
{
[OperationContract]
string SayHello( string yourName);
}
public class Say:ISay
{
#region ISay Members
public string SayHello( string yourName)
{
return string .Format( " Hello, {0}. Now is {1}. " , yourName, DateTime.Now);
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace SayHello
{
[ServiceContract]
public interface ISay
{
[OperationContract]
string SayHello( string yourName);
}
public class Say:ISay
{
#region ISay Members
public string SayHello( string yourName)
{
return string .Format( " Hello, {0}. Now is {1}. " , yourName, DateTime.Now);
}
#endregion
}
}
and then, you can construct your host now.
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SayHello;
namespace HostConsole
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " Welcome to WCF " );
using (ServiceHost host = new ServiceHost( typeof (Say), new Uri( " net.tcp://localhost:8000/ " )))
{
host.AddServiceEndpoint( typeof (ISay), new NetTcpBinding(), " SayHello " );
host.Open();
Console.ReadLine();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SayHello;
namespace HostConsole
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " Welcome to WCF " );
using (ServiceHost host = new ServiceHost( typeof (Say), new Uri( " net.tcp://localhost:8000/ " )))
{
host.AddServiceEndpoint( typeof (ISay), new NetTcpBinding(), " SayHello " );
host.Open();
Console.ReadLine();
}
}
}
}
Please note that I'm using Tcp binding. The next is the Client.
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SayHello;
namespace ClientConsole
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " Please input your name
"
);
string yourName = Console.ReadLine();
ChannelFactory < ISay > cf = new ChannelFactory < ISay > ( new NetTcpBinding(), new EndpointAddress( " net.tcp://localhost:8000/SayHello " ));
ISay say = cf.CreateChannel();
Console.WriteLine(say.SayHello(yourName));
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SayHello;
namespace ClientConsole
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " Please input your name

string yourName = Console.ReadLine();
ChannelFactory < ISay > cf = new ChannelFactory < ISay > ( new NetTcpBinding(), new EndpointAddress( " net.tcp://localhost:8000/SayHello " ));
ISay say = cf.CreateChannel();
Console.WriteLine(say.SayHello(yourName));
Console.ReadLine();
}
}
}
the Client uses the ChannelFactory to create an Endpoint and connect to Host. The metadate is ISay an interface defined by me.
and the next chapter, I will use config file to config WCF instead of custom programming.
the Contract is the same, and the Host has a little changes.
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SayHello;
namespace HostConsole
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " Welcome to WCF " );
using (ServiceHost host = new ServiceHost( typeof (Say)))
{
host.Open();
Console.ReadLine();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SayHello;
namespace HostConsole
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " Welcome to WCF " );
using (ServiceHost host = new ServiceHost( typeof (Say)))
{
host.Open();
Console.ReadLine();
}
}
}
}
Please notice that hte host is using type of Say. You need add a new App.config file like,
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< system.serviceModel >
< services >
< service name ="SayHello.Say" behaviorConfiguration ="MEXGET" >
< host >
< baseAddresses >
< add baseAddress ="http://localhost:8000/" />
</ baseAddresses >
</ host >
< endpoint address ="SayHello" binding ="basicHttpBinding" contract ="SayHello.ISay" ></ endpoint >
< endpoint address ="mex" binding ="mexHttpBinding" contract ="IMetadataExchange" ></ endpoint >
</ service >
</ services >
< behaviors >
< serviceBehaviors >
< behavior name ="MEXGET" >
< serviceMetadata httpGetEnabled ="true" />
</ behavior >
</ serviceBehaviors >
</ behaviors >
</ system.serviceModel >
</ configuration >
< configuration >
< system.serviceModel >
< services >
< service name ="SayHello.Say" behaviorConfiguration ="MEXGET" >
< host >
< baseAddresses >
< add baseAddress ="http://localhost:8000/" />
</ baseAddresses >
</ host >
< endpoint address ="SayHello" binding ="basicHttpBinding" contract ="SayHello.ISay" ></ endpoint >
< endpoint address ="mex" binding ="mexHttpBinding" contract ="IMetadataExchange" ></ endpoint >
</ service >
</ services >
< behaviors >
< serviceBehaviors >
< behavior name ="MEXGET" >
< serviceMetadata httpGetEnabled ="true" />
</ behavior >
</ serviceBehaviors >
</ behaviors >
</ system.serviceModel >
</ configuration >
In this config file, notice the service's name is [Namesapce].[ClassName] format, and it has a behavior which will enabled the httpGet for the client to get metadata, the "mex" is the Endpoint specified for geting metadata.
the Client is more easy to write, but need to add web reference using the URI mentioned above http://localhost:8000/.
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SayHello;
namespace ClientConsole
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " Please input your name
"
);
string yourName = Console.ReadLine();
// ChannelFactory<ISay> cf = new ChannelFactory<ISay>(new BasicHttpBinding(), new EndpointAddress(" http://localhost :8000/SayHello1"));
// ISay say = cf.CreateChannel();
SaySR.SayClient say = new ClientConsole.SaySR.SayClient();
Console.WriteLine(say.SayHello(yourName));
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SayHello;
namespace ClientConsole
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " Please input your name

string yourName = Console.ReadLine();
// ChannelFactory<ISay> cf = new ChannelFactory<ISay>(new BasicHttpBinding(), new EndpointAddress(" http://localhost :8000/SayHello1"));
// ISay say = cf.CreateChannel();
SaySR.SayClient say = new ClientConsole.SaySR.SayClient();
Console.WriteLine(say.SayHello(yourName));
Console.ReadLine();
}
}
}