user132706
user132706

Reputation: 41

Remove server response header at self hosted WCF

I know that there are a quite some posts online about removing the server header at http response, however, mostly I found out they are for IIS hosted WCF service. My application is hosting the WCF service. The object is ServiceHost, I added a behavior to the ServiceHost

serviceHost.Description.Behaviors.Add(ModifyResponseBehavior);

In the behavior,

public void BeforeSendReply(ref Message reply, object correlationState)
{
    var httpCtx = System.ServiceModel.Web.WebOperationContext.Current;
    if (httpCtx != null)
    {
        httpCtx.OutgoingResponse.Headers.Add("Server", string.Empty);
    }
}

I tried add empty server value, and remove server headers, neither of them remove the server header at response. Will any of you have a suggestion about this?

Thank you very much!

Upvotes: 0

Views: 866

Answers (1)

Adrya
Adrya

Reputation: 3597

The only way i could remove it for my self-hosted service is set in Windows registry:
HKLM\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\DisableServerHeader = 2
Restart the machine after the change and this will remove the Server header for all self-hosted services on that machine.

Upvotes: 1

Related Questions