Hangfire + ASP.NET MVC - c#

This is my first time posting here.
I am currently writing an ASP.NET MVC application (using .NET 4.5.2 and MVC 5). I want to integrate Hangfire into my project. I have set up a side project which worked. But when I tried to integrate it into my main project, several errors occurred. They are:
The .dll files had to be strong-named (I fixed this issue by using ildasm/ilasm to strong name them. I replace the original files that came with the nuget package.)
After strong-naming and replacing the files, I ran into another error. When I tried to build and run the solution, a 'GlobalConfiguration' does not exists in the current context, and also a are you missing an assembly reference error is thrown.
I have tried various ways to solve this, but to no avail. I would really like some advice on this ! Thanks !

GlobalConfiguration is a class in the Hangfire namespace. You should import the namespace so that you can use it (and the extension methods it provides)
using Hangfire;
So now code like this will work:
GlobalConfiguration.Configuration.UseSqlServerStorage("MyConnectionString");

Related

C# with WPF -- Using project reference and adding to reference list does not work

In a specific project (meant for web-services) in my .net solution (Visual Studio), I have added a using directive:
using ourSystem.Common.WPF
and I also added the corresponding reference (pointing to the DLL) to the project file.
The web-services project compiles, builds, and works just fine until I try to add the WPF project.
When I add the WPF project, I get the error message:
Error CS0234 The type or namespace name 'WPF' does not exist in the namespace 'ourSystem.Common' (are you missing an assembly reference?)
I have looked at similar questions on stack overflow and web posts elsewhere, but nothing seems to work.
It appears that the problem must be some sort of incompatibility between files.
NOTE: The Visual Studio Solution (VS2019) I am working on has several projects, and this DLL/namespace works in many of the other projects, just not the project that I am trying to add it to.
Both the WPF project and the project I am trying to reference it in are v4.5.2
Thus, at least for that attribute, they should be compatible.
For what it's worth, I have also tried unloading projects, and reloading them. This often changes the error count from about 4 (all the same message) to well over 100, and it seems to lose all references to everything.
Edit added: It turns out the relative structure of the many projects within the solution (which I didn't describe in the original post of this question) was key in finding an answer. See my own answer below for details, if interested.
Any suggestions on how to fix this problem?
I was able to fix it essentially as follows:
I refactored the directory structure.
It had been
A-> Top Level GUI project that had called B & C
B-> Folder with the projects used by A including WPF, but not Web Service Project.
C-> Folder with Web Service Project
I then moved all of C to the same layer as B.
Having them in the same folder seemed to 'placate' the compiler.
I am not claiming that this was absolutely needed, nor that it should always be done, however it worked in my case.
I had to also redo some code within the C project to account for different layering within C's own folders (especially packages folder), as it still had legacy code as if it were the top-level project. The following link was useful in that: (especially the answer by Nikita R.)
How to fix re-layering problem

ConfigurationManager.ConnectionStrings[...] can't be used even after including "using System.Configuration"

I'm coding a project in C# in ASP.net MVC. I have the connection string in the Web.config file. I can't share it due to security reasons. In my code, I have the line: string CS2 = ConfigurationManager.ConnectionStrings["..."].ConnectionString; But I get an error and a red line under "ConfigurationManager" that says it does not exist in the current context.
I've checked other pages that say I need to include the reference by clicking on the project > Add > Reference > and then select the correct one. But I don't see Configuration listed there. And after browsing and searching, I still don't find it. I'm not quite sure what I can do besides try learning the Entity Framework which I'm not really a fan of doing.
EDIT: solution ended up being finding the library in NuGet, installing it, and adding an app.config file with the connectionString in it.
What version of .NET are you using? For .NET Core projects, reference the Nuget package linked below. For a full .NET project you should be able to include the System.Configuration project from references. And make sure using System.Configuration; is in place.
https://www.nuget.org/packages/System.Configuration.ConfigurationManager/

How do I make my Web API project actually see the System.Data.SqlClient NuGet package?

There seem to be a good number of questions related to this, but I haven't found a solution to the problem yet. I apologize if the answer is here and I just haven't found it.
I'm trying to create a Web API project that gives me the middle layer between an HTML page for uploading a file and SQL database that stores the files. I know it's generally a bad approach to store files in the database, but I'm trying make a smaller test app that will help me diagnose a bigger problem in an existing database that I can't change.
I can set up a controller with a path like "api/docs/save" just fine, but saving the file in the database has become something of a nightmare. I can't seem to get a reference to "System.Data" or "System.Data.SqlClient" to work in the project. I've tried the NuGet packages with no luck. I even tried just making a class library using normal C# .NET, but adding a reference to that class library doesn't work either.
How do I set up a Web API project so I can use the data base access methods I'm used to?
As a side note: I don't have access to the existing front end code for the app, so I can't just add a test page there. I need to create something I can run locally through Visual Studio.
I can replicate your problem in VS 2017 using Core 1.1 and there does seem to be a problem.
After installing the NuGet package save and reload the project and it should be fine.

Swagger.NET MVC Api Exception

I've been looking at different options for providing auto generated API Documentation and Swagger seems to be up there.
However when trying this out for the first time I'm getting exceptions on start up.
Running Visual Studio 2013.
Create new Web API project.
Using Package Manager, run:
Install-Package Swagger.Net & Install-Packager Swagger.Net.UI
Making sure we've enable the XML documentation we get this exception:
Method 'GetDocumentation' in type 'Swagger.Net.XmlCommentDocumentationProvider'
from assembly 'Swagger.Net, Version=0.5.1.0, Culture=neutral, PublicKeyToken=null'
does not have an implementation.
Does anyone else know of a step we are missing? We found & followed this:
https://github.com/Swagger-Net/Swagger.Net/wiki
https://github.com/Swagger-Net/Swagger.Net/blob/master/README.md
Swagger doesn't support .NET 4.5 - Had to get the source code out from git and modify it to work with 4.5.
Right click the project and set the target framework in there
I have made a library for use with mvc that generates the documentation for you based on attributes. It may be what your looking for.
MVC3: https://www.nuget.org/packages/SwaggerAPIDocumentation/
MVC4: https://www.nuget.org/packages/SwaggerAPIDocumentation.Mvc4/
MVC5: https://www.nuget.org/packages/SwaggerAPIDocumentation.Mvc5/
MVC5.1: https://www.nuget.org/packages/SwaggerAPIDocumentation.Mvc51/
There is dcoumentation in the repository on github just click the project site link. Hope this helps
Try using Swashbuckle.
Based on Swagger as far as I know and it worked first time for me...
https://github.com/domaindrivendev/Swashbuckle

Compile error: 'The type or namespace name 'Runtime' does not exist in the namespace 'MyAppNamespace.System' (are you missing an assembly reference?)'

I am writing a WPF client that consumes services from an ASP web service (VS2010, .net 4.0). It was working just fine until yesterday when I suddenly get the above compiler error. Double-clicking on the error takes me to Reference.cs and highlights just about anything along the lines of:
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=2)]
(The 'Runtime' object is highlighted as not being in the MyAppNameSpace).
I've tried googling this and re-adding the service reference and re-adding the assemby reference but nothing works and I'm completely stuck. Reference.cs is automatically generated and I'm still relatively new to VS2010 so I'm not sure where to go with this.
Hoping someone here can point me in the right direction...?
I had the same problem. After hours of trying to solve it I realised I had a class in my library called system. What a cock.
Your problem is that you had defined a "System" namespace somewhere that the compiler and Intellisense were finding before the real "System". The solution is to remove the offending namespace, then clean and rebuild the project.
I had the same issue with a VB project only I had no old code as it was a new service reference. I did not have any other namespace with System in the name. The solution for me was to make the following series of replacements:
VB
in Reference.vb
Replace System. with nothing
Then replace "Runtime.Serialization" with "System.Runtime.Serialization"
Lastly replace <Global.ComponentModel with <ComponentModel
C#
in Reference.cs (I didn't try this but expect it will work)
Replace System. with nothing
Then replace "Runtime.Serialization" with "System.Runtime.Serialization"
Lastly replace [Global.ComponentModel with [ComponentModel
One other detail that might lead someone to a root cause is that the service wsdl was coming from an old style asp.net web service (.asmx), here: https://api.mindbodyonline.com/0_5/ClassService.asmx?wsdl
Update
I've subsequently found that changing the target framework to 4.5.1 resolved my compilation issue. Also I had the root cause was that I had different framework versions targeted by my web project and the class library containing my service references. Once I changed them all to 4.5.1 everything was happy no changes to Reference.vb was needed.
Well, for the sake of anyone looking for an answer in future - I gave up trying to fix it and copied a backup of the project files over the existing ones and brought the changes since them in manually. That fixed the corrupt referencing issue.
I had a similar problem, but with a different solution.
My project used to be called "JobRunner", but it changed to "NGB.JobRunner". In the project settings however the default namespace and assembly name was still "JobRunner", and the Reference.cs file was being generated with this namespace.
I have no idea why it was causing problems, but when I fixed the default project namespace and assembly name, and re-added the service reference, problem solved!
I had the similar problem. It was the upper case 'T' in time which was causing the issue. I was doing RunTime but the correct was 'Runtime'.
What a time waste due to a typo!!

Categories