我现在尝试至少两个小时,到处搜索,但无法弄清楚如何在代码中写这个:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="TCTBinding">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="xxx" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://omitted"
binding="basicHttpBinding" bindingConfiguration="TCTBinding"
contract="ServiceReference1.DoTheThing" name="HTTPS_Port" />
</client>
我想出了这个:
var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
binding.Security.Transport.Realm = "xxx";
string url = "https://omitted";
using (ServiceReference1.TCTClient cl = new ServiceReference1.TCTClient(binding, new EndpointAddress(url)))
{
cl.ClientCredentials.UserName.UserName = "user";
cl.ClientCredentials.UserName.Password = "pass";
var req = new ServiceReference1.MyRequest()
{
DATA_START = "1234",
DATA_STOP = "1234"
};
var x = cl.DoTheThing(req);
richTextBox1.Text += "RESPONSE FROM SERVER: " + x.RESPONSE;
}
但它不起作用:
Type : System.ServiceModel.FaultException
Message : Server Error
Source : mscorlib
DeclaringType : System.Runtime.Remoting.Proxies.RealProxy
TargetSite : Void HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage, System.Runtime.Remoting.Messaging.IMessage)
StackTrace:
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
然而这有效(从app.config获取绑定):
using (ServiceReference1.TCTClient cl = new ServiceReference1.TCTClient(HTTPS_Port))
{
cl.ClientCredentials.UserName.UserName = "user";
cl.ClientCredentials.UserName.Password = "pass";
var req = new ServiceReference1.MyRequest()
{
DATA_START = "1234",
DATA_STOP = "1234"
};
var x = cl.DoTheThing(req);
richTextBox1.Text += "RESPONSE FROM SERVER: " + x.RESPONSE;
}
我错过了什么?
答案 0 :(得分:0)
尝试使用ChannelFactory:
,而不是像这样新增服务ChannelFactory<ServiceReference1.TCTClient> chan =
new ChannelFactory<ServiceReference1.ITCTClient>(binding, new EndpointAddress(url));
chan.Open();
ServiceRefernece1.TCTClient client = chan.CreateChannel();
client.DotheThing(....