Last active
August 29, 2015 14:12
-
-
Save sajayantony/96c687ff189504d00356 to your computer and use it in GitHub Desktop.
WCF Sample for a PipeTransport Based Client server without Service Contracts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace PipeTransportSample | |
{ | |
using System; | |
using System.ServiceModel; | |
using System.ServiceModel.Channels; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var service = new Service(); | |
service.MessageHandler += (msg) => | |
{ | |
var str = msg.GetBody<string>(); | |
Console.WriteLine(str); | |
}; | |
service.Start(); | |
SendMessage(); | |
Console.ReadLine(); | |
} | |
public static void SendMessage() | |
{ | |
var customBinding = new CustomBinding( | |
new BinaryMessageEncodingBindingElement(), | |
new NamedPipeTransportBindingElement()); | |
var factory = customBinding.BuildChannelFactory<IDuplexSessionChannel>(); | |
factory.Open(); | |
var channel = factory.CreateChannel(new EndpointAddress("net.pipe://localhost/mypipeserver")); | |
channel.Open(); //Use async variations if you want it to be non-blocking | |
for (int i = 0; i < 10; i++) | |
{ | |
var message = Message.CreateMessage( | |
customBinding.MessageVersion, | |
"JsonMessage", | |
JsonConvert.SerializeObject(new { value = "test " + i, counter = i })); | |
channel.Send(message); | |
} | |
channel.Close(); | |
} | |
} | |
internal class Service | |
{ | |
private IChannelListener<IDuplexSessionChannel> _listener; | |
public event Action<Message> MessageHandler; | |
public void Start() | |
{ | |
var customBinding = new CustomBinding( | |
new BinaryMessageEncodingBindingElement(), | |
new NamedPipeTransportBindingElement()); | |
_listener = customBinding.BuildChannelListener<IDuplexSessionChannel>( | |
new Uri("net.pipe://localhost/mypipeserver")); | |
_listener.Open(); | |
StartAcceptingClients(); | |
} | |
private async void StartAcceptingClients() | |
{ | |
while (true) | |
{ | |
var channel = await AcceptChannels(); | |
ReceiveMessages(channel); | |
} | |
} | |
private async Task<IDuplexSessionChannel> AcceptChannels() | |
{ | |
var channel = await Task.Factory.FromAsync<IDuplexSessionChannel>( | |
_listener.BeginAcceptChannel, | |
_listener.EndAcceptChannel, | |
(object)null); | |
channel.Open(); | |
return channel; | |
} | |
private async void ReceiveMessages(IDuplexSessionChannel channel) | |
{ | |
while (true) | |
{ | |
Message message = await Task.Factory.FromAsync<Message>( | |
channel.BeginReceive, | |
channel.EndReceive, | |
(object)null); | |
if (message == null) | |
{ | |
channel.Close(); | |
return; | |
} | |
if (MessageHandler != null) | |
{ | |
MessageHandler(message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment