ASP.NET Inject Identity - asp.net

I am trying to inject UserManager and UserManager with Ninject. But it gives an error.
What is wrong with the binding?
UserStore
public class UserStoreModule : NinjectModule
{
/// <summary>
/// The load.
/// </summary>
public override void Load()
{
Bind<IUserStore<Account>>()
.To<UserStore<Account>>()
.InRequestScope().WithConstructorArgument("context", context => Kernel.Get<RovinMediaContext>());
}
}
UserManager
public class UserManagerModule : NinjectModule
{
/// <summary>
/// The load.
/// </summary>
public override void Load()
{
Kernel.Bind<UserManager<Account>>().ToSelf();
}
}
Result to the following error
Error activating string No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency string into parameter connectionString of constructor of type RovinMediaContext
1) Request for MyDbContext

Related

Is there a way to add multiple entity sets to a single controller with OData endpoints?

Is there a way to have multiple OData endpoints/functions return different types mapped to a single ODataController? The normal crud operations map to an endpoint odata/foo but if I wanted to add more methods to the controller can I define a function that returns a different type under odata/foo/foosummaries?
Could I add more entity sets that would map to the same controller class? For example if I wanted to have an endpoint under my FooController that returned FooSummaries that had the endpoint ~/odata/Foo/FooSummaries?
public void Apply(ODataModelBuilder builder, ApiVersion apiVersion, string routePrefix)
{
builder.EntitySet<FooResponse>("Foos");
builder.EntityType<FooSummaries>()
.Function(nameof(SubmissionsController.GetFooSummaries))
.ReturnsCollection<IEnumerable<FooSummaryREsponse>>();
}
Would I need to decorate the OData controller differently with ODataPrefix or ODataRoute?
public class SubmissionsController : ODataController
{
#region Private Fields
private ISubmissionManager _manager;
private ILogger _logger;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
/// <param name="manager">A SubmissionManager instance.</param>
/// <param name="logger">A Logger instance.</param>
public SubmissionsController(ISubmissionManager manager, ILogger logger)
{
_manager = manager;
_logger = logger;
}
#endregion
#region HttpGet Endpoints
/// <summary>
/// Odata get all foos
/// </summary>
/// <returns></returns>
[HttpGet()]
[Produces("application/json")]
[ProducesResponseType(200, Type = typeof(IQueryable<FooResponse>))]
[ProducesResponseType(400, Type = typeof(ProblemDetails))]
[ProducesResponseType(404, Type = typeof(ProblemDetails))]
[ProducesResponseType(500, Type = typeof(ProblemDetails))]
[EnableQuery(PageSize = 20)]
public IActionResult Get()
{
try
{
IQueryable<FooResponse> result = _manager.GetFoos();
return Ok(result);
}
catch (FooException ex)
{
return ex.GetObjectResult(this);
}
}
/// <summary>
/// Get an odata queryable foo summary
/// </summary>
/// <param name="query">The query arguments</param>
/// <returns></returns>
[HttpGet("api/v{version:apiVersion}/odata/Foos/summaries")]
[Produces("application/json")]
[ProducesResponseType(200, Type = typeof(IEnumerable<FooSummaryResponse>))]
[ProducesResponseType(400, Type = typeof(ProblemDetails))]
[ProducesResponseType(404, Type = typeof(ProblemDetails))]
[ProducesResponseType(500, Type = typeof(ProblemDetails))]
[EnableQuery()]
public IActionResult GetFooSummaries()
{
try
{
IQueryable<FooSummaryResponse> results = _manager.GetFooSummaryResponse();
results);
return Ok(results);
}
catch (FooException ex)
{
return ex.GetObjectResult(this);
}
}
}

When I query my REST service with Breeze JS, the parameter is not passed

I am working on a SPA with AngularJS and BreezeJS. The backend is implemented on ASP.NET Core MVC 6 and EF 6.1.3.
The REST service is running, but when I query the service with Breeze JS, the parameter is not passed.
E.g.
[HttpGet]
[EnableQuery]
public IQueryable<Event> Events()
{
return this._contextProvider.Context.Events;
}
I tried to call the service over the browser .../Context/Events?$filter=(Id%20eq%202). The result is still the same the filter is not apply.
What have I missed? How can I add OData to MVC6?
My Controller:
namespace FleetManagement.WebApi.Controllers
{
/// <summary>Main REST / Breeze controller for the Fleet Management module.</summary>
/// <seealso cref="Microsoft.AspNetCore.Mvc.Controller"/>
[BreezeController]
[EnableCors("AllowAll")]
[EnableQuery]
[EnableBreezeQuery]
public class ContextController : Controller
{
#region Fields.
private readonly D4GEFContextProvider<FleetManagementContext> _contextProvider;
#endregion
#region Constructors / Desctructor.
/// <summary>Initializes a new instance of the <see cref="ContextController"/> class.</summary>
public ContextController()
{
var connectionString = "Data Source=lomoofsv14\\d4g;Initial Catalog=Test_Westfalen_D4G_Custom_RandomData;Integrated Security=True;MultipleActiveResultSets=True;Application Name=FMREST";
this._contextProvider = new D4GEFContextProvider<FleetManagementContext>(connectionString);
}
#endregion
#region Public Methods.
/// <summary>Get a list of assets to a given asset filter and data profile.</summary>
/// <param name="filters">The filters for the asset types is a string of type names e.g. 'Tractor, Trailer'.</param>
/// <param name="profileUId">The profile identifier for the data filter.</param>
/// <returns>IEnumerable<Asset>.</returns>
[HttpGet]
public IQueryable<Asset> Assets(string filters, string profileUId)
{
return this._contextProvider.Context.GetAssets(filters, profileUId);
}
/// <summary>Assets the types.</summary>
/// <returns>IQueryable<System.String>.</returns>
[HttpGet]
public IQueryable<string> AssetTypes()
{
return Enum.GetNames(typeof(AssetTypes))
.AsQueryable();
}
/// <summary>Data collection "Events".</summary>
/// <returns>IQueryable<Event>.</returns>
[HttpGet]
public IQueryable<Event> Events()
{
return this._contextProvider.Context.Events;
}
/// <summary>Metadata of the EF DBContext necessary for Breeze to build the JSON objects.</summary>
/// <returns>System.String.</returns>
[HttpGet]
public string Metadata()
{
return this._contextProvider.Metadata();
}
/// <summary>Saves all changes to the database.</summary>
/// <param name="saveBundle">The save bundle.</param>
/// <returns>SaveResult.</returns>
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
this._contextProvider.BeforeSaveEntityDelegate = this.BeforeSaveEntityDelegate;
this._contextProvider.AfterSaveEntitiesDelegate = this.AfterSaveEntitiesDelegate;
return this._contextProvider.SaveChanges(saveBundle);
}
[HttpGet]
public IQueryable<Tractor> Tractors()
{
return this._contextProvider.Context.Tractors;
}
[HttpGet]
public IQueryable<Trailer> Trailers()
{
return this._contextProvider.Context.Trailers;
}
#endregion
#region Private Methods.
private void AfterSaveEntitiesDelegate(Dictionary<Type, List<EntityInfo>> saveMap, List<KeyMapping> keyMappings)
{
// TractorToEvent Assign Event to tractor
}
}
}

ASP.NET Identity DbContext confusion

A default MVC 5 App comes with this piece of code in IdentityModels.cs - this piece of code is for all the ASP.NET Identity operations for the default templates:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
If I scaffold a new controller using views with Entity Framework and create a "New data context..." in the dialog, I get this generated for me:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class AllTheOtherStuffDbContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public AllTheOtherStuffDbContext() : base("name=AllTheOtherStuffDbContext")
{
}
public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }
}
}
If I scaffold another controller + view using EF, say for instance for an Animal model, this new line would get autogenerated right under public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; } - like this:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class AllTheOtherStuffDbContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public AllTheOtherStuffDbContext() : base("name=AllTheOtherStuffDbContext")
{
}
public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }
public System.Data.Entity.DbSet<WebApplication1.Models.Animal> Animals { get; set; }
}
}
ApplicationDbContext (for all the ASP.NET Identity stuff) inherits from IdentityDbContext which in turn inherits from DbContext.
AllOtherStuffDbContext (for my own stuff) inherits from DbContext.
So my question is:
Which of these two (ApplicationDbContext and AllOtherStuffDbContext) should I use for all my other own models? Or should I just use the default autogenerated ApplicationDbContext since it shouldn't be a problem using it since it derives from the base class DbContext, or will there be some overhead? You should use only one DbContext object in your app for all your models (I've read this somewhere) so I should not even consider using both ApplicationDbContext and AllOtherStuffDbContext in a single app? Or what is best practice in MVC 5 with ASP.NET Identity?
I would use a single Context class inheriting from IdentityDbContext.
This way you can have the context be aware of any relations between your classes and the IdentityUser and Roles of the IdentityDbContext.
There is very little overhead in the IdentityDbContext, it is basically a regular DbContext with two DbSets. One for the users and one for the roles.
There is a lot of confusion about IdentityDbContext, a quick search in Stackoverflow and you'll find these questions:
"
Why is Asp.Net Identity IdentityDbContext a Black-Box?
How can I change the table names when using Visual Studio 2013 AspNet Identity?
Merge MyDbContext with IdentityDbContext"
To answer to all of these questions we need to understand that IdentityDbContext is just a class inherited from DbContext.
Let's take a look at IdentityDbContext source:
/// <summary>
/// Base class for the Entity Framework database context used for identity.
/// </summary>
/// <typeparam name="TUser">The type of user objects.</typeparam>
/// <typeparam name="TRole">The type of role objects.</typeparam>
/// <typeparam name="TKey">The type of the primary key for users and roles.</typeparam>
/// <typeparam name="TUserClaim">The type of the user claim object.</typeparam>
/// <typeparam name="TUserRole">The type of the user role object.</typeparam>
/// <typeparam name="TUserLogin">The type of the user login object.</typeparam>
/// <typeparam name="TRoleClaim">The type of the role claim object.</typeparam>
/// <typeparam name="TUserToken">The type of the user token object.</typeparam>
public abstract class IdentityDbContext<TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken> : DbContext
where TUser : IdentityUser<TKey, TUserClaim, TUserRole, TUserLogin>
where TRole : IdentityRole<TKey, TUserRole, TRoleClaim>
where TKey : IEquatable<TKey>
where TUserClaim : IdentityUserClaim<TKey>
where TUserRole : IdentityUserRole<TKey>
where TUserLogin : IdentityUserLogin<TKey>
where TRoleClaim : IdentityRoleClaim<TKey>
where TUserToken : IdentityUserToken<TKey>
{
/// <summary>
/// Initializes a new instance of <see cref="IdentityDbContext"/>.
/// </summary>
/// <param name="options">The options to be used by a <see cref="DbContext"/>.</param>
public IdentityDbContext(DbContextOptions options) : base(options)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="IdentityDbContext" /> class.
/// </summary>
protected IdentityDbContext()
{ }
/// <summary>
/// Gets or sets the <see cref="DbSet{TEntity}"/> of Users.
/// </summary>
public DbSet<TUser> Users { get; set; }
/// <summary>
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User claims.
/// </summary>
public DbSet<TUserClaim> UserClaims { get; set; }
/// <summary>
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User logins.
/// </summary>
public DbSet<TUserLogin> UserLogins { get; set; }
/// <summary>
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User roles.
/// </summary>
public DbSet<TUserRole> UserRoles { get; set; }
/// <summary>
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User tokens.
/// </summary>
public DbSet<TUserToken> UserTokens { get; set; }
/// <summary>
/// Gets or sets the <see cref="DbSet{TEntity}"/> of roles.
/// </summary>
public DbSet<TRole> Roles { get; set; }
/// <summary>
/// Gets or sets the <see cref="DbSet{TEntity}"/> of role claims.
/// </summary>
public DbSet<TRoleClaim> RoleClaims { get; set; }
/// <summary>
/// Configures the schema needed for the identity framework.
/// </summary>
/// <param name="builder">
/// The builder being used to construct the model for this context.
/// </param>
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<TUser>(b =>
{
b.HasKey(u => u.Id);
b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique();
b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex");
b.ToTable("AspNetUsers");
b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();
b.Property(u => u.UserName).HasMaxLength(256);
b.Property(u => u.NormalizedUserName).HasMaxLength(256);
b.Property(u => u.Email).HasMaxLength(256);
b.Property(u => u.NormalizedEmail).HasMaxLength(256);
b.HasMany(u => u.Claims).WithOne().HasForeignKey(uc => uc.UserId).IsRequired();
b.HasMany(u => u.Logins).WithOne().HasForeignKey(ul => ul.UserId).IsRequired();
b.HasMany(u => u.Roles).WithOne().HasForeignKey(ur => ur.UserId).IsRequired();
});
builder.Entity<TRole>(b =>
{
b.HasKey(r => r.Id);
b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex");
b.ToTable("AspNetRoles");
b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();
b.Property(u => u.Name).HasMaxLength(256);
b.Property(u => u.NormalizedName).HasMaxLength(256);
b.HasMany(r => r.Users).WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
b.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
});
builder.Entity<TUserClaim>(b =>
{
b.HasKey(uc => uc.Id);
b.ToTable("AspNetUserClaims");
});
builder.Entity<TRoleClaim>(b =>
{
b.HasKey(rc => rc.Id);
b.ToTable("AspNetRoleClaims");
});
builder.Entity<TUserRole>(b =>
{
b.HasKey(r => new { r.UserId, r.RoleId });
b.ToTable("AspNetUserRoles");
});
builder.Entity<TUserLogin>(b =>
{
b.HasKey(l => new { l.LoginProvider, l.ProviderKey });
b.ToTable("AspNetUserLogins");
});
builder.Entity<TUserToken>(b =>
{
b.HasKey(l => new { l.UserId, l.LoginProvider, l.Name });
b.ToTable("AspNetUserTokens");
});
}
}
Based on the source code if we want to merge IdentityDbContext with our DbContext we have two options:
**First Option:**
Create a DbContext which inherits from IdentityDbContext and have access to the classes.
public class ApplicationDbContext
: IdentityDbContext
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
static ApplicationDbContext()
{
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
// Add additional items here as needed
}
Extra Notes:
We can also change asp.net Identity default table names with the following solution:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(): base("DefaultConnection")
{
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("user");
modelBuilder.Entity<ApplicationUser>().ToTable("user");
modelBuilder.Entity<IdentityRole>().ToTable("role");
modelBuilder.Entity<IdentityUserRole>().ToTable("userrole");
modelBuilder.Entity<IdentityUserClaim>().ToTable("userclaim");
modelBuilder.Entity<IdentityUserLogin>().ToTable("userlogin");
}
}
Furthermore we can extend each class and add any property to classes like 'IdentityUser', 'IdentityRole', ...
public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
public ApplicationRole()
{
this.Id = Guid.NewGuid().ToString();
}
public ApplicationRole(string name)
: this()
{
this.Name = name;
}
// Add any custom Role properties/code here
}
// Must be expressed in terms of our custom types:
public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationRole,
string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
static ApplicationDbContext()
{
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
// Add additional items here as needed
}
To save time we can use AspNet Identity 2.0 Extensible Project Template to extend all the classes.
Second Option:(Not recommended)
We actually don't have to inherit from IdentityDbContext if we write all the code ourselves.
So basically we can just inherit from DbContext and implement our customized version of "OnModelCreating(ModelBuilder builder)" from the IdentityDbContext source code
This is a late entry for folks, but below is my implementation. You will also notice I stubbed-out the ability to change the the KEYs default type: the details about which can be found in the following articles:
Extending Identity Models and Using Integer Keys Instead of Strings
Change Primary Key for Users in ASP.NET Identity
NOTES:
It should be noted that you cannot use Guid's for your keys. This is because under the hood they are a Struct, and as such, have no unboxing which would allow their conversion from a generic <TKey> parameter.
THE CLASSES LOOK LIKE:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
#region <Constructors>
public ApplicationDbContext() : base(Settings.ConnectionString.Database.AdministrativeAccess)
{
}
#endregion
#region <Properties>
//public DbSet<Case> Case { get; set; }
#endregion
#region <Methods>
#region
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//modelBuilder.Configurations.Add(new ResourceConfiguration());
//modelBuilder.Configurations.Add(new OperationsToRolesConfiguration());
}
#endregion
#region
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
#endregion
#endregion
}
public class ApplicationUser : IdentityUser<string, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
#region <Constructors>
public ApplicationUser()
{
Init();
}
#endregion
#region <Properties>
[Required]
[StringLength(250)]
public string FirstName { get; set; }
[Required]
[StringLength(250)]
public string LastName { get; set; }
#endregion
#region <Methods>
#region private
private void Init()
{
Id = Guid.Empty.ToString();
}
#endregion
#region public
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, string> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
#endregion
#endregion
}
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
#region <Constructors>
public CustomUserStore(ApplicationDbContext context) : base(context)
{
}
#endregion
}
public class CustomUserRole : IdentityUserRole<string>
{
}
public class CustomUserLogin : IdentityUserLogin<string>
{
}
public class CustomUserClaim : IdentityUserClaim<string>
{
}
public class CustomRoleStore : RoleStore<CustomRole, string, CustomUserRole>
{
#region <Constructors>
public CustomRoleStore(ApplicationDbContext context) : base(context)
{
}
#endregion
}
public class CustomRole : IdentityRole<string, CustomUserRole>
{
#region <Constructors>
public CustomRole() { }
public CustomRole(string name)
{
Name = name;
}
#endregion
}
If you drill down through the abstractions of the IdentityDbContext you'll find that it looks just like your derived DbContext. The easiest route is Olav's answer, but if you want more control over what's getting created and a little less dependency on the Identity packages have a look at my question and answer here. There's a code example if you follow the link, but in summary you just add the required DbSets to your own DbContext subclass.

dynamic load recive activity wf

I'm trying to load and invoke activityes from custom activity as follows:
Imagine I have a xamlx like this:
--Sequence
|----- LoadActiviy
|--Initialize dictionary with input data of activitity
|----- Invoke
This works when activity NOT CONTAINS receive/send messages. But when i try with activity wich contains receive/send messages the result is a exception
WorkflowApplicationUnhandledExceptionEventArgs: Only registered bookmark scopes can be used for creating scoped bookmarks.
The code:
1-Load xaml: (Load activity)
public sealed class LoadActivity : CodeActivity<Activity>
{
#region Properties
/// <summary>
/// Gets or sets Path.
/// </summary>
[RequiredArgument]
public InArgument<string> Path { get; set; }
#endregion
#region Methods
/// <summary>
/// The execute method.
/// </summary>
/// <param name="context">
/// The context.
/// </param>
/// <returns>
/// An activity loaded from a file
/// </returns>
protected override Activity Execute(CodeActivityContext context)
{
return ActivityXamlServices.Load(this.Path.Get(context));
}
#endregion
}
2- Run activity:
public class SynchronousSynchronizationContext : SynchronizationContext
{
public override void Post(SendOrPostCallback d, object state)
{
d(state);
}
}
public sealed class Invoke : CodeActivity
{
#region Properties
/// <summary>
/// Gets or sets Activity.
/// </summary>
/// <remarks>
/// The activity that will be invoked. Can be loaded from XAML.
/// </remarks>
[RequiredArgument]
public InArgument<Activity> Activity { get; set; }
public OutArgument<IDictionary<string, object>> Output { get; set; }
/// <summary>
/// Gets or sets Input.
/// </summary>
/// <remarks>
/// The input arguments you want to pass to the other workflow
/// </remarks>
public InArgument<IDictionary<string, object>> Input { get; set; }
#endregion
// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override void Execute(CodeActivityContext context)
{
try
{
IDictionary<string,object> _input= this.Input.Get(context);
foreach (KeyValuePair<string,object> item in _input )
{
Debug.WriteLine(string.Format("{0} {1}", item.Key, item.Value));
}
// AutoResetEvent idleEvent = new AutoResetEvent(false);
WorkflowApplication app = new WorkflowApplication(this.Activity.Get(context),this.Input.Get(context));
app.SynchronizationContext = new SynchronousSynchronizationContext();
app.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
// idleEvent.Set();
};
app.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e)
{
// Display the unhandled exception.
Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}",
e.InstanceId, e.UnhandledException.Message);
Console.WriteLine("ExceptionSource: {0} - {1}",
e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId);
// Instruct the runtime to terminate the workflow.
// Other choices are Abort and Cancel. Terminate
// is the default if no OnUnhandledException handler
// is present.
return UnhandledExceptionAction.Terminate;
};
app.Idle = e => Console.WriteLine("WorkflowApplication.Idle called");
Console.WriteLine("Before WorkflowApplication.Run()");
app.Run();
}
catch
{
throw;
}
}
}
Any ideas?
You can only use a Receive activity in a workflow hosted in a WorkflowServiceHost. Even if your main workflow is hosted in a WorkflowServiceHost the child workflow is hosted in a WorkflowApplication and can't contain a Receive activity because it isn't running as part of the WCF infrastructure.

ASP.NET MVC - Using Ninject bindings outside of controllers

I'm using ASP.NET MVC3, and Ninject. I've set up the standard code implementation in "AppStart_NinjectMVC3.cs" that sets up the bindings and adds a kernel to the DependencyResolver like this:
public static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUserRepository>().To<UserRepository>();
...
}
public static void Start() {
IKernel kernel = new StandardKernel();
RegisterServices(kernel);
DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
}
All is working well in my controllers - dependencies are being resolved fine.
I'd like to be able to use Ninject and these bindings outside of controllers, and outside of the MVC stack. For example, I have a bunch of regular aspx pages in which I'd like to use my ninject kernel, and some code hanging off global.asax too.
Can I re-use my Ninject kernel in these other places, or do I need to also register a kernel in my Global.asax appstart?
The current development release found on http://teamcity.codebetter.com provides support for side a side usage of ordinary aspx pages, mvc and wcf. You might want to have a look at this.
Be aware this is a development version and it is not tested very well. Nevertheless, I think it should be pretty much stable. But as it is work in progress it the interface can change. Also I won't give a lot of support before I have written the Ninject 2.4 preview blog about this change.
You need
Ninject
Ninject.Web.Common
Ninject.Web
Ninject.Web.MVC3
I've used the Ninject MVC Extension within my ASP.NET MVC application.
Here is the manner in which I've achieved what I think you're trying to accomplish.
Global.asax.cs:
public class MvcApplication : NinjectHttpApplication
{
/// <summary>
/// Overridden Ninject method that is called once the application has started and is initialized
/// </summary>
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
// Tell the MVC Framework to use our implementation of metadataprovider.
ModelMetadataProviders.Current = new XXX.myNamespace.MetadataProvider();
// Tell the MVC Framework to use our CartModelBinder class
ModelBinders.Binders.Add(typeof(Cart), new CartModelBinder());
}
/// <summary>
/// Establish a reference to our DIFactory object
/// <remarks>
/// This application currently uses Ninject for dependency injection.
/// </remarks>
/// </summary>
/// <returns></returns>
protected override IKernel CreateKernel()
{
return DIFactory.GetNinjectFactory();
}
// snip... additional global.asax.cs methods
}
DIFactory.cs:
/// <summary>
/// This class is used as a container for dependency injection throughout the entire application
/// </summary>
public class DIFactory
{
public static IKernel _kernel = null;
/// <summary>
/// Method used to create a single instance of Ninject's IKernel
/// </summary>
/// <returns>IKernel</returns>
public static IKernel GetNinjectFactory()
{
if (_kernel == null)
{
var modules = new INinjectModule[]
{
new ServiceModule()
};
_kernel = new StandardKernel(modules);
}
return _kernel;
}
/// <summary>
/// Method used as a service locator for the IConfiguration interface
/// </summary>
/// <returns></returns>
public static IConfiguration CreateConfigurationType()
{
return _kernel.Get<IConfiguration>();
}
// snip....additional public static methods for all other Interafaces necessary
}
ServiceModule.cs:
/// <summary>
/// Configures how abstract service types are mapped to concrete implementations
/// </summary>
internal class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IConfiguration>().To<XXX.myNamespace.Configuration>();
// snip... all other bindings to interfaces
}
}
Use in other classes besides Controllers:
UserInteraction.cs:
public class UserInteraction : IUserInteraction
{
private IConfiguration configuration;
public bool SubmitFeedback(Feedback feedback)
{
try
{
this.configuration = DIFactory.CreateConfigurationType();
// snip additional logic...
}
catch(Exception ex)
{
// snip
}
}
}

Resources