我已从提供的WSDL文件创建了WCF服务引用。在C#中我创建了一个带有基本绑定的代理客户端实例,并调用了所需的方法:
public static bool main()
{
Debugger.Launch();
var binding = new BasicHttpsBinding();
binding.Security.Mode = BasicHttpsSecurityMode.Transport;
binding.TextEncoding = System.Text.Encoding.UTF8;
var remoteAddress = new System.ServiceModel.EndpointAddress("https://tester.mysite.de:8443/webservice/OrderNumber");
using (var orderNumberClient = new orderNumberClient(new System.ServiceModel.BasicHttpBinding(BasicHttpSecurityMode.Transport), remoteAddress))
{
string IDSystem = "123";
string IDOSystem = "abc";
//set timeout
orderNumberClient.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, 10000);
orderNumberClient.ClientCredentials.UserName.UserName = "test";
orderNumberClient.ClientCredentials.UserName.Password = "test";
//call web service method
string productResponse = orderNumberClient.getNewOrderNumber(IDSystem, "01", IDOSystem); ;
MessageBox.Show(productResponse);
}
return true;
}
不幸的是,当我打电话给“getNewOrderNumber”时,我收到了一个相当无益的错误。方法:
System.Reflection.TargetInvocationException:Ein Aufrufziel hat einen Ausnahmefehler verursacht。 ---> System.ServiceModel.FaultException: WebService处理异常
服务器堆栈跟踪:
贝 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime 操作,ProxyRpc& RPC)
bei System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway,ProxyOperationRuntime操作,Object [] ins, 对象[]出局,TimeSpan超时)
贝 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime operation)
...
Web服务端没有错误,因为它在SOAPUI中工作正常,我可能在绑定中遗漏了什么吗?
希望有更多关于网络服务知识的人可以了解根本原因。
答案 0 :(得分:0)
事实证明我没有通过电话发送'授权'标题。为了解决这个问题,在使用实际的Web服务方法调用之前,我使用OperationContextScope
类将所需的HttpRequestMessageProperty
添加到代理实例内部通道:
using (OperationContextScope scope = new OperationContextScope(orderNumberClient.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes(orderNumberClient.ClientCredentials.UserName.UserName + ":" +
orderNumberClient.ClientCredentials.UserName.Password));
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
string Response = orderNumberClient.getNewOrderNumber(IDSystem, "01", IDOSystem);
}