.Net Core - WCF 无法识别的消息版本

.Net Core - WCF unrecognized message version

提问人:Mehmet Kayacan 提问时间:10/17/2023 最后编辑:Mehmet Kayacan 更新时间:10/17/2023 访问量:37

问:

我在 webapp .net4 应用程序中使用的 WCF 服务使用以下代码。但是,当我更新到 .net Core 时,我收到各种错误,并且解决方案不起作用。

在旧版本中工作的代码

 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
 XXX.ZAZ_WO_WS ws = new XXX.ZAZ_WO_WS();
 ServicePointManager.Expect100Continue = false;
 ws.Proxy = HttpWebRequest.GetSystemWebProxy();
 ws.Proxy.Credentials = CredentialCache.DefaultCredentials;
 NetworkCredential nCre = new NetworkCredential("Mehmet", "123456");
 ws.Credentials = nCre;

XXX.ZCheckStock theCheckStock = new XXX.ZCheckStock();
XXX.ZCheckStockResponse theCheckStockResponse = new XXX.ZCheckStockResponse();
theCheckStock.IMatnr = "1";
theCheckStock.ITplnr = "2";
theCheckStockResponse = ws.ZCheckStock(theCheckStock);`



核心版本第一次尝试

var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);

binding.MessageEncoding = WSMessageEncoding.Text;
binding.UseDefaultWebProxy = true;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
var endpointAddress = new EndpointAddress("http://example.com:8000/sap/bc/srt/wsdl/bndg_xx/wsdl11/allinone/ws_policy/document?sap-client=140");

XXX.ZAZ_WO_WSClient wsClient = new XXX.ZAZ_WO_WSClient(binding, endpointAddress);

using (OperationContextScope scope = new OperationContextScope(wsClient.InnerChannel))
{
    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
    requestMessage.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("Mehmet:123456"));
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

    ZCheckStock theCheckStock = new ZCheckStock
    {
        IMatnr = "1",
        ITplnr = "2"
    };

    ZCheckStockResponse theCheckStockResponse = wsClient.ZCheckStock(theCheckStock);

   wsClient.Close();

核心版本第 2 次尝试

  `    string serviceEndpoint = "http://example.com:8000/sap/bc/srt/wsdl/bndg_xx/wsdl11/allinone/ws_policy/document?sap-client=140";

        BasicHttpBinding binding = new BasicHttpBinding
        {
            Security = { Mode = BasicHttpSecurityMode.TransportCredentialOnly, Transport = { ClientCredentialType = HttpClientCredentialType.Basic } }
        };

        var client = new ZAZ_WO_WSClient(binding, new EndpointAddress(serviceEndpoint));

        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

        var credentials = new NetworkCredential("Mehmet", "123456");
        client.ClientCredentials.UserName.UserName = credentials.UserName;
        client.ClientCredentials.UserName.Password = credentials.Password;

        try
        {
            var request = new ZCheckStock
            {
                IMatnr = "1",
                ITplnr = "2"
            };

            var response = client.ZCheckStock(request);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
            return BadRequest("An error occurred: " + ex.Message);
        }
        finally
        {
            client.Close();
        }`

我尝试将该方法与 2 种不同的方法一起使用

.NET ASP.Net-Core WCF 肥皂

评论


答: 暂无答案