WCF和通过Castle.Windsor注入依赖
本文关键字:Windsor 注入 依赖 Castle WCF | 更新日期: 2023-09-27 18:06:34
我用Castle。Windsor作为IoC容器,并尝试像下面描述的那样注册依赖项:http://blog.ploeh.dk/CommentView,guid,f1a71969-0584-4a15-9395-9f2ac65f104b.aspx#commentstart我编写了下面的代码:
public class RiverdaleServiceHostfactory : DefaultServiceHostFactory
{
public RiverdaleServiceHostfactory()
: base(CreateKernel())
{
}
private static IKernel CreateKernel()
{
InversionOfControl.RegisterAll();
InversionOfControl.Container.AddFacility<WcfFacility>();
return InversionOfControl.Container.Kernel;
}
}
它给了我一个关于数据合同的错误合同名称'Riverdale.Api.DataContracts。在服务"CustomerSearchService"实现的契约列表中找不到CustomerInfoType。我检查了属性,配置,一切都配置成应该的样子。看来自从那篇文章发表以来,图书馆已经发生了很大的变化,并且知道这不是应该走的路。
更重要的是,我已经下载了3.0版本的WCF工具,其中的演示不能在我的PC本地工作,说:
无法加载类型' castle . functions . wcfinintegration . demo . global '.
最好的做法是什么?我错过了什么?
new WindsorContainer()
.AddFacility<WcfFacility>()
.Register(
Component.For<IServiceBehavior>().Instance(metadata),
Component.For<IServiceBehavior>().Instance(debug),
Component
.For<IService1>()
.ImplementedBy<Service1>()
.Interceptors(Castle.Core.InterceptorReference.ForType<ServiceInterceptor>()).Anywhere
.Named("service1")
.LifeStyle.Transient
.AsWcfService(new DefaultServiceModel().Hosted()
.AddEndpoints(
WcfEndpoint.BoundTo(new BasicHttpBinding()),
WcfEndpoint.BoundTo(new WSHttpBinding(SecurityMode.None)).At("ws")
))
);
}
Service1。SVC文件
<%@ ServiceHost Language="C#" Debug="true" Service="service1"
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory,
Castle.Facilities.WcfIntegration" %>
来自样本的问题位于bin/output配置的垃圾中。使用Castle windsor WCF设施的3.0库的方法是在Global中编写下一个代码。Asax:
using System;
using System.ServiceModel.Description;
using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Installer;
namespace Riverdale.Web
{
public class Global : System.Web.HttpApplication
{
private static IWindsorContainer _container;
protected void Application_Start(object sender, EventArgs e)
{
var returnFaults = new ServiceDebugBehavior
{
IncludeExceptionDetailInFaults = true,
HttpHelpPageEnabled = true
};
var metadata = new ServiceMetadataBehavior { HttpGetEnabled = true };
InversionOfControl.RegisterAll();
InversionOfControl.Container
.AddFacility<WcfFacility>()
.Install(Configuration.FromXmlFile("SearchCustomerWindsorConfig.xml"))
.Register(
Component.For<IServiceBehavior>().Instance(returnFaults),
Component.For<IServiceBehavior>().Instance(metadata));
_container = InversionOfControl.Container;
}
protected void Application_End(object sender, EventArgs e)
{
if (_container != null)
{
_container.Dispose();
}
}
}
}
配置xml文件应该包含如下内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<components>
<component id="CustomerSearchService"
service="Riverdale.Api.ICustomerSearchService, Riverdale.Api"
type="Riverdale.Api.CustomerSearchService, Riverdale.Api">
</component>
</components>
</configuration>