I want to customize the default help page we get for any WCF service.
P.s. : I don't want a custom help page but just want to customize the default help page we get .
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vaikesh K PPosted Aug 21, 2015, 8:18 AM
Here is sample code:
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(XmlDictionaryWriterwriter)
{
// 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();
}
}
Here is the config:
xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service name="foo">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Service/"/>
baseAddresses>
host>
<endpoint address="E1"
binding="basicHttpBinding"
bindingConfiguration="b"
contract="IMyContract"/>
<endpoint address=""
binding="customBinding"
bindingConfiguration="helpPage"
contract="IMyHelpPageContract"/>
service>
services>
<client>
<endpoint address="http://localhost:8000/Service/E1"
binding="basicHttpBinding"
bindingConfiguration="b"
contract="IMyContract"
name="c1"/>
client>
<bindings>
<customBinding>
<binding name="helpPage">
<textMessageEncoding messageVersion ="None" />
<httpTransport/>
binding>
customBinding>
<basicHttpBinding>
<binding name="b"/>
basicHttpBinding>
bindings>
system.serviceModel>
configuration>
Run it, and then hit the base address in a browser, and you'll see the custom help page.
See the sample http://msdn2.microsoft.com/en-us/library/aa395208.aspx for some more details on sending back arbitrary responses over HTTP GET.