我已成功使用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.
答案 0 :(得分:0)
创建绑定后,添加以下代码:
binding.SetMessageVersion(System.ServiceModel.Channels.MessageVersion.Soap11);