Tuesday, January 29, 2013

Page_Load executing twice

Correct Page_Load from loading twice
Check to make sure that the .aspx page and .cs page are not both wiring up Page_Load.

.aspx page:

<%@ Page Language="C#" AutoEventWireup="false"...

.aspx.cs code behind:

private void InitializeComponent()

{
     this.Load += new System.EventHandler(this.Page_Load);
}  

If AutoEventWireup="true" and this.Load wires up the Page_Load method, either remove the manual wireup or set AutoEventWireup="false".

Thursday, January 17, 2013

WCF Adding HttpHeaders to ChannelFactory request

To add credentials to WCF ChannelFactory request:
Create Set up ChannelFactory with desired binding:
ChannelFacotry<ITest>factory = new ChannelFactory<ITest>(new BasicHttpBinding(BasicHttpSecurityMode.None));

Create proxy using EndpointAddress:
ITest proxy = factory.CreateChannel(new EndpointAddress(TheEndpointAddress));
Create OperationContextScope with the proxy:
OperationContextScope contextScope = new OperationContextScope(proxy)
Create HttpRequestMessageProperty and configure credentials:
HttpRequestMessageProperty httpHeaders = new HttpRequestMessageProperty();
httpHeaders.Headers[USERNAME_HTTP_HEADER] = Username;

httpHeaders.Headers[PASSWORD_HTTP_HEADER] = Password;

Add HttpRequestMessageProperty to the OperationContexts outgoing message properties:
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpHeaders;