Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: In a simple setup with co-hosted controllers: System.ObjectDisposedException: Message is closed. #1356

Open
1 task done
CezaryKlus opened this issue Mar 26, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@CezaryKlus
Copy link

Duplicate ?

  • I have searched issues/discussions and did not find other issues/discussions reporting this bug.

Product version

1.5.2

Describe expected behavior

CoreWCF services function properly.

Describe actual behavior

Receiving an exception
System.ObjectDisposedException: Message is closed.
with the following stack trace

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.ObjectDisposedException: Message is closed.
         at CoreWCF.Channels.BufferedMessage.get_Properties()
         at CoreWCF.Channels.AspNetCoreReplyChannel.HandleRequest(HttpContext context)
         at CoreWCF.Channels.RequestDelegateHandler.HandleRequest(HttpContext context)
         at CoreWCF.Channels.ServiceModelHttpMiddleware.InvokeAsync(HttpContext context)
         at CoreWCF.Channels.MetadataMiddleware.InvokeAsync(HttpContext context)
         at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
         at NSwag.AspNetCore.Middlewares.SwaggerUiIndexMiddleware.Invoke(HttpContext context)
         at NSwag.AspNetCore.Middlewares.RedirectToIndexMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Absolutely no special middleware/interceptor that can read the message has been registered.

My setup:

        builder.Services.AddServiceModelServices();
        builder.Services.AddServiceModelMetadata();
        builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();
        app.UseStaticFiles(...);

        app.UseSwaggerUi(x => x.DocumentPath = "/openapi.yaml");

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseServiceModel(serviceBuilder =>
        {
            serviceBuilder.AddService<TransactionalService>();
            serviceBuilder.AddServiceEndpoint<TransactionalService, ITransactionalService>(new WSHttpBinding(SecurityMode.None), "/soap");
            var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>();
            serviceMetadataBehavior.HttpGetEnabled = true;
        });

        app.MapControllers();

Which binding

WsHttp

security

None

Which .NET version

.NET 7

Which os platform

Windows

Code snippet used to reproduce the issue

No response

Stacktrace if any

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.ObjectDisposedException: Message is closed.
         at CoreWCF.Channels.BufferedMessage.get_Properties()
         at CoreWCF.Channels.AspNetCoreReplyChannel.HandleRequest(HttpContext context)
         at CoreWCF.Channels.RequestDelegateHandler.HandleRequest(HttpContext context)
         at CoreWCF.Channels.ServiceModelHttpMiddleware.InvokeAsync(HttpContext context)
         at CoreWCF.Channels.MetadataMiddleware.InvokeAsync(HttpContext context)
         at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
         at NSwag.AspNetCore.Middlewares.SwaggerUiIndexMiddleware.Invoke(HttpContext context)
         at NSwag.AspNetCore.Middlewares.RedirectToIndexMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
@CezaryKlus CezaryKlus added the bug Something isn't working label Mar 26, 2024
@CezaryKlus
Copy link
Author

This is something related to the SOAP message processing/validation.
What makes the error happen is the SOAP message with the default namespace defined e.g.
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:int="..." **xmlns=**"...">
and content like

   <soap:Header/>
   <soap:Body>
      <int:x>
         <int:y>
            **<z></z>**

as soon as I change the message to
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:int="..." **xmlns:mes=**"...">

   <soap:Header/>
   <soap:Body>
      <int:x>
         <int:y>
            **<mes:z></mes:z>**

it gets processed successfully.
Although was not able to reproduce in a project created from scratch based on CoreWCF template.

@jimm98y
Copy link

jimm98y commented May 19, 2024

I'm running into this as well with Onvif messages:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
   <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <GetCapabilities xmlns="http://www.onvif.org/ver10/device/wsdl">
            <Category>All</Category>
       </GetCapabilities>
   </s:Body>
</s:Envelope>

CoreWCF fails with:

System.ObjectDisposedException: 'Message is closed.'

>	CoreWCF.Primitives.dll!CoreWCF.Channels.BufferedMessage.Properties.get() Line 1615	C#
 	CoreWCF.Http.dll!CoreWCF.Channels.AspNetCoreReplyChannel.HandleRequest(Microsoft.AspNetCore.Http.HttpContext context) Line 127	C#
 	CoreWCF.Http.dll!CoreWCF.Channels.RequestDelegateHandler.HandleRequest(Microsoft.AspNetCore.Http.HttpContext context) Line 125	C#
 	CoreWCF.Http.dll!CoreWCF.Channels.ServiceModelHttpMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context) Line 49	C#
 	CoreWCF.Primitives.dll!CoreWCF.Channels.MetadataMiddleware.BuildBranch.AnonymousMethod__2(Microsoft.AspNetCore.Http.HttpContext reqContext) Line 103	C#
 	CoreWCF.Primitives.dll!CoreWCF.Channels.MetadataMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context) Line 53	C#
 	Microsoft.AspNetCore.Diagnostics.dll!Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(Microsoft.AspNetCore.Http.HttpContext context)	Unknown
 	Microsoft.AspNetCore.HostFiltering.dll!Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context)	Unknown
 	Microsoft.WebTools.BrowserLink.Net.dll!Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context)	Unknown
 	Microsoft.WebTools.BrowserLink.Net.dll!Microsoft.WebTools.BrowserLink.Net.VsContentMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context)	Unknown
 	Microsoft.AspNetCore.Watch.BrowserRefresh.dll!Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context)	Unknown
 	Microsoft.AspNetCore.Server.Kestrel.Core.dll!Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests<Microsoft.AspNetCore.Hosting.HostingApplication.Context>(Microsoft.AspNetCore.Hosting.Server.IHttpApplication<Microsoft.AspNetCore.Hosting.HostingApplication.Context> application)	Unknown
 	Microsoft.AspNetCore.Server.Kestrel.Core.dll!Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync<Microsoft.AspNetCore.Hosting.HostingApplication.Context>(Microsoft.AspNetCore.Hosting.Server.IHttpApplication<Microsoft.AspNetCore.Hosting.HostingApplication.Context> application)	Unknown
 	Microsoft.AspNetCore.Server.Kestrel.Core.dll!Microsoft.AspNetCore.Server.Kestrel.Core.Internal.HttpConnection.ProcessRequestsAsync<Microsoft.AspNetCore.Hosting.HostingApplication.Context>(Microsoft.AspNetCore.Hosting.Server.IHttpApplication<Microsoft.AspNetCore.Hosting.HostingApplication.Context> httpApplication)	Unknown
 	Microsoft.AspNetCore.Server.Kestrel.Core.dll!Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.KestrelConnection<Microsoft.AspNetCore.Connections.ConnectionContext>.ExecuteAsync()	Unknown
 	Microsoft.AspNetCore.Server.Kestrel.Core.dll!Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.KestrelConnection<System.__Canon>.System.Threading.IThreadPoolWorkItem.Execute()	Unknown

I'm using the sample app from this repo with WsHttpBinding (needed for Soap 1.2) and Onvif generated from WSDL using svcutil.exe -sc.

@jimm98y
Copy link

jimm98y commented May 19, 2024

So in my case, it turned out I was using incorrect binding:

var binding = new WSHttpBinding(SecurityMode.None);
binding.Security.Message.ClientCredentialType = MessageCredentialType.None;

I fixed it by using the following which also works on the client side:

    var httpTransportBinding = new HttpTransportBindingElement
    {
        AuthenticationScheme = AuthenticationSchemes.Anonymous
    };
    var textMessageEncodingBinding = new TextMessageEncodingBindingElement
    {
        MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
    };  
    var binding = new CustomBinding(textMessageEncodingBinding, httpTransportBinding);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants