Created
May 16, 2012 23:07
-
-
Save kellabyte/2714772 to your computer and use it in GitHub Desktop.
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
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