WCF: Changing ClientCredentials produces "Manual addressing is enabled on this factory, so all messages sent must be pre-addressed."

Martin picture Martin · Jan 21, 2011 · Viewed 9.5k times · Source

can anyone help, i am trying to call a rest service via the channel factory but sending along my credentials... The rest service uses Windows authentication.

But with the following code i get "Manual addressing is enabled on this factory, so all messages sent must be pre-addressed." error when using GetMessage

I know my service works as if i remove Windows authentication it works! BUt with windows authentication on and not changing clientCredentials i get BAD REQUEST whioch i think is normal... so i need to send along my client credentials

I am a little lost.

   ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8000");


  var defaultCredentials = cf.Endpoint.Behaviors.Find<ClientCredentials>();
  cf.Endpoint.Behaviors.Remove(defaultCredentials); 


  // step two - instantiate your credentials
  ClientCredentials loginCredentials = new ClientCredentials();
  loginCredentials.UserName.UserName = "Test";
  loginCredentials.UserName.Password = "test";


  // step three - set that as new endpoint behavior on factory
  cf.Endpoint.Behaviors.Add(loginCredentials); //add required ones


        IService channel = cf.CreateChannel();

        Console.WriteLine(channel.GetMessage("Dhananjay Get"));

        Console.WriteLine(channel.PostMessage("Dhananjay Post"));

Answer

Greg Sansom picture Greg Sansom · Jan 22, 2011

You need to add a webHttp behavior, and wire that behavior up to your endpoint. The end result will look something like this:

<system.serviceModel>
  <services>
    <service ...>
       <endpoint behaviorConfiguration="webHttpBehavior" ...>
       </endpoint>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
    <behavior name="webHttpBehavior">
      <webHttp/>
    </behavior>
    </endpointBehaviors>
  </behaviors>
  ...
 </system.serviceModel>

If this doesn't help, please post your web.config.