作为 StackOverflow 的长期读者,但在这里没有找到解决我的问题的方法是我第一次尝试提出问题,所以不要对我太苛刻 :-)
我有以下 WCF 4 REST 服务定义:
服务合同
namespace RestService2.Service
{
[ServiceContract]
public interface ISampleService
{
[WebGet(UriTemplate = "")]
List<SampleItem> GetCollection();
[WebInvoke(UriTemplate = "", Method = "POST")]
SampleItem Create(SampleItem instance);
[WebGet(UriTemplate = "?id={id}")]
SampleItem Get(int id);
[WebInvoke(UriTemplate = "?id={id}", Method = "PUT")]
SampleItem Update(int id, SampleItem instance);
[WebInvoke(UriTemplate = "?id={id}", Method = "DELETE")]
void Delete(int id);
}
}
服务实施
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class SampleService : ISampleService
{
private IDatabase db;
public SampleService(IDatabase db)
{
this.db = db;
}
public SampleService()
{
}
public List<SampleItem> GetCollection()
{
return db.Items.Values.ToList();
}
public SampleItem Create(SampleItem instance)
{
// Add the new instance of SampleItem to the collection
db.Items.Add(instance.Id, instance);
return db.Items[instance.Id];
}
//..Rest omitted..
}
数据库接口:
using RestService2.Entities;
namespace RestService2.Service
{
public interface IDatabase
{
Dictionary<int, SampleItem> Items { get; }
}
}
数据库实现:
public class Database : IDatabase
{
private Dictionary<int, SampleItem> items;
public Database()
{
}
public Dictionary<int, SampleItem> Items
{
get
{
return items;
}
}
}
..和 global.asax 文件
namespace RestService2.Web
{
public class Global : HttpApplication
{
static IWindsorContainer Container {get; set;}
void Application_Start(object sender, EventArgs e)
{
BuildContainer();
RegisterRoutes();
}
private void BuildContainer()
{
Container = new WindsorContainer();
Container.AddFacility<WcfFacility>()
.Register(Component.For<ISampleService>().ImplementedBy<SampleService>().Named("SampleService"))
.Register(Component.For<IDatabase>().ImplementedBy<Database>());
}
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("SampleService",
new DefaultServiceHostFactory(), typeof(SampleService)));
}
}
}
服务契约、服务实现、数据库接口和数据库实现在程序集 A 中,SampleItem(一个实体)在程序集 B 中定义,global.asax.cs 在程序集 C 中。
我已将程序集 A 和 B 的引用添加到程序集 C。
当我尝试访问服务帮助页面(或与此相关的任何服务方法)时,我收到以下错误消息:Could not find a component with type RestService2.Service.SampleService, RestService2.Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, did you forget to register it?
知道可能是什么问题吗?我应该如何正确配置容器?
问候