我开发了一个使用 Agatha RRSL 的 WCF 服务库,但我不知道如何初始化容器。如果我在 ASP.NET Web 应用程序中重新创建此服务,我可以从 Global.asax.cs Application_Start() 调用初始化代码,并且一切正常。初始化代码为:
public static class ComponentRegistration
{
public static void Register()
{
new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(),
typeof(HelloWorldRequest).Assembly,
typeof(Agatha.Castle.Container)).Initialize();
}
}
在 WCF 服务库中,我添加了一个 App_Code 文件夹,其中包含一个调用:
public static void AppInitialize()
{
ComponentRegistration.Register();
}
这不起作用,因为我的客户端应用程序抛出了一个异常,即该类型没有响应。我还尝试向 web.config 文件添加一个组件,但我从来没有接近工作。
我还尝试创建一个执行初始化的自定义 ServiceHost:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Agatha.ServiceLayer;
using System.Reflection;
using Sample.Common.RequestsAndResponses;
namespace Sample.ServiceLayer.WCFHost
{
public class CustomServiceHostFactory : ServiceHostFactory
{
public CustomServiceHostFactory()
{
new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(), typeof(HelloWorldRequest).Assembly,
typeof(Agatha.Castle.Container)).Initialize();
}
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new CustomServiceHost(serviceType, baseAddresses);
}
}
public class CustomServiceHost : ServiceHost
{
public CustomServiceHost()
{
}
public CustomServiceHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
}
protected override void OnOpening()
{
base.OnOpening();
}
protected override void OnClosing()
{
base.OnClosing();
}
protected override void ApplyConfiguration()
{
base.ApplyConfiguration();
}
}
}
但是,我的客户遇到了同样的异常:
System.InvalidOperationException was unhandled
Message=There is no response with type Sample.Common.RequestsAndResponses.HelloWorldResponse. Maybe you called Clear before or forgot to add appropriate request first.
Source=Agatha.Common
StackTrace:
at Agatha.Common.RequestDispatcher.Get[TResponse]() in c:\src\Agatha\Agatha.Common\RequestDispatcher.cs:line 125
at Agatha.Common.RequestDispatcher.Get[TResponse](Request request) in c:\src\Agatha\Agatha.Common\RequestDispatcher.cs:line 150
at ConsoleApplication1.Program.Main(String[] args) in C:\Users\ultraviolet\Documents\Visual Studio 2010\Projects\AgathaHelloWorld\ConsoleApplication1\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
我应该采取什么方法让 WCF 服务库运行我的初始化代码,以便主机返回正确的类型?任何指导将不胜感激。谢谢。