[英]Web Service call error
我已經從提供的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發生異常。 ---> System.ServiceModel.FaultException:Web服務處理異常
服務器堆棧跟蹤:
bei System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime操作,ProxyRpc&rpc)
bei System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布爾型單向,ProxyOperationRuntime操作,Object [] ins,Object [] out,TimeSpan超時)
bei System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)
...
這不是Web服務方面的錯誤,因為在SOAPUI中可以正常工作,我是否可能在綁定中缺少某些內容?
希望對網絡服務有更多了解的人可以闡明根本原因。
事實證明,我沒有隨電話一起發送“ Authorization”標頭。 為了克服該問題,在進行實際的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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.