Code Snippet
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.Xml;
[ServiceContract]
public interface IMyContract
{
[OperationContract]
string echo(string s);
}
[ServiceContract]
public interface IMyHelpPageContract
{
[OperationContract(Action="*", ReplyAction="*")]
Message Help();
}
[ServiceBehavior(ConfigurationName = "foo")]
public class MyService : IMyContract, IMyHelpPageContract
{
public string echo(string s) { return s; }
public Message Help()
{
return new MyHelpPageMessage();
}
}
abstract class ContentOnlyMessage : Message
{
MessageHeaders headers;
MessageProperties properties;
protected ContentOnlyMessage()
{
this.headers = new MessageHeaders(MessageVersion.None);
}
public override MessageHeaders Headers
{
get
{
if (IsDisposed)
{
throw new ObjectDisposedException("blah");
}
return this.headers;
}
}
public override MessageProperties Properties
{
get
{
if (IsDisposed)
{
throw new ObjectDisposedException("blah");
}
if (this.properties == null)
{
this.properties = new MessageProperties();
}
return this.properties;
}
}
public override MessageVersion Version
{
get
{
return headers.MessageVersion;
}
}
protected override void OnBodyToString(XmlDictionaryWriter writer)
{
OnWriteBodyContents(writer);
}
}
class MyHelpPageMessage : ContentOnlyMessage
{
public MyHelpPageMessage()
: base()
{
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
// write the HTML you want on the help page here.could read from external file if you want
writer.WriteStartElement("HTML");
writer.WriteStartElement("HEAD");
writer.WriteRaw("");
writer.WriteEndElement(); //HEAD
writer.WriteRaw(@"
This is my own help page
I can put whatever content I want here
");
writer.WriteEndElement(); //HTML
}
}
public class Repro
{
public static void Main()
{
ServiceHost service = new ServiceHost(typeof(MyService));
service.Description.Behaviors.Remove<ServiceDebugBehavior>();// need to turn off default help page so as to get our own
service.Open();
Console.WriteLine("Service open at {0} - press a key to continue", service.BaseAddresses[0]);
Console.ReadKey();
ChannelFactory<IMyContract> cf = new ChannelFactory<IMyContract>("c1");
IMyContract proxy = cf.CreateChannel();
Console.WriteLine(proxy.echo("Hello World"));
((IClientChannel)proxy).Close();
service.Close();
Console.WriteLine("Done, press a key");
Console.ReadKey();
}
}