问题
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"));
回答1:
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.
回答2:
Although this issue has an accepted answer, I thought I'd add a little information.
Because of the bug in GetQueryStringConverter
of the WebServiceFactory
class (see Microsoft Connect report), you cannot use <WebHttp/>
if you are passing array's of objects through a parameter. Instead, add the <enableWebScript/>
element to the binding.
Client App.Config or Web.Config
<behaviors>
<endpointBehaviors>
<behavior name="WebHttp_Behaviour">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
Service Web.Config
<behaviors>
<serviceBehaviors>
...
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebHttp_EndPointBehaviour">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
At least, I have had to add <enableWebScript\>
to both client and server.
回答3:
For me I needed to add <webHttp/>
to Behaviours on client side.
来源:https://stackoverflow.com/questions/4759672/wcf-changing-clientcredentials-produces-manual-addressing-is-enabled-on-this-f