【问题标题】:.netcore rpc call java side webservice, throws exception.netcore rpc调用java端webservice,抛出异常
【发布时间】:2018-08-27 09:58:44
【问题描述】:

下面是我的代码:

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 时,它会抛出异常:

序列化消息正文时出错:“无法生成临时类 (result=1)。错误 CS0012:类型“system.object”在未引用的程序集中定义。对程序集的引用“netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" 必须添加。错误 CS0012: type "system.nullable1" is defined in an unreferenced assembly.References to assembly"netstandard, Version=2.0.0.0, Culture=neutral , 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);
    }

【问题讨论】:

  • 你能用英文打印例外吗?你不能指望有人会读中文(对不起,如果我错了)字母
  • @hellow,对不起。我试着把它改成英文
  • 您的项目目标是netcoreapp2.0 还是net47?与我们分享生成的getMethodAsync 的定义。尝试在您的.csproj 中添加&lt;Reference Include="netstandard" /&gt;。这个问题出现在这个特定的方法还是所有的服务方法上?
  • 项目类型为 netcoreapp2.0,则 netstandard 引用默认包含在此类型项目中。我在netframework项目中测试调用它使用webservice引用,效果很好。生成代理类的定义,请看问题的正文。我稍后会更新,谢谢。

标签: c# asp.net-core


【解决方案1】:

现在,我使用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 安全通道。 但它在网络框架的项目中运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2012-09-25
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多