繁体   English   中英

Web服务调用错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM