This is the 1st step - Simple TCP
Ok...so let's start than...
1st step, lets implement the easiest WCF "hello world" - just to warm up.
We'll start with the contract:
[ServiceContract]
public interface ISampleContract
{
[OperationContract]
string GetData(Guid identifier);
[OperationContract]
void Execute(Guid identifier);
}
Next we'll implement this interface on server side - our "bussiness logic":
public class testFancyProxyService:ISampleContract
{
public string GetData(Guid identifier)
{
Console.WriteLine("recieved GetData request (id {0})", identifier);
//in real life we'll probably get data using the identifier
return "hello world..";
}
public void Execute(Guid identifier)
{
Console.WriteLine("recieved Execute request (id {0})", identifier);
}
}
Next, lets host the service:
class Program
{
private static ServiceHost serviceHost = null;
static void Main(string[] args)
{
try
{
startListening();
Console.WriteLine("Server is up, press any key to stop it...");
Console.Read();
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error: {0}\n\n Stack:{1}", ex.Message, ex.StackTrace));
Console.Read();
}
finally
{
stopListening();
}
}
private static void startListening()
{
serviceHost = new ServiceHost(typeof(testFancyProxyServer.testFancyProxyService));
// listening for messages.
serviceHost.Open();
}
private static void stopListening()
{
if (serviceHost != null)
{
if (serviceHost.State == CommunicationState.Opened)
{
serviceHost.Close();
}
}
}
}
Configure the server's endpoint to listen on any available TCP port and that's it for the server.
<endpoint address="net.tcp://localhost:8080/testFancyProxy" binding="netTcpBinding"
contract="testFancyProxyContracts.ISampleContract" /;>
Same thing on client side - we'll code a small proxy (yes, you can instead use "add service reference" on visual studio :-)):
public class testFancyProxyProxy:ISampleContract
{
private ChannelFactorychannelFactory;
private ISampleContract proxy;
public testFancyProxyProxy()
{
channelFactory = new ChannelFactory("tcpEndPoint");
proxy = channelFactory.CreateChannel();
}
#region ISampleContract Members
public string GetData(Guid identifier)
{
return proxy.GetData(identifier);
}
public void Execute(Guid identifier)
{
proxy.Execute(identifier);
}
#endregion
}
Configure the client and test it:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:8080/testFancyProxy/"
binding="netTcpBinding"
contract="testFancyProxyContracts.ISampleContract"
name="tcpEndPoint"/>
</client>
</system.serviceModel>
</configuration>
Testing...:
public class testFancyProxyConsumer
{
private testFancyProxyClient.testFancyProxyProxy proxy;
public void Run()
{
proxy = new testFancyProxyProxy();
Console.WriteLine(proxy.GetData(Guid.NewGuid()));
Console.WriteLine("calling Execute..");
proxy.Execute(Guid.NewGuid());
}
}
That's it!! 1st step - nice & simple..this project will be the base of the next steps.
Till next step...
Diego
PS: source download
excellent !! Thank You.
ReplyDelete