0

I have a MVC 5 Web App that has run without problem for 3 years. Yesterday i changed branch and suddenly i get the error "Could not load file or assembly 'System.Web.Http', Version=4.0.0.0". This happens in Entity Framework OnModelCreating (though i'm sure EF isn't the problem).

OnModelCreating:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        var entitymethod = typeof(DbModelBuilder).GetMethod("Entity");


        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            try
            {
                var a = assembly.GetTypes();
            }
            catch(ReflectionTypeLoadException ex)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (Exception exSub in ex.LoaderExceptions)
                {
                    sb.AppendLine(exSub.Message);
                    System.IO.FileNotFoundException exFileNotFound = exSub as System.IO.FileNotFoundException;
                    if (exFileNotFound != null)
                    {
                        if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
                        {
                            sb.AppendLine("Fusion Log:");
                            sb.AppendLine(exFileNotFound.FusionLog);
                        }
                    }
                    sb.AppendLine();
                }
                string errorMessage = sb.ToString();
            }
            //find alle de klasser der arver fra Entity
            var entitytypes = assembly.GetTypes().Where(x => x.IsSubclassOf(typeof(Entity)));
            foreach (var type in entitytypes)
            {
                if(type.Name != "Field")
                //lav et DbSet af hver type
                entitymethod.MakeGenericMethod(type).Invoke(modelBuilder, new object[] { });
            }
        }

note the try catch is for debugging this problem. I can see that the assembly that the foreach loop has gotten to is Microsoft.Practices.Unity.WebApi, so i'm guessing this is the assembly needing System.Web.Http. The thing is though, this Web App is running on about 30 different Web Apps for different customers, and neither the servers nor my colleagues computers has that assembly.

I have tried copying the assemblies from a Web App that works perfectly, but to no effect.

Chimera
  • 149
  • 1
  • 13
  • Entity uses a mapping between the c# application and the database. Either the connection to the database is not occurring in the API or something changed in the database that is causing an issue with the mapping. You can update the mapping in VS. Update Model : https://www.c-sharpcorner.com/article/create-and-update-an-edmx-file-using-entity-framework-data-model-in-visual-stud/ – jdweng Mar 27 '19 at 10:16
  • I'll take a look at the link you send, thanks. I am wondering however what makes you think it's a Entity Framework problem? – Chimera Mar 27 '19 at 10:20
  • The 1st line of code : var entitymethod = typeof(DbModelBuilder).GetMethod("Entity"); It looks like you are trying to get the database model. You are getting a model (not null) and getting into the foreach. So something is wrong with the model or the connection. – jdweng Mar 27 '19 at 10:25
  • I just tried to comment that part out, and i still get the error. What the method does is running through all assemblies through reflection and creating DbSets for Entity Framework. – Chimera Mar 27 '19 at 10:38
  • It is a property of the project so the link show a edmx file that contains the mapping. What I think is happening is Entity is connecting to database using WebApi and mapping to the database. What database are you using? Is it SQL Server? If the Server version changed then the Entity mapping need to get updated. – jdweng Mar 27 '19 at 10:57
  • It's SQL Server hosted on Azure. However nothing changed, and all other prod systems works – Chimera Mar 27 '19 at 11:48
  • It wasn't clear if each PC connected to the same SQL Server or different Servers. And if PCs were connected to same database of different databases because a server can have more than one database – jdweng Mar 27 '19 at 12:14
  • Each Web App has different SQL databases but the same server. – Chimera Mar 27 '19 at 12:18

3 Answers3

0

Property window use copy local True Property , faced the same Issue and fixed

Junaid
  • 31
  • 3
  • Problem is i don't have this assembly anywhere. Not in any production code that works or on my colleagues' computers. – Chimera Mar 27 '19 at 10:40
  • I faced the same scenario recently when we install .NET it is installing the corresponding dll in GAC folder normally C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\ , so it will work in you machine and your Colleges computer , but when goes to production it will not work , so you can Add this reference in your project by Add Reference -> search for System.Web.Http then make sure Copy Local True on Property it will work – Junaid Mar 27 '19 at 11:07
  • I searched the entirety of the folder, and the assembly isn't there, neither is it on my colleagues computers. – Chimera Mar 27 '19 at 11:44
0

You can Add System.Web.Http suing Add reference Add Reference then make sure that

Copy Local True note you colleague machine .NET was installed so this DLL will Pick from GAC Folder see more details for GAC enter link description here

Junaid
  • 31
  • 3
  • System.Web.Http is not available when i try to add a reference. Is the problem that for some reason the file has gone missing perhaps? – Chimera Mar 27 '19 at 11:40
0

GIT (assuming you're using GIT) usually ignores .dll files and .exe files. Chances are that the dll file for the System.Web.Http assembly was never added to GIT and was always on your drive. Now that you've changed branch, its checked out everything on GIT and removed what was on your drive.

I did find a System.Web.Http assembly in 'Microsoft.AspNet.WebApi.Core' To add this:

  1. Right Click the Project in Solution Explorer
  2. Manage NuGet Packages...
  3. Click on Browse
  4. Type 'Microsoft.AspNet.WebApi.Core' into the search
  5. Install the 'Microsoft.AspNet.WebApi.Core' package

You might want to remove .dll from .gitignore if this is important; as this kind of thing has happened to me also as Visual Studio usually only puts dlls in to the bin folder.

pm101
  • 1,309
  • 10
  • 30
  • I tried installing this, but i guess it's a different one. Atleast it didn't help – Chimera Mar 27 '19 at 11:46
  • Any errors when using this DLL? It might help you find the correct one as it might be downloadable somewhere – pm101 Mar 27 '19 at 11:47
  • I found this: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Core/ package, that you may require. I've updated the answer to reflect this You should remove the one that doesn't work in the Manage NuGET packages. – pm101 Mar 27 '19 at 11:48
  • I tried installing this, but it didn't help. It does have Core at the end, so isn't it for .Net Core? – Chimera Mar 27 '19 at 11:56
  • Not sure as I don't normally do ASP web apps -- Perhaps the wrong version? You can change version on the NuGet download (referring to this post on SO https://stackoverflow.com/questions/20202188/where-can-i-find-a-nuget-package-for-upgrading-to-system-web-http-v5-0-0-0 – pm101 Mar 27 '19 at 12:09
  • did you try re-checking out the branch and checking the .csproj file for the exact package name? – pm101 Mar 27 '19 at 12:22
  • My projects aren't referencing the assembly directly, it seems like it's a dependency of the Unity package. This problem is on all my branches. – Chimera Mar 27 '19 at 12:48