Skip to content

Commit

Permalink
Add another sample: Announcement is done via UDP, discovery is HTTP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Atsushi Eno committed Sep 2, 2010
1 parent d721616 commit 8eef4a4
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 10 deletions.
31 changes: 21 additions & 10 deletions samples/wcf/discovery/Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@

DISCOVERY_SOURCES = sample-discovery.cs AnnouncementBoundDiscoveryService.cs InspectionBehaviors.cs
SERVICE_SOURCES = sample-service.cs ContractTypes.cs
CLIENT_SOURCES = sample-client.cs ContractTypes.cs InspectionBehaviors.cs
DISCOVERY_SOURCES = AnnouncementBoundDiscoveryService.cs InspectionBehaviors.cs
SERVICE_SOURCES = ContractTypes.cs InspectionBehaviors.cs
CLIENT_SOURCES = ContractTypes.cs InspectionBehaviors.cs

all: \
udp-service.exe udp-client.exe \
sample-discovery.exe sample-service.exe sample-client.exe
sample-discovery.exe sample-service.exe sample-client.exe \
sample-discovery2.exe sample-service2.exe sample-client2.exe

udp-service.exe : udp-service.cs
dmcs -debug -pkg:wcf udp-service.cs -r:System.ServiceModel.Discovery.dll

udp-client.exe : udp-client.cs
dmcs -debug -pkg:wcf udp-client.cs -r:System.ServiceModel.Discovery.dll

sample-discovery.exe : $(DISCOVERY_SOURCES)
dmcs -pkg:wcf -r:System.ServiceModel.Discovery $(DISCOVERY_SOURCES)
sample-discovery.exe : sample-discovery.cs $(DISCOVERY_SOURCES)
dmcs -pkg:wcf -r:System.ServiceModel.Discovery sample-discovery.cs $(DISCOVERY_SOURCES)

sample-service.exe : $(SERVICE_SOURCES)
dmcs -pkg:wcf -r:System.ServiceModel.Discovery $(SERVICE_SOURCES)
sample-service.exe : sample-service.cs $(SERVICE_SOURCES)
dmcs -pkg:wcf -r:System.ServiceModel.Discovery sample-service.cs $(SERVICE_SOURCES)

sample-client.exe : $(CLIENT_SOURCES)
dmcs -pkg:wcf -r:System.ServiceModel.Discovery $(CLIENT_SOURCES)
sample-client.exe : sample-client.cs $(CLIENT_SOURCES)
dmcs -pkg:wcf -r:System.ServiceModel.Discovery sample-client.cs $(CLIENT_SOURCES)

sample-discovery2.exe : sample-discovery2.cs $(DISCOVERY_SOURCES)
dmcs -pkg:wcf -r:System.ServiceModel.Discovery sample-discovery2.cs $(DISCOVERY_SOURCES)

sample-service2.exe : sample-service2.cs $(SERVICE_SOURCES)
dmcs -pkg:wcf -r:System.ServiceModel.Discovery sample-service2.cs $(SERVICE_SOURCES)

sample-client2.exe : sample-client2.cs $(CLIENT_SOURCES)
dmcs -pkg:wcf -r:System.ServiceModel.Discovery sample-client2.cs $(CLIENT_SOURCES)

clean:
rm -f udp-service.exe udp-service.exe.mdb udp-client.exe udp-client.exe.mdb

rm -f sample-discovery.exe sample-service.exe sample-client.exe
rm -f sample-discovery2.exe sample-service2.exe sample-client2.exe
64 changes: 64 additions & 0 deletions samples/wcf/discovery/sample-client2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Discovery;

public class Test
{
public static void Main ()
{
RunCodeUnderDiscoveryHost (new Uri ("http://localhost:37564"), new Uri ("http://localhost:4949"), UseCase1Core);
}

static void RunCodeUnderDiscoveryHost (Uri serviceUri, Uri dHostUri, Action<Uri,DiscoveryEndpoint> action)
{
var dbinding = new CustomBinding (new HttpTransportBindingElement ());
var dAddress = new EndpointAddress (dHostUri);
var dEndpoint = new DiscoveryEndpoint (dbinding, dAddress);
var ib = new InspectionBehavior ();
ib.RequestSending += delegate (ref Message msg, IClientChannel channel) {
var mb = msg.CreateBufferedCopy (0x10000);
msg = mb.CreateMessage ();
Console.WriteLine (mb.CreateMessage ());
return null;
};
ib.ReplyReceived += delegate (ref Message msg, object id) {
var mb = msg.CreateBufferedCopy (0x10000);
msg = mb.CreateMessage ();
Console.WriteLine (mb.CreateMessage ());
};
dEndpoint.Behaviors.Add (ib);

action (serviceUri, dEndpoint);
}

static void UseCase1Core (Uri serviceUri, DiscoveryEndpoint dEndpoint)
{
// actual client, with DiscoveryClientBindingElement
var be = new DiscoveryClientBindingElement () { DiscoveryEndpointProvider = new ManagedDiscoveryEndpointProvider (dEndpoint) };
var clientBinding = new CustomBinding (new BasicHttpBinding ());
clientBinding.Elements.Insert (0, be);
clientBinding.SendTimeout = TimeSpan.FromSeconds (10);
clientBinding.ReceiveTimeout = TimeSpan.FromSeconds (10);
var cf = new ChannelFactory<ITestService> (clientBinding, DiscoveryClientBindingElement.DiscoveryEndpointAddress);
var ch = cf.CreateChannel ();
Console.WriteLine (ch.Echo ("TEST"));
}

class ManagedDiscoveryEndpointProvider : DiscoveryEndpointProvider
{
public ManagedDiscoveryEndpointProvider (DiscoveryEndpoint endpoint)
{
this.endpoint = endpoint;
}

DiscoveryEndpoint endpoint;

public override DiscoveryEndpoint GetDiscoveryEndpoint ()
{
return endpoint;
}
}

}

55 changes: 55 additions & 0 deletions samples/wcf/discovery/sample-discovery2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Discovery;

public class Test
{
public static void Main ()
{
RunCodeUnderDiscoveryHost (new Uri ("http://localhost:37564"), new Uri ("http://localhost:4949"));
}

static void RunCodeUnderDiscoveryHost (Uri serviceUri, Uri dHostUri)
{
// announcement service
var aEndpoint = new UdpAnnouncementEndpoint (new Uri ("soap.udp://239.255.255.250:3802/"));

// discovery service
var dbinding = new CustomBinding (new HttpTransportBindingElement ());
var dAddress = new EndpointAddress (dHostUri);
var dEndpoint = new DiscoveryEndpoint (dbinding, dAddress);
// Without this, .NET rejects the host as if it had no service.
dEndpoint.IsSystemEndpoint = false;
var ib = new InspectionBehavior ();
ib.RequestReceived += delegate (ref Message msg, IClientChannel channel, InstanceContext instanceContext) {
var mb = msg.CreateBufferedCopy (0x10000);
msg = mb.CreateMessage ();
Console.Error.WriteLine (mb.CreateMessage ());
return null;
};
ib.ReplySending += delegate (ref Message msg, object o) {
var mb = msg.CreateBufferedCopy (0x10000);
msg = mb.CreateMessage ();
Console.Error.WriteLine (mb.CreateMessage ());
};

dEndpoint.Behaviors.Add (ib);
aEndpoint.Behaviors.Add (ib);

// it internally hosts an AnnouncementService
using (var inst = new AnnouncementBoundDiscoveryService (aEndpoint)) {
var host = new ServiceHost (inst);
host.AddServiceEndpoint (dEndpoint);
host.Description.Behaviors.Find<ServiceDebugBehavior> ()
.IncludeExceptionDetailInFaults = true;
host.Open ();
Console.WriteLine ("Type [CR] to quit...");
Console.ReadLine ();
host.Close ();
}
}
}


54 changes: 54 additions & 0 deletions samples/wcf/discovery/sample-service2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Discovery;

public class Test
{
public static void Main ()
{
RunCodeUnderDiscoveryHost (new Uri ("http://localhost:37564"), new Uri ("http://localhost:4949"), UseCase1Core);
}

static void RunCodeUnderDiscoveryHost (Uri serviceUri, Uri dHostUri, Action<Uri,AnnouncementEndpoint,DiscoveryEndpoint> action)
{
var aEndpoint = new UdpAnnouncementEndpoint (new Uri ("soap.udp://239.255.255.250:3802/"));
var dbinding = new CustomBinding (new HttpTransportBindingElement ());
var dAddress = new EndpointAddress (dHostUri);
var dEndpoint = new DiscoveryEndpoint (dbinding, dAddress);

var ib = new InspectionBehavior ();
ib.RequestReceived += delegate (ref Message msg, IClientChannel
channel, InstanceContext instanceContext) {
var mb = msg.CreateBufferedCopy (0x10000);
msg = mb.CreateMessage ();
Console.Error.WriteLine (mb.CreateMessage ());
return null;
};
ib.ReplySending += delegate (ref Message msg, object o) {
var mb = msg.CreateBufferedCopy (0x10000);
msg = mb.CreateMessage ();
Console.Error.WriteLine (mb.CreateMessage ());
};

dEndpoint.Behaviors.Add (ib);
aEndpoint.Behaviors.Add (ib);

action (serviceUri, aEndpoint, dEndpoint);
}

static void UseCase1Core (Uri serviceUri, AnnouncementEndpoint aEndpoint, DiscoveryEndpoint dEndpoint)
{
// actual service, announcing to 4989
var host = new ServiceHost (typeof (TestService));
var sdb = new ServiceDiscoveryBehavior ();
sdb.AnnouncementEndpoints.Add (aEndpoint);
host.Description.Behaviors.Add (sdb);
host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), serviceUri);
host.Open ();
Console.WriteLine ("Type [CR] to quit ...");
Console.ReadLine ();
host.Close ();
}
}

0 comments on commit 8eef4a4

Please sign in to comment.