如何解决XmlException:ASP .NET Core 中缺少根元素
我正在使用 SoapCore(用于 ASP.NET Core 的 SOAP 协议中间件)使用本地机器上的现有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。
我的 Startup.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);
}
}
}
解决方法
所有 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 无效,要么是应用程序无法访问它而您使用的是空文件。
,我遇到了同样的问题,我的 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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。