Skip to content

Instantly share code, notes, and snippets.

@jeroenheijmans
Created January 6, 2017 10:21
Show Gist options
  • Save jeroenheijmans/aa69b00863f7f69990a3323c8bfa4d7f to your computer and use it in GitHub Desktop.
Save jeroenheijmans/aa69b00863f7f69990a3323c8bfa4d7f to your computer and use it in GitHub Desktop.
Minimal WCF host and client setup for WCF repros
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Threading.Tasks;
namespace WcfMinimalTest
{
// WARNING! This minimal repro does no cleanup or error handling whatsoever!!
// Loosely based on http://stackoverflow.com/a/7833188/419956 by @Anuraj
public class Program
{
static void Main()
{
Task.Run(() => Host());
var client = new FooProxy();
var result = client.Bar();
Console.WriteLine($"Received result from service via proxy: {result}");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
static void Host()
{
Console.WriteLine("Service starting...");
var host = new ServiceHost(typeof(FooService), new Uri("net.pipe://localhost/"));
host.AddServiceEndpoint(typeof(IFooService), new NetNamedPipeBinding(), "fooservice");
host.Open();
Console.WriteLine("Service started.");
}
}
[ServiceContract]
public interface IFooService
{
[OperationContract]
string Bar();
}
public class FooService : IFooService
{
public string Bar()
{
Console.WriteLine($"Service {nameof(Bar)}");
return "Barrrr, matey!";
}
}
public class FooProxy : ClientBase<IFooService>
{
public FooProxy() : base (new ServiceEndpoint(ContractDescription.GetContract(
typeof(IFooService)),
new NetNamedPipeBinding(),
new EndpointAddress("net.pipe://localhost/fooservice")))
{ }
public string Bar()
{
Console.WriteLine($"Proxy {nameof(Bar)}");
return Channel.Bar();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment