TWi*_*ckz 3 spring.net azure azure-worker-roles
我在使用spring.net 4.0开发Web应用程序方面有一定的经验,nhibernate 3.0用于基于ASP.net的Web应用程序.最近我遇到了一种情况,我需要使用spring.net来注入属于WorkerRole该类的服务依赖项.我正常创建了app.config文件,正如我通常使用的spring.config文件一样.这是为了清楚起见.(我已经排除了根节点)
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web" requirePermission="false" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" requirePermission="false" />
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<!-- Application services and data access that has been previously developed and tested-->
<resource uri="assembly://DataAccess/data-access-config.xml" />
<resource uri="assembly://Services/service-config.xml" />
<resource uri="AOP.xml" />
<resource uri="DI.xml"/>
</context>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
<parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />
<parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop" />
</parsers>
</spring>
Run Code Online (Sandbox Code Playgroud)
同样这是AOP.xml
<object id="FilterServiceProxy" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop">
<property name="proxyInterfaces" value="Domain.IFilterService"/>
<property name="target" ref="FilterService"/>
<property name="interceptorNames">
<list>
<value>UnhandledExceptionThrowsAdvice</value>
<value>PerformanceLoggingAroundAdvice</value>
</list>
</property>
</object>
</objects>
Run Code Online (Sandbox Code Playgroud)
和DI.xml
<object type="FilterMt.WorkerRole, FilterMt" >
<property name="FilterMtService1" ref="FilterServiceProxy"/>
</object>
Run Code Online (Sandbox Code Playgroud)
但是,我无法将任何依赖项注入worker角色.有人可以让我知道我在这里做错了什么吗?是否有不同的方法为Windows Azure应用程序配置Spring.net DI?
我没有得到任何配置错误,但我发现没有注入依赖项,因为我尝试注入的属性对象仍然为null.
ast*_*kov 10
根据我的经验,您不能向WorkerRole类(实现RoleEntryPoint的类)注入任何内容.到目前为止我使用Unity(我还为Unity构建了自己的助手来帮助我注入Azure设置),我有自己的基础设施运行并由Unity构建,但我在工作人员的代码中创建它角色.
例如,我在RoleEntry点的OnStart()方法中初始化依赖项容器,在那里我解决了我需要的任何东西.然后在我的Run()方法中,我在已解析的依赖项上调用一个方法.
这是我的RoleEntryPoint实现的快速剥离版本:
public class WorkerRole : RoleEntryPoint
{
private UnityServiceHost _serviceHost;
private UnityContainer _container;
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("FIB.Worker entry point called", "Information");
using (this._container = new UnityContainer())
{
this._container.LoadConfiguration();
IWorker someWorker = this._container.Resolve<IWorker>();
someWorker.Start();
IWorker otherWorker = this._container.Resolve<IWorker>("otherWorker");
otherWorker.Start();
while (true)
{
// sleep 30 minutes. we don't really need to do anything here.
Thread.Sleep(1800000);
Trace.WriteLine("Working", "Information");
}
}
}
public override bool OnStart()
{
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
this.CreateServiceHost();
return base.OnStart();
}
public override void OnStop()
{
this._serviceHost.Close(TimeSpan.FromSeconds(30));
base.OnStop();
}
private void CreateServiceHost()
{
this._serviceHost = new UnityServiceHost(typeof(MyService));
var binding = new NetTcpBinding(SecurityMode.None);
RoleInstanceEndpoint externalEndPoint =
RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["ServiceEndpoint"];
string endpoint = String.Format(
"net.tcp://{0}/MyService", externalEndPoint.IPEndpoint);
this._serviceHost.AddServiceEndpoint(typeof(IMyService), binding, endpoint);
this._serviceHost.Open();
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我自己的逻辑是IWorker接口,我可以拥有尽可能多的实现,并且我在Run()方法中对它们进行了充实.我做的更多是拥有一个WCF服务,再次通过DI与Unity完全配置.这是我的IWorker界面:
public interface IWorker : IDisposable
{
void Start();
void Stop();
void DoWork();
}
Run Code Online (Sandbox Code Playgroud)
就是这样.我的WorkerRole中没有任何"硬"依赖项,只有Unity容器.在我的两个工人中,我有非常复杂的DI,一切都很好.
您不能直接干扰WorkerRole.cs类的原因是它由Windows Azure基础结构实例化,而不是由您自己的基础结构实例化.您必须接受这一点,并在WorkerRole适当的方法中构建您的基础结构.并且不要忘记你必须永远不要退出/中断/返回/退出Run()方法.这样做会标记Windows Azure基础结构,使您的代码出现问题并触发角色回收.
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
2703 次 |
| 最近记录: |