[RESOLVED]compilation error – CS0122: 'mapdistance.DistHandler' is inaccessible due to its protection level (EDITED)

I am querying the Bing Maps api through a rest service, based on information from a
MSDN tutorial.

Originally I started out with a test project in Visual Studio, and the code works there as designed.

Now I have copied the 3 files involved to where it is going to live as part of my site, and I get the error message above.

C:Windowssystem32> "C:WindowsMicrosoft.NETFrameworkv4.0.30319csc.exe" /t:library /utf8output /R:"C:WindowsassemblyGAC_MSILSystem.Data.SqlServerCe4.0.0.0__89845dcd8080cc91System.Data.SqlServerCe.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.ComponentModel.DataAnnotationsv4.0_4.0.0.0__31bf3856ad364e35System.ComponentModel.DataAnnotations.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.WorkflowServicesv4.0_4.0.0.0__31bf3856ad364e35System.WorkflowServices.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl35e9a55a448f4a257_d715ce01RestSharp.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl33ff72591d8ce7474_b735cf01zxing.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.Helpersv4.0_2.0.0.0__31bf3856ad364e35System.Web.Helpers.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Data.Entityv4.0_4.0.0.0__b77a5c561934e089System.Data.Entity.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Activitiesv4.0_4.0.0.0__31bf3856ad364e35System.Activities.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl332bcbe5a68a37e57_d715ce01Newtonsoft.Json.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl3ec6e8f6888e94967_d715ce01iTextSharp.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Drawingv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Drawing.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.WebPagesv4.0_2.0.0.0__31bf3856ad364e35System.Web.WebPages.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl38cf09cbd58f686f8_d615ce01DotNetOpenAuth.OAuth.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILMicrosoft.CSharpv4.0_4.0.0.0__b03f5f7f11d50a3aMicrosoft.CSharp.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Net.Httpv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Net.Http.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Xml.Linqv4.0_4.0.0.0__b77a5c561934e089System.Xml.Linq.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILWebMatrix.WebDatav4.0_2.0.0.0__31bf3856ad364e35WebMatrix.WebData.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl39ac13ad668708ef8_d615ce01DotNetOpenAuth.AspNet.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51App_Code.5dgnjctq.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.DynamicDatav4.0_4.0.0.0__31bf3856ad364e35System.Web.DynamicData.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl3a8d694dd8b946f8_d615ce01DotNetOpenAuth.Core.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILNuGet.Corev4.0_1.6.30117.9648__31bf3856ad364e35NuGet.Core.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Configurationv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Configuration.dll" /R:"C:WindowsMicrosoft.NETFrameworkv4.0.30319mscorlib.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl3846e796b478fff65_726cce01EntityFramework.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystemv4.0_4.0.0.0__b77a5c561934e089System.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.WebPages.Administrationv4.0_2.0.0.0__31bf3856ad364e35System.Web.WebPages.Administration.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Runtime.Serializationv4.0_4.0.0.0__b77a5c561934e089System.Runtime.Serialization.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Corev4.0_4.0.0.0__b77a5c561934e089System.Core.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILWebMatrix.Datav4.0_2.0.0.0__31bf3856ad364e35WebMatrix.Data.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.Extensionsv4.0_4.0.0.0__31bf3856ad364e35System.Web.Extensions.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl3eae21eaa2cb2997_a018cf01Microsoft.Web.Helpers.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.ServiceModel.Webv4.0_4.0.0.0__31bf3856ad364e35System.ServiceModel.Web.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl3f3f60bfd50c835fc_66e2cf01Twilio.WebMatrix.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_32System.Datav4.0_4.0.0.0__b77a5c561934e089System.Data.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.ServiceModelv4.0_4.0.0.0__b77a5c561934e089System.ServiceModel.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Data.DataSetExtensionsv4.0_4.0.0.0__b77a5c561934e089System.Data.DataSetExtensions.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Data.Entity.Designv4.0_4.0.0.0__b77a5c561934e089System.Data.Entity.Design.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.ServiceModel.Activationv4.0_4.0.0.0__31bf3856ad364e35System.ServiceModel.Activation.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.Razorv4.0_2.0.0.0__31bf3856ad364e35System.Web.Razor.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Securityv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Security.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.WebPages.Deploymentv4.0_2.0.0.0__31bf3856ad364e35System.Web.WebPages.Deployment.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.Servicesv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Web.Services.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.ServiceModel.Activitiesv4.0_4.0.0.0__31bf3856ad364e35System.ServiceModel.Activities.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.ApplicationServicesv4.0_4.0.0.0__31bf3856ad364e35System.Web.ApplicationServices.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Net.Http.WebRequestv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Net.Http.WebRequest.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Web.WebPages.Razorv4.0_2.0.0.0__31bf3856ad364e35System.Web.WebPages.Razor.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_32System.EnterpriseServicesv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.EnterpriseServices.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl31608e86e58aa69f8_d615ce01DotNetOpenAuth.OpenId.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILMicrosoft.Web.Infrastructurev4.0_1.0.0.0__31bf3856ad364e35Microsoft.Web.Infrastructure.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl311ca539da81379f8_d615ce01DotNetOpenAuth.OpenId.RelyingParty.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl3c7951ba8b447574_b735cf01zxing.presentation.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl34daf8edb69ddff65_726cce01EntityFramework.SqlServer.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_32System.Webv4.0_4.0.0.0__b03f5f7f11d50a3aSystem.Web.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILMicrosoft.VisualStudio.Web.PageInspector.Loaderv4.0_1.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.Web.PageInspector.Loader.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Xmlv4.0_4.0.0.0__b77a5c561934e089System.Xml.dll" /R:"C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.IdentityModelv4.0_4.0.0.0__b77a5c561934e089System.IdentityModel.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl39de30ca2e4929c02_d615ce01Microsoft.Web.WebPages.OAuth.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl38b2c05e5517e33fc_66e2cf01Twilio.Api.dll" /R:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51assemblydl33c4ebd20c8788af8_d615ce01DotNetOpenAuth.OAuth.Consumer.dll" /out:"C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51App_Web_testdistance.cshtml.cdcab7d2.s-jejj60.dll" /D:DEBUG /debug+ /optimize- /w:4 /nowarn:1659;1699;1701;612;618 /warnaserror-  "C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51App_Web_testdistance.cshtml.cdcab7d2.s-jejj60.0.cs" "C:UsersrobertAppDataLocalTempTemporary ASP.NET Filesroota4e278a519f17d51App_Web_testdistance.cshtml.cdcab7d2.s-jejj60.1.cs"


Microsoft (R) Visual C# Compiler version 4.0.30319.18408

for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

c:UsersrobertDocumentsMy Web Sitesbetestdistance.cshtml(3,31): error CS0122: 'mapdistance.DistHandler' is inaccessible due to its protection level
c:UsersrobertDocumentsMy Web Sitesbetestdistance.cshtml(4,5): error CS0118: 'System.Web.WebPages.WebPageRenderingBase.Response' is a 'property' but is used like a 'type'
c:UsersrobertDocumentsMy Web Sitesbetestdistance.cshtml(4,34): error CS0122: 'mapdistance.DistHandler' is inaccessible due to its protection level
c:UsersrobertDocumentsMy Web Sitesbetestdistance.cshtml(5,14): error CS0122: 'mapdistance.DistHandler' is inaccessible due to its protection level

Googling "Is inaccessessible due to its protection level" does not result in anything useful to me at least.

I am wondering what scenario would cause file to work in one application but not in the other?

testdistance is the razor C code that calls DistHandler class file

mapdistance is the name space inside DistHandler

@using mapdistance;
@{
    string locationsRequest = DistHandler.CreateRequest("Pasedena, MD", "Sparks, MD", "&avoid=minimizeTolls");
    Response locationsResponse = DistHandler.MakeRequest(locationsRequest);
    var rt = DistHandler.ProcessResponse2(locationsResponse);


}

And DistHandler.cs, at least the first few lines:

using System;
using System.Net;
using System.Runtime.Serialization.Json;
using BingMapsRESTService.Common.JSON;


namespace mapdistance
{
    static class DistHandler
    {
        static string BingMapsKey = "blahblah";

        public static string CreateRequest(string queryString1, string queryString2, string options)
        {
            string UrlRequest = "http://dev.virtualearth.net/REST/v1/Routes/Driving" +
             "?wp.0=" + queryString1 +
             "&wp.1=" + queryString2 +
             options +
             "&output=json" +
             " &key=" + BingMapsKey;
            return (UrlRequest);
        }

        public static Response MakeRequest(string requestUrl)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                        throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));
                    DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
                    object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                    Response jsonResponse = objResponse as Response;
                    return jsonResponse;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }


        static public Route ProcessResponse2(Response locationsResponse)
        {
            int locNum = locationsResponse.ResourceSets[0].Resources.Length;

            Route location = (Route)locationsResponse.ResourceSets[0].Resources[0];
            return location;

            //for (int i = 0; i < locNum; i++)
            //{
            //    Route location = (Route)locationsResponse.ResourceSets[0].Resources[i];
            //    var x = location.TravelDistance;
            //}


        }

EDIT: I am adding the main program that calls DistHandler in the VS application (which works)

using System;
using System.Net;
using System.Runtime.Serialization.Json;
using BingMapsRESTService.Common.JSON;
using mapdistance;

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string locationsRequest = DistHandler.CreateRequest("Hunt Valley, MD", "Pasedena, MD", "&avoid=minimizeTolls");
                Response locationsResponse = DistHandler.MakeRequest(locationsRequest);
                var rt =DistHandler.ProcessResponse2(locationsResponse);
                Console.WriteLine(rt.TravelDistance.ToString() + " " + rt.TravelDuration.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }
        }
    }

You’re right. This problem is because by default the accessibility to the classes in .NET are set to be private. This class that you’re showing doesn’t have anything set to it, so the default properties would be applied. 

static class DistHandler

If you can, you can edit it and make it public. Like this

public static class DistHandler

then you will be able to use it. 

Yes, that did it.

Thank you very much!

Leave a Reply