question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

While browsing svc getting ProtocolException

See original GitHub issue

Hi, I am trying a simple sample method in .net5. From Visual Studio, while viewing in browser, getting below error http://localhost:8750/MyService/MyService.svc

An unhandled exception occurred while processing the request. XmlException: The body of the message cannot be read because it is empty. Unknown location ProtocolException: There is a problem with the XML that was received from the network. See inner exception for more details. CoreWCF.Channels.AspNetCoreReplyChannel.HandleRequest(HttpContext context)

Could you please help me resolve the issue? Complete sample code copied below…

MyService.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <AssemblyName>MyService</AssemblyName>
    <RootNamespace>SampleService</RootNamespace>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="MyService.svc" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="MyService.svc" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="CoreWCF.ConfigurationManager" Version="0.3.2" />
    <PackageReference Include="CoreWCF.Primitives" Version="0.3.2" />
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
    <PackageReference Include="System.ServiceModel.Primitives" Version="4.9.0" />
  </ItemGroup>

</Project>

Program.cs

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace SampleService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
      WebHost.CreateDefaultBuilder(args)
          .UseStartup<Startup>();
    }
}

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using CoreWCF.Configuration;
using CoreWCF;

namespace SampleService
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddServiceModelServices();
            services.AddServiceModelConfigurationManagerFile("web.config");
            services.Configure<IISServerOptions>(options => 
            {
                options.AllowSynchronousIO = true;
            });
            services.AddRouting();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseRouting();
            app.UseServiceModel(builder =>
             {
                 builder
                     .AddService<MyService>()
                     .AddServiceEndpoint<MyService, IMyService>(new BasicHttpBinding(), "MyService.svc");

             });
            
        }
    }
}

IMyService.cs

using CoreWCF;

namespace SampleService
{
    [ServiceContract(Namespace = "http://SampleService/")]
    public interface IMyService
    {
        [OperationContract(Action = "http://SampleService/SimpleMethod", ReplyAction = "http://SampleService/SimpleMethod")]
        string SimpleMethod(string msg);
    }
}

MyService.cs

using CoreWCF;

namespace SampleService
{
    [ServiceBehavior(Namespace = "http://SampleService/")]
    public class MyService : IMyService
    {
        public string SimpleMethod(string msg)
        {
            return "Hello " + msg;
        }
    }
}

launchSettings.json

{
  "IIS": {
    "commandName": "IIS",
    "launchBrowser": true,
    "launchUrl": "http://localhost:8750/MyService/MyService.svc",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
  },
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:8750/MyService/MyService.svc",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyService": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:5001;http://localhost:5000"
    }
  }
}

MyService.svc

<%@ServiceHost Language="C#" Debug="true" Service="SampleService.MyService" CodeBehind="~/App_Code/MyService.cs" %>

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<system.web>
		<identity impersonate="false" />
		
	</system.web>
	<system.webServer>
		<directoryBrowse enabled="false" />
		<handlers>
			<add name="aspNetCore" path="*.svc" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
        </handlers>
		<aspNetCore processPath="dotnet" arguments=".\MyService.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
        <defaultDocument>
            <files>
                <add value="MyService.svc" />
            </files>
        </defaultDocument>
	</system.webServer>
	<system.serviceModel>
		<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
		<bindings>
			<basicHttpBinding>
				<binding name="LargeSettings" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
					<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
					<security mode="None" />
				</binding>
			</basicHttpBinding>
		</bindings>
		<behaviors>
			<serviceBehaviors>
				<behavior name="ServiceMetaData">
					<serviceMetadata httpGetEnabled="true" />
					<serviceDebug includeExceptionDetailInFaults="true" />
				</behavior>
			</serviceBehaviors>
		</behaviors>
		<services>
			<service name="SampleService.MyService" behaviorConfiguration="ServiceMetaData">
				<endpoint bindingNamespace="http://SampleService/"
					address="http://localhost:8750/MyService/MyService.svc" 
				binding="basicHttpBinding" bindingConfiguration="LargeSettings" 
				contract="SampleService.IMyService"  />
			</service>
		</services>
	</system.serviceModel>
</configuration>

Editted by @mconnew for formatting

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mconnewcommented, Dec 3, 2021

This is exactly the problem, there’s no code in there to handle this case yet. I think normally you would get a bad request response from WCF as the method isn’t supported without the metadata stuff. I’m currently working on adding WSDL support, so I’ll add this to the things I need to fix, making sure we return a response consistent with what WCF currently returns when metadata hasn’t been enabled.

0reactions
petersladekcommented, Dec 3, 2021

Btw I managed to get it working by bypassing CoreWCF middleware. Just add following in Configure() right before app.UseServiceModel().

app.MapWhen(context => context.Request.Method == "GET", app =>
{
    app.Run(context =>
    {
        context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
        return Task.CompletedTask;
    });
});

app.UseServiceModel(...)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Protocol Exception when i try to call WCF from ...
browse the svc file first to check that everyhting is ok. Also, set set to True property includeExceptionDetailInFaults in service config. – ...
Read more >
ProtocolException Class (System.ServiceModel)
The exception seen on the client that is thrown when communication with the remote party is impossible due to mismatched data transfer protocols....
Read more >
WCF & useless ProtocolException - mnaoumov.NET
Imagine we have a program which uses WCF services. Sometimes your WCF service stops to work and you are getting such exception on...
Read more >
How to Browse the SVC files for Revit Server
Solution: Open IIS Manager (Start > Run > search "inetmgr"). Browse from the top level server to Sites and expand the Default Web...
Read more >
Service | Android Developers
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found