0

我正在使用 SoapCore(用于 ASP.NET Core 的 SOAP 协议中间件)在我的本地计算机上使用现有的肥皂文件 (.WSDL) 创建一个 Soap 服务器。

我正在使用以下配置appsettings.json

"FileWSDL":
  {
    "UrlOverride": "/Service.asmx",
    "WebServiceWSDLMapping":
    {
      "Service.asmx":
      {
        "WsdlFile": "SOAP.wsdl",
        "SchemaFolder": "D: /",
        "WsdlFolder": "D: /"
      }
    }
  },
    "VirtualPath": "",

问题是,当我尝试运行应用程序时,出现以下错误:

处理请求时发生未处理的异常。
XmlException:缺少根元素。
System.Xml.XmlTextReaderImpl.Throw(异常 e)

一些帮助?

我正在使用 ASP.NET Core 3.1。

我的启动.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Models;
using SoapCore;
using Microsoft.Extensions.Configuration;

namespace SoapServer
{
    public class Startup
    {
        private readonly IConfiguration Configuration;

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.TryAddSingleton<ISampleService, SampleService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var settings = Configuration.GetSection("FileWSDL").Get<WsdlFileOptions>();
            settings.AppPath = env.ContentRootPath; // The hosting environment root path

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });

            app.UseSoapEndpoint<ISampleService>("/Service.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer, false, null, settings);
        }
    }
}
4

2 回答 2

0

所有 xml 文件(wsdl 文件也是 xml)必须有一个根元素:https ://www.w3schools.com/xml/xml_syntax.asp

XML 文档必须包含一个根元素,它是所有其他元素的父元素

有效示例:

<root>
  <child>
    <subchild>.....</subchild>
  </child>
  <child>
    <subchild>.....</subchild>
  </child>
</root>

无效示例:

  <child>
    <subchild>.....</subchild>
  </child>
  <child>
    <subchild>.....</subchild>
  </child>

因此,要么您的 wsdl 无效,要么应用程序无法访问它并且您使用的是空文件。

于 2021-02-06T11:04:42.633 回答
0

我遇到了同样的问题,我的 XML 很好 - 它只是没有找到它。
我通过在设置对象上设置 AppPath 值来解决它:

var settings = Configuration.GetSection("FileWSDL").Get<WsdlFileOptions>();            
settings.AppPath = AppDomain.CurrentDomain.BaseDirectory; 
app.UseSoapEndpoint<Models.RateLinxWS>("/Shipping.asmx", new BasicHttpBinding() { Name = "RateLinxWSSoap", Namespace = "RateLinxWSSoap" }, SoapSerializer.XmlSerializer, false,null, settings);
于 2021-02-12T19:56:12.260 回答