.netcore rpc调用Java端Web服务,引发异常

时间:2018-08-27 09:58:45

标签: c# asp.net-core

下面是我的代码:

BasicHttpBinding basicHttpBinding = null;
EndpointAddress endpointAddress = null;
ChannelFactory<RtServiceImpl> factory = null;
RtServiceImpl serviceProxy = null;

try
{
    basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
    basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
    endpointAddress = new EndpointAddress(new Uri("https://webserviceurl"));
    factory = new ChannelFactory<RtServiceImpl>(basicHttpBinding, endpointAddress);

    factory.Credentials.UserName.UserName = "usrn";
    factory.Credentials.UserName.Password = "passw";
    serviceProxy = factory.CreateChannel();

    using (var scope = new OperationContextScope((IContextChannel)serviceProxy))
    {
        var result = serviceProxy.getMethodAsync("xx", 0, 10).Result;
    }

    factory.Close();
    ((ICommunicationObject)serviceProxy).Close();
}
catch (MessageSecurityException ex)
{
    throw;
}
catch (Exception ex)
{
    throw;
}

当我致电getMethodAsync时,它会引发异常:

序列化消息正文时发生错误:“无法生成临时类(结果= 1)。错误CS0012:在未引用的程序集中定义了类型“ system.object”。引用程序集“ netstandard,版本= 2.0.0.0,文化=中性,必须添加PublicKeyToken = cc7b13ffcd2ddd51。错误CS0012:类型“ system.nullable1”在未引用的程序集中定义。引用程序集“ netstandard,版本= 2.0.0.0,文化=中性,必须添加PublicKeyToken = cc7b13ffcd2ddd51”。 错误CS0012:在未引用的程序集中定义了类型“ system.datetime”。必须添加对程序集“ netstandard,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = cc7b13ffcd2ddd51”的引用。 错误CS0030:无法将类型“ system.nullable1”转换为“ system.datetime”

定义代理类:

 [System.ServiceModel.OperationContractAttribute(Action="", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormatAttribute(Style=System.ServiceModel.OperationFormatStyle.Rpc, SupportFaults=true, Use=System.ServiceModel.OperationFormatUse.Encoded)]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(TrainBean))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(ExamInfoBean))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(TraincMsBean))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(StudyInfoBean))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(TeacherLessonBean))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(TeacherBean))]
    [return: System.ServiceModel.MessageParameterAttribute(Name="getTCTeacherBeanReturn")]
    System.Threading.Tasks.Task<TeacherBean[]> getTCTeacherBeanAsync(string token, int startInex, int endInex);
public System.Threading.Tasks.Task<TeacherBean[]> getTCTeacherBeanAsync(string token, int startInex, int endInex)
    {
        return base.Channel.getTCTeacherBeanAsync(token, startInex, endInex);
    }

1 个答案:

答案 0 :(得分:0)

现在,我使用http协议来调用soap方法。

 if (url.StartsWith("https"))
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        }
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Timeout = timeout;
        request.Method = "POST";
        request.KeepAlive = true;
        request.ContentType = "text/xml;charset=UTF-8";
        request.Headers.Set("SOAPAction", soapAction);
        try
        {
            using (Stream stream = request.GetRequestStream())
            {
                byte[] datas = Encoding.UTF8.GetBytes(soapXml);
                stream.Write(datas, 0, datas.Length);
                stream.Close();
            }
            var response = (HttpWebResponse)request.GetResponse();
            using (Stream stream = response.GetResponseStream())
            {
                if (stream != null)
                {
                    var reader = new StreamReader(stream);
                    return System.Web.HttpUtility.HtmlDecode(reader.ReadToEnd());
                }
            }
        }
        catch (Exception ex)
        {
        }
        return string.Empty;

如果服务器是http,则返回ok。当服务器是https时,尽管我在此处添加了一些代码:

if (url.StartsWith("https"))
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        }

它抛出异常: 请求中止:创建SSL / TLS安全通道失败。 但是在netframework的项目中效果很好。