Visual Studio - Snapshot of Project Structure
Global.asax.cs文件,其中我已放置依赖项并注册webAPI控制器
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using StructureMap;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using GeoLocationApi.Services;
using GeoLocationApi.Controllers;
using GeoLocationApi.Models;
using System.Reflection;
namespace GeoLocationApi
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var builder = new ContainerBuilder();
builder.RegisterType<locationService>().InstancePerRequest();
builder.RegisterApiControllers(typeof(locationController).Assembly);
builder.RegisterType<locationContext>().As<locationContext>().InstancePerRequest();
builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
}
}
}
locationService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GeoLocationApi.Models;
using System.Linq.Expressions;
namespace GeoLocationApi.Services
{
public partial class locationService : IlocationService
{
private readonly IRepository<location> _locationRepository;
public locationService(IRepository<location> locationRepository)
{
this._locationRepository = locationRepository;
}
public virtual IQueryable<location> GetAllLocations(params Expression<Func<location, object>>[] includeProperties)
{
return _locationRepository.Table.IncludeProperties(includeProperties);
}
}
}
locationController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using GeoLocationApi.Models;
using GeoLocationApi.Services;
using GeoLocationApi.Infrastructure;
namespace GeoLocationApi.Controllers
{
[RoutePrefix("api")]
public class locationController : ApiController
{
private readonly IlocationService _locationService;
public locationController(IlocationService locationService)
{
this._locationService = locationService;
}
[Route("getlocation")]
[HttpGet]
public locationDTO GetLocationDetails()
{
var data = _locationService.GetAllLocations().Where(x => x.locationActiveStatus != false);
return AutoMapperConfiguration.Mapper.Map<locationDTO>(data);
}
}
}
IRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GeoLocationApi.Models;
namespace GeoLocationApi.Services
{
public partial interface IRepository<T> where T : BaseEntity
{
/// <summary>
/// Get entity by identifier
/// </summary>
/// <param name="id">Identifier</param>
/// <returns>Entity</returns>
T GetById(object id);
/// <summary>
/// Insert entity
/// </summary>
/// <param name="entity">Entity</param>
void Insert(T entity);
/// <summary>
/// Insert entities
/// </summary>
/// <param name="entities">Entities</param>
void Insert(IEnumerable<T> entities);
/// <summary>
/// Update entity
/// </summary>
/// <param name="entity">Entity</param>
void Update(T entity);
/// <summary>
/// Update entities
/// </summary>
/// <param name="entities">Entities</param>
void Update(IEnumerable<T> entities);
/// <summary>
/// Delete entity
/// </summary>
/// <param name="entity">Entity</param>
void Delete(T entity);
/// <summary>
/// Delete entities
/// </summary>
/// <param name="entities">Entities</param>
void Delete(IEnumerable<T> entities);
/// <summary>
/// Gets a table
/// </summary>
IQueryable<T> Table { get; }
/// <summary>
/// Gets a table with "no tracking" enabled (EF feature) Use it only when you load record(s) only for read-only operations
/// </summary>
IQueryable<T> TableNoTracking { get; }
}
}
locationContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace GeoLocationApi.Models
{
public class locationContext : DbContext
{
public DbSet<location> Location { get; set; }
}
}
location.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace GeoLocationApi.Models
{
public class location : BaseEntity
{
public string locationName { get; set; }
public string locationDescription { get; set; }
public string locationLongitude { get; set; }
public string locationLatitutde { get; set; }
public bool locationActiveStatus { get; set; }
}
}
当我运行项目时,我在浏览器选项卡中收到有关构造函数的错误。
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
An error occurred when trying to create a controller of type 'locationController'. Make sure that the controller has a parameterless public constructor.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = locationController (ReflectionActivator), Services = [GeoLocationApi.Controllers.locationController], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'GeoLocationApi.Controllers.locationController' can be invoked with the available services and parameters: Cannot resolve parameter 'GeoLocationApi.Services.IlocationService locationService' of constructor 'Void .ctor(GeoLocationApi.Services.IlocationService)'. (See inner exception for details.)
</ExceptionMessage>
<ExceptionType>Autofac.Core.DependencyResolutionException</ExceptionType>
<StackTrace>
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters) at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType) at Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'GeoLocationApi.Controllers.locationController' can be invoked with the available services and parameters: Cannot resolve parameter 'GeoLocationApi.Services.IlocationService locationService' of constructor 'Void .ctor(GeoLocationApi.Services.IlocationService)'.
</ExceptionMessage>
<ExceptionType>Autofac.Core.DependencyResolutionException</ExceptionType>
<StackTrace>
at Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(IComponentContext context, IEnumerable`1 parameters) at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
</StackTrace>
</InnerException>
</InnerException>
</Error>
由于我是Stackoverflow和ASP.NET WEB API的新手,我需要您解释一下这个问题并告诉我它为什么会出现这个错误。
提前致谢,感谢您的帮助。
答案 0 :(得分:0)
我认为你错误地注册了navigator.geolocation.getCurrentPosition(
function (pos) {
var myLatLng = new google.maps.LatLng(parseFloat(pos.coords.latitude), parseFloat(pos.coords.longitude));
this.setCurrentLocationMarker(myLatLng);
map.setCenter(myLatLng);
},
function (error) { return false; });
setCurrentLocationMarker(latLng) {
this.userLat = latLng.lat();
this.userLng = latLng.lng();
this.$scope.$apply();
}
,它应该映射到界面,而不是实际类
locationService