SignalR 2.0 The namespace '' already contains a definition for 'Startup' - asp.net-mvc-4

I tried to follow a tutorial on SignalR 2.0. with ASP.NET MVC 4.0
I build the new Startup class as instructed.
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(MyApp.Startup))]
namespace MyApp
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
app.MapSignalR();
}
}
}
When I tried to build the project. I got The namespace 'MyApp' already contains a definition for 'Startup' .
I have search the whole project and physical folders to find where the second Startup,cs is but I could not find it.
Can anyone share some light on this?
Thanks,

I found the duplicate. It was defined in BundleConfig.cs. Removed it from BundleConfig.cs and it builds fine. Thanks,

Try making your Startup class partial:
public partial class Startup

Related

Build error when using [FromService] decorators in controller with Functions v3

I have created a new projected, where I am trying to use [FromServices] with Microsoft.NET.Sdk.Functions" Version="3.0.13" and "Microsoft.Azure.Functions.Extensions" Version="1.1.0".
There are some tickets opened on this subject. For example this one.
I haven't seen any response/solution from Microsoft.
Is there an incompatibility between libs, what is the combination that I should use?
If this method injection doesn't work, can you please tell me what other alternatives do I have?
Startup.cs
using Azure.Storage.Blobs;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
[assembly: FunctionsStartup(typeof(Startup))]
namespace SubscriptionManager.Functions
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient<IAccountService, AccountService>();
builder.Services.AddScoped<ISubscriptionService, SubscriptionService>();
}
}
}
Thank you!
The .Net version comaptible with "Microsoft.Azure.Functions.Extensions" Version="1.1.0" is .NETStandard 2.0.
Please refer the Document.

DotNet Core Dependency Injection Multiple Projects

I am building a Blazor WASM application.
The application is working but I am now looking to split the project into sensible self contained projects but having a problem working out how to implement the dependency injection without creating a circular dependency between projects.
Projects:
App.Client - UI Razor Pages
App.Server - Main project, controllers, defines interfaces
App.Shared - Shared models between Client & Server
App.Data - Implements repositories, unit of work, Db Context, migrations
The problem I am having is that the App.Data project has a dependency on the App.Server project to implement the interfaces it requires, but then I am not sure how to configure the dependencies in the startup.cs file in the App.Server project as this cannot have a dependency on the App.Data project.
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Test.Models;
using Test.Models.Data;
namespace Test
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("database"), b => b.MigrationsAssembly("Test")));
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IAntiforgery antiforgery)
{
...
}
}
}
Project structure
I ended up combining the two comments to implement a Clean Architecture structure modified for the Blazor WASM application.
Adding a Core project, defining the interfaces, and Infrastructure project implementing infrastructure interfaces such as the database. Server then has dependencies on these 2 projects which makes sense.
Startup then configures the interfaces via DI, using the appropriate implementations either in Core or Infrastructure.

How to get ConnectionString in a ClassLibrary?

Connection strings exist in appsettings.json in an ASP.Net MVC Core Project. I also have a Class Library Project in the same solution and there I want to get the connection string of web project, I am unable to find help in official resources, how can I achieve this?
Update: The class library is of .Net 4.6.1, also in the ASP.Net Core Project I am targeting .Net 4.6.1.
Register Configuration in Startup.cs
public void ConfigureServices(IServicesCollection services)
{
services.AddSingleton(Configuration);
}
Then you can inject it in controller or any other library project class
public HomeController(IConfigurationRoot Configuration)
{
this.Configuration= Configuration;
}
Then you can get connection string as you get in Startup.cs
Configuration.GetConnectionString("DefaultConnection")

Develop WebAPI using mono

Is there any guide or documentation to build a REST API using Mono console or MonoDevelop. I tried to create MVC application in MonoDevelop however couldn't find App_Start/WebApiConfig.cs or relevant files by which I can define routes and other settings which I usually do in Visual studio based application.
Short answer is there is no template in MD for this, however it's very easy to get going:
Create a new ASP.NET Project (the bottom project in the MD ASP.NET project templates).
Use NuGet to add ASP.NET Web API 2.2.
Add an App_Start folder manually, and a WebApiConfig.cs with your routes etc.
Something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace MyWebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// TODO: Add any additional configuration code.
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Add WebApiConfig configuration into the Global.asax.cs
For example:
protected void Application_Start (Object sender, EventArgs e)
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
Then start adding your models and controllers etc. as usual.
Hope this help, M.
I followed the instructions given in #muszeo's answer and it worked.
I've created a sample project at https://github.com/sashoalm/HelloWebApi. It defines a single controller called HelloWebApiController, which creates an endpoint at http://localhost:8080/api/HelloWebApi that returns a string with "hello, world".
I've tested it on Linux with MonoDevelop 5.10.
You can clone it using git clone https://github.com/sashoalm/HelloWebApi.git
The best place to start is the official ASP.NET documentation: https://docs.asp.net/en/latest/getting-started/installing-on-linux.html
I haven't used MonoDevelop but I don't think it supports dnx based apps. To generate the app skeleton you can use yo https://github.com/OmniSharp/generator-aspnet
Then you can use VS Code to edit your code https://code.visualstudio.com/

Use Ninject in both main and referenced projects

I have MVC4 website project and WCF project, both using Ninject.
I want to use class from WCF project in website project. I add reference to project and get both NinjectWebCommon.Start() executing (with "The static container already has a kernel associated with it!" error).
Is there way to make what I want?
Solved this using this startup in referenced project
public class Global : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
return new StandardKernel(new ServiceModule());
}
}