Showing posts with label CoreWCF. Show all posts
Showing posts with label CoreWCF. Show all posts

Tuesday, 21 November 2023

Increasing timeout in CoreWCF project for client

I have tested out CoreWCF a bit and it is good to see WCF once again in a modern framework such as ASP.NET Core. Here is how you can increase timeouts in CoreWCF. You can put the timeout into an appsettings file too if you want. First off, after having added a Service Reference to your WCF service. Look inside the Reference.cs file. Make note of:
  1. Namespace in the Reference.cs file
  2. Class name of the client
My client uses these Nuget packages in its csproj :
  
  
  <ItemGroup>
    <PackageReference Include="System.ServiceModel.Duplex" Version="4.10.*" />
    <PackageReference Include="System.ServiceModel.Federation" Version="4.10.*" />
    <PackageReference Include="System.ServiceModel.Http" Version="4.10.*" />
    <PackageReference Include="System.ServiceModel.NetTcp" Version="4.10.*" />
    <PackageReference Include="System.ServiceModel.Security" Version="4.10.*" />
  </ItemGroup>
  
    
 
 <ItemGroup>
    <PackageReference Include="CoreWCF.Primitives" Version="1.*" />
    <PackageReference Include="CoreWCF.Http" Version="1.*" />
  </ItemGroup> 
 
Look inside the Reference.cs file, a method called ConfigureEndpoint is listed :
	
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
    public partial class ServiceClient : System.ServiceModel.ClientBase, MyService.IService
    {
        
        /// 
        /// Implement this partial method to configure the service endpoint.
        /// 
        /// The endpoint to configure
        /// The client credentials
        static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);

        //more code 
    
    
Next up, implementing this method to configured the binding.
	
    
namespace MyService
{
    public partial class ServiceClient
    {

        /// <summary>
        /// Implement this partial method to configure the service endpoint.
        /// </summary>
        /// <param name="serviceEndpoint">The endpoint to configure</param>
        /// <param name="clientCredentials">The client credentials</param>
        static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials)
        {
            serviceEndpoint.Binding.OpenTimeout 
                = serviceEndpoint.Binding.CloseTimeout
                = serviceEndpoint.Binding.ReceiveTimeout
                = serviceEndpoint.Binding.SendTimeout = TimeSpan.FromSeconds(15);
        }

    }
}
    
    
We also want to be able to configure the timeout here. Lets add the following nuget packages also to the client (I got a .NET 6 console app):
	
 <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
 <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
    
We can also avoid hardcoding timeouts by adding appsettings.json to our project and set the file to copy to output folder. If you are inside a console project you can add json config file like this, preferably registering it in some shared setup in Program.cs, but I found it a bit challenging to consume it from a static method I ended up with this :
 

            /// 
            /// Implement this partial method to configure the service endpoint.
            /// 
            /// The endpoint to configure
            /// The client credentials
            static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials)
            {
                var serviceProvider = new ServiceCollection()
                    .AddSingleton(_ =>
                        new ConfigurationBuilder()
                            .SetBasePath(Path.Combine(AppContext.BaseDirectory))
                            .AddJsonFile("appsettings.json", optional: true)
                            .Build())
                    .BuildServiceProvider();
    
                var config = serviceProvider.GetService();
    
                int timeoutInSeconds = int.Parse(config!["ServiceTimeoutInSeconds"]);
                serviceEndpoint.Binding.OpenTimeout
                    = serviceEndpoint.Binding.CloseTimeout
                    = serviceEndpoint.Binding.ReceiveTimeout
                    = serviceEndpoint.Binding.SendTimeout = TimeSpan.FromSeconds(timeoutInSeconds);
            }
                 
               
And we have our appsettings.json file :


    {
      "ServiceTimeoutInSeconds" :  9
    }
    

The CoreWCF project got an upgrade tool that will do a lot of the migration for you. WCF had a lot of config settings and having an appsettings.json file for every setting will be some work. The upgrade tool should take care of generating some of these config values and add them into dedicated json files for this.