在没有App.Config的情况下使用ClearUsernameBinding

时间:2013-10-02 16:00:17

标签: wcf web-services c#-4.0

我已成功使用CUB调用Web服务,但我依赖于app.config中的配置。我想知道是否可以通过代码生成绑定而不向app.config添加任何条目。

我尝试了以下内容:

ClearUsernameBinding binding = new ClearUsernameBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8087/SampleService/");

EchoServiceReference.EchoServiceClient client = new EchoServiceReference.EchoServiceClient(binding, address);
client.ClientCredentials.UserName.UserName = "yaron";
client.ClientCredentials.UserName.Password = "1234";
Console.WriteLine(client.EchoString("hi"));

但我得到了错误:

Envelope Version 'EnvelopeNone (http://schemas.microsoft.com/ws/2005/05/envelope/none)' does not support adding Message Headers.

1 个答案:

答案 0 :(得分:0)

创建绑定后,添加以下代码:

binding.SetMessageVersion(System.ServiceModel.Channels.MessageVersion.Soap11);