Query on C# ASP.NET Core Web App (Model-View-Controller)

78 Views Asked by At

The property I am facing issue with is of FieldDictionary datatype that is not returning the value when sending a GET request and receiving the response in ServiceStack version 6.9 After upgrading from ServiceStack 4.5.14 to 6.9 Fields Value is Null in response.Results(Null Reference Exception in Fields Property)

The structure of FieldDictionary property is as below:

   [DataContract]
   public class ExampleClass
   {
    [DataMember]
    [ApiMember(ExcludeInSchema = true, DataType = "object", 
    Verb = "NONE")]
    public FieldDictionary Fields { get; set; }
   }


   [CollectionDataContract(Name = "Field", ItemName = 
    "Field")]
    public partial class FieldDictionary : 
     Dictionary<string, IProperty>
    {
    }

so how do I create a web app wherein i send a get request and in the reponse i need to check FieldDictionary parameter's response values

Hence I am creating a reproducible web app of this issue to narrow down the root cause below is what i have tried

Program.cs

using ServiceStack;
using Servicestackreplication;

namespace Servicestackreplication
{
    public class Program
    {
        public static void Main(string[] args)
        {

            AppHost appHost = new AppHost();
            appHost.Init();

            CreateHostBuilder(args, appHost as AppHostBase).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args, AppHostBase appHost) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.ConfigureServices(services =>
                    {
                        // Register ServiceStack services
                        services.AddSingleton(appHost);
                    });
                });
    }
}

Startup.cs

using ServiceStack;
using System.ComponentModel;

namespace Servicestackreplication
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }

        public void ConfigureContainer(Container container)
        {
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers(); 
            });
        }
    }
}

AppHost.cs

using Funq;
using ServiceStack;


namespace Servicestackreplication
{
    public class AppHost : AppHostBase
    {
        public AppHost() : base("ss", typeof(SampleService).Assembly)
        {
        }


        public override void Configure(Container container)
        {

            Plugins.Add(new SharpPagesFeature());
        }

    }
}

MyServices.cs

using System.Collections.Generic;

using ServiceStack;

namespace Servicestackreplication
{
    [Route("/sample", "GET")]
    public class SampleRequest : IReturn<SampleResponse>
    {
    }

    public class SampleResponse
    {
        public string Name  { get; set; }
        public int Age { get; set; }
        public FieldDictionary FieldDictionary { get; set; }
    }

    public class FieldDictionary : Dictionary<string, IProperty>
    {
    }

    public interface IProperty
    {

    }
    public class SampleService : Service
    {
        public object Get(SampleRequest request)
        {

            var response = new SampleResponse
            {
                Name = "ah",
                Age = 30,
                FieldDictionary = new FieldDictionary
         {
    { "Key1", new PropertyImplementation() },
    { "Key2", new PropertyImplementation() }
          }

            };
            //Log.Debug($"Name: {response.Name}, Age: {response.Age}");

            //foreach (var entry in response.FieldDictionary)
            //{
            //    Log.Debug($"{entry.Key}: {entry.Value}");
            //}
            return response;
        }
    }

    public class PropertyImplementation : IProperty
    {

    }
}

I am getting below error in line in AppHost.cs

public AppHost() : base("ss", typeof(SampleService).Assembly)
        {
        }

System.TypeLoadException: 'Could not load type 'System.Web.IHttpHandler' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.'

Please help me resolve this

0

There are 0 best solutions below