在控制台应用程序中使用Service Bus和Castle Windsor的WCF Azure托管客户端

时间:2016-08-05 15:31:00

标签: c# wcf azure castle-windsor azureservicebus

您好我正尝试使用Castle Windsor在控制台应用中使用以下示例在Azure中托管WCF服务来使用依赖注入工作:

https://azure.microsoft.com/en-gb/documentation/articles/service-bus-dotnet-how-to-use-relay/

我的WCF服务使用Castle Windsor(小样本)正常工作:

 public class PersonService : IPersonService
    {
    private readonly IPersonManager _personManager;

    public PersonService(IPersonManager personManager)
    {
        _personManager = personManager;
    }

    public Message Create(Person person)
    {
        Message result = _ personManager.Create(person);
    }
}

Castle Windsor(在wcf服务Global.asax中)

private void BuildWindsorContainer ()
{
var container = new WindsorContainer();
container.Kernel.AddFacility<WcfFacility>();
container.Kernel.Register(
Component.For< IPersonManager >().ImplementedBy<PersonManager >(),
Component.For<IPersonService>()
    .ImplementedBy<PersonService>()
    .Named("WebS.PersonService"),
);
}

在我的托管客户端中,我调用了WCF服务:

private static ServiceHost CallPersonService()
{
BuildWindsorContainer ();

var sh = new ServiceHost(typeof (PersonService));

sh.AddServiceEndpoint(typeof (IPersonService), new NetTcpBinding(), "net.tcp://localhost:37050/person");

sh.AddServiceEndpoint(typeof (IPersonService), new NetTcpRelayBinding(),
ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, "person"))
.Behaviors.Add(new TransportClientEndpointBehavior
{
    TokenProvider =
        TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", Key)
});

private void BuildWindsorContainer ()
{
var container = new WindsorContainer();
container.Kernel.AddFacility<WcfFacility>();
container.Kernel.Register(
Component.For< IPersonManager >().ImplementedBy<PersonManager >(),
Component.For<IPersonService>()
.ImplementedBy<PersonService>()
.Named("WebS.PersonService"),
);
}

如何将依赖项从客户端注入服务?

var sh = new ServiceHost(typeof (PersonService));
sh.AddServiceEndpoint(typeof (IPersonService), new NetTcpBinding(), "net.tcp://localhost:37050/person");

当客户端运行时,IPersonManager在服务中为空。

1 个答案:

答案 0 :(得分:1)

尝试

var sh = (ServiceHost)new DefaultServiceHostFactory()
    .CreateServiceHost(typeof(PersonService).AssemblyQualifiedName, new Uri[] { });