Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellabyte/2714772 to your computer and use it in GitHub Desktop.
Save kellabyte/2714772 to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
ScheduledTaskService service = new ScheduledTaskService();
ServiceHost host = new ServiceHost(service);
AttachDataContractResolver(host.Description.Endpoints[0]);
host.Open();
IScheduledTaskService client =
CreateClient("net.tcp://localhost:1212");
// Submit an order.
ScheduledTask<OrderTask> task = new ScheduledTask<OrderTask>();
task.Value = new OrderTask(2);
// Submit a shipment.
ScheduledTask<ShipmentTask> task2 = new ScheduledTask<ShipmentTask>();
task2.Value = new ShipmentTask("Parliament");
client.Submit(task);
client.Submit(task2);
Console.ReadKey();
}
private static IScheduledTaskService CreateClient(string endpointAddress)
{
EndpointAddress address = null;
address = new EndpointAddress(endpointAddress);
Binding binding = new NetTcpBinding(SecurityMode.None);
ChannelFactory<IScheduledTaskService> factory =
new ChannelFactory<IScheduledTaskService>(binding, address);
IScheduledTaskService service = factory.CreateChannel();
AttachDataContractResolver(factory.Endpoint);
return service;
}
private static void AttachDataContractResolver(ServiceEndpoint endpoint)
{
ContractDescription cd = endpoint.Contract;
foreach (OperationDescription opdesc in cd.Operations)
{
DataContractSerializerOperationBehavior serializerBehavior =
opdesc.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (serializerBehavior == null)
{
serializerBehavior =
new DataContractSerializerOperationBehavior(opdesc);
opdesc.Behaviors.Add(serializerBehavior);
}
serializerBehavior.DataContractResolver =
new TaskDataContractResolver();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment