Using .NET 4.5.2, Visual studio 2017, C# 7.1, Unity, NServiceBus 6.
I receive the following error:
My application is a console app, here's some of the Program.cs code:
private static async Task ConfigureUnity()
{
IUnityContainer container = new UnityContainer();
var endpointConfiguration = new EndpointConfiguration("NSB.ChannelAdvisorService");
var transport = endpointConfiguration.UseTransport<LearningTransport>();
endpointConfiguration.AssemblyScanner().ExcludeAssemblies("netstandard");
endpointConfiguration.UseContainer<UnityBuilder>(
customizations =>
{
customizations.UseExistingContainer(container);
});
var endpointInstance = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
//register
container.RegisterType(typeof(IGenericHttpRequestRepository<>), typeof(GenericHttpRequestRepository<>), new TransientLifetimeManager());
container.RegisterType<IOrderRepository, OrderRepository>();
container.RegisterType<IShipmentRepository, ShipmentRepository>();
container.RegisterType<IOrderProcessService, OrderProcessService>();
container.RegisterType<IShipmentService, ShipmentService>();
container.RegisterInstance(endpointConfiguration);
//resolve
var orderProcessService = container.Resolve<IOrderProcessService>();
var shipmentService = container.Resolve<IShipmentService>();
.....
As you can see I'm using Unity and NServiceBus, this is to register DI and also use it withing NServicebus so i can DI it into my service to send a command.
The service trys to DI "IEndpointInstance"
public class OrderProcessService : IOrderProcessService
{
private static Logger logger = LogManager.GetCurrentClassLogger();
private readonly IEndpointInstance _endpoint;
public OrderProcessService(IEndpointInstance endpoint)
{
_endpoint = endpoint;
}
public async Task PostNewOrderBatch()
{
var list = _orderRepository.GetBatchedOrders();
foreach(var item in list)// parallel this?
{
await _endpoint.Send(item.ToObject<ProcessBatchOrdersCommand>()).ConfigureAwait(false);
_orderRepository.DeleteFile(item.Property("FilePath").Value.ToString());
}
}
}
I get the feeling it could be an issue about the order of things, I don't think I've missed anything out as far as i can tell in some examples?
In NServiceBus v6 and later the endpoint instance is no longer automatically registered in the container. You need to register the endpoint instance returned from Endpoint.Start(configuration) on the existing container.
See https://docs.particular.net/nservicebus/dependency-injection/#using-an-existing-instance-endpoint-resolution
Related
All the code samples I've seen so far for Azure WebJobs rely on some kind of trigger (e.g. TimerTrigger or QueueTrigger).
I am looking specifically at WebJobs SDK 3.x, by the way.
So. For a triggerless WebJob (Windows Service-alike one), am I expected to use NoAutomaticTrigger and find a way to kickoff my "main" code manually?
Or should I resort to implementing and registering a class that implements the IHostedService interface?
So far that's the approach I'm taking but it feels more of a hack than a recommended way.
I have not even tried to deploy this code and only ran it on my local machine, so I am afraid that the publishing process will confirm my code is not suitable for Azure WebJobs in its current form.
EntryPoint.cs
This is how the application is being bootstrap when the process is starting.
using Microsoft.Azure.ServiceBus;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace AbcCorp.Jobs
{
public static class Program
{
static async Task Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", false)
.Build();
var hostBuilder = new HostBuilder()
.ConfigureWebJobs(builder => { builder.AddAzureStorageCoreServices(); })
.ConfigureServices(serviceCollection =>
{
ConfigureServices(serviceCollection, config);
serviceCollection.AddHostedService<ConsoleApplication>();
});
using (var host = hostBuilder.Build())
await host.RunAsync();
}
private static IServiceCollection ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
{
services.AddTransient<ConsoleApplication>();
// ... more DI registrations
return services;
}
}
}
ConsoleApplication.cs
This would normally be implemented as a function with a trigger.
The thing is, I want this code to only run once on the process startup.
It will start listening on the service bus events using the regular Microsoft.Azure.ServiceBus SDK package.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using AbcCorp.Internal.Microsoft.Azure.ServiceBus;
using AbcCorp.Api.Messaging;
namespace AbcCorp.Jobs
{
public sealed class ConsoleApplication: IHostedService
{
private readonly IReceiver<SubmissionNotification> _messageReceiver;
private readonly MessageHandler _messageHandler;
public ConsoleApplication(IReceiver<SubmissionNotification> messageReceiver, MessageHandler messageHandler)
{
_messageReceiver = messageReceiver;
_messageHandler = messageHandler;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_messageReceiver.StartListening(_messageHandler.HandleMessage, _messageHandler.HandleException);
return Task.Delay(Timeout.Infinite);
}
public Task StopAsync(CancellationToken cancellationToken)
{
_messageReceiver.Dispose();
return Task.CompletedTask;
}
}
}
So you want a console application to run in a WebJob and listen to messages. You don't really care about WebJob magic like triggers, it's just a place to run your console app. I've done the exact same thing before.
I found the IHostedService abstraction to be very helpful, but I didn't like their SDK. I found it bloated and hard to use. I didn't want to take a large dependency in order use a large array of special magic Azure stuff, when all I wanted to do was run a console application in a WebJob for now, and maybe move it elsewhere later.
So I ended just deleting that dependency, stealing the Shutdown code from the SDK and writing my own Service Host. The result is on my Github Repo azure-webjob-host. Feel free to use it or raid it for ideas. I don't know, maybe if I did it again I'd have another attempt at getting the SDK to work, but I present this is a bit of an alternative to the SDK.
Basically I wrote an IServiceHost not too different from yours (except that StartAsync exited when stuff started instead of just hanging). Then I wrote my own service host, which is basically just a loop:
await _service.StartAsync(cancellationToken);
while (!token.IsCancellationRequested){await Task.Delay(1000);}
await _service.StopAsync(default);
Then I stole the WebJobsShutdownWatcher code from their repo.
Then I created an IServiceHost that started my message handler. (I was using Rabbit, which has nothing to do with triggers or azure stuff)
public class MessagingService : IHostedService, IDisposable
{
public MessagingService(ConnectionSettings connectionSettings,
AppSubscriberSettings subscriberSettings,
MessageHandlerTypeMapping[] messageHandlerTypeMappings,
ILogger<MessagingService> logger)
{
....
}
public async Task StartAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
await Task.WhenAll(subscribers.Value.Select(s => s.StartSubscriptionAsync()));
}
public async Task StopAsync(CancellationToken cancellationToken)
{
...
}
public void Dispose()
{
...
}
}
Then I put that all together into something like this:
IHostedService myService = new MyService();
using (var host = new ServiceHostBuilder().HostService(myService))
{
await host.RunAsync(default);
}
I have some workers attached to service bus topics and what we do is the following (ServiceBusClient is a custom Class that contains our Subscription Client):
public override Task StartAsync(CancellationToken cancellationToken)
{
_serviceBusClient.RegisterOnMessageHandlerAndReceiveMessages(MessageReceivedAsync);
_logger.LogDebug($"Started successfully the Import Client. Listening for messages...");
return base.StartAsync(cancellationToken);
}
public void RegisterOnMessageHandlerAndReceiveMessages(Func<Message, CancellationToken, Task> ProcessMessagesAsync)
{
// Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
// Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
// Set it according to how many messages the application wants to process in parallel.
MaxConcurrentCalls = 1,
// Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
// False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
AutoComplete = false
};
// Register the function that processes messages.
SubscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}
And then you can use DI to instantiate your service bus client and inject on the constructor of your Worker class.
Here i have the initialization of the singleton instance of my custom class Service Bus Client
services.AddSingleton<IServiceBusClient, ServiceBusClient>((p) =>
{
var diagnostics = p.GetService<EventHandling>();
var sbc = new ServiceBusClient(
programOptions.Endpoint,
programOptions.TopicName,
programOptions.Subscriber,
programOptions.SubscriberKey);
sbc.Exception += exception => diagnostics.HandleException(exception);
return sbc;
});
Then on this custom class, i initialize my subscription client
public ServiceBusClient(
string endpoint,
string topicName,
string subscriberName,
string subscriberKey, ReceiveMode mode = ReceiveMode.PeekLock)
{
var connBuilder = new ServiceBusConnectionStringBuilder(endpoint, topicName, subscriberName, subscriberKey);
var connectionString = connBuilder.GetNamespaceConnectionString();
ConnectionString = connectionString;
TopicName = topicName;
SubscriptionName = topicName;
SubscriptionClient = new SubscriptionClient(connectionString, topicName, subscriberName, mode);
}
You can check #george chen's answer from this post How to create service bus trigger webjob?
where instead of creating a receiver and registering a message handler, you can use the in built queue trigger and and write your message handler logic inside it.
Please help, this is driving me crazy!
My Server -> SignalR (JS) Client method execution works fine via the SignalR pipeline, but not when called via a background service. I'm using ASP.NET MVC / Castle Windsor DI
I use a custom Dependency Resolver which I register during Owin Startup
I observe that via the NotificationHub (SignalR pipeline), Clients is resolved to a HubConnectionContext class:
However, via my background service, Clients resolves to a HubConnectionContextBase class:
... so I'm pretty sure it's a DI issue. I just can't see what I'm doing wrong. Also any tips to Debug would be greatly appreciated. Thanks
Application Start:
bootstrapper = ContainerBootstrapper.Bootstrap();
this.container = bootstrapper.Container;
var resolverSignalR = new DependencyResolverSignalR(container);
GlobalHost.DependencyResolver = resolverSignalR;
OwinConfig:
app.MapSignalR(url, DependencyResolverSignalR.CreateHubConfiguration());
DependencyResolverSignalR:
public class DependencyResolverSignalR : DefaultDependencyResolver
{
public static HubConfiguration CreateHubConfiguration()
{
var signalrDependencyResolver = new DependencyResolverSignalR(_container);
var configuration = new HubConfiguration {
EnableDetailedErrors = true,
Resolver = signalrDependencyResolver
};
return configuration;
}
private static IWindsorContainer _container;
public DependencyResolverSignalR(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
_container = container;
}
public override object GetService(Type serviceType)
{
return _container.Kernel.HasComponent(serviceType) ? _container.Resolve(serviceType) : base.GetService(serviceType);
}
public override IEnumerable<object> GetServices(Type serviceType)
{
return _container.Kernel.HasComponent(serviceType) ? _container.ResolveAll(serviceType).Cast<object>() : base.GetServices(serviceType);
}
}
NotificationService:
(runs via a loop every 10 seconds - AFTER client has connected)
// Not working
var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
var clients = hubContext.Clients;
clients.All.receiveMessage(testMessage);
NotificationHub:
public override Task OnConnected()
{
var connectionId = Context.ConnectionId;
...
...
// Working fine
base.Clients.All.receiveMessage(testMessage);
return base.OnConnected();
}
Client:
omitted since it works fine via the signalr pipeline
I would expect the receiveMessage() client method called via the NotificationService to be executed on the client in exactly the same way it does when called via the SignalR pipeline. Instead nada. No error message, the call just silently does nothing.
I've even tried following the guide here (though it's geared towards Ninject) SignalR document to resolve the Clients (IHubConnectionContext) directly. Exactly the same result.
Code:
var resolverSignalR = new DependencyResolverSignalR(container);
container.Register(Component
.For<IHubConnectionContext<dynamic>>()
.UsingFactoryMethod(() =>
resolverSignalR.Resolve<IConnectionManager().GetHubContext<NotificationHub>().Clients));
Solved! It was an issue with the HubConfiguration - I'm not sure what exactly. But just bypassing it (which is possible because I'm already replacing the DependencyResolver in Application_Start()) fixed the issue.
Old OwinConfig:
app.MapSignalR(url, DependencyResolverSignalR.CreateHubConfiguration());
Replaced by:
app.MapSignalR();
but make sure you have something like this in Application_Start() (or wherever you initialise your DI container)
var resolverSignalR = new DependencyResolverSignalR(container);
GlobalHost.DependencyResolver = resolverSignalR;
I created a new console app in VS2017 and I am trying to run this code to demonstrate I can encrypt and decrypt strings in .NET Core. I have tried calling RunSample from the Program.cs Main but it wants it to be a static method. If I make RunSample static then I'm getting a null reference exception when trying to set the var protectedpayload.
using System;
using Microsoft.AspNetCore.DataProtection;
public class MyClass
{
private readonly IDataProtector _protector;
public MyClass(IDataProtectionProvider provider)
{
_protector = provider.CreateProtector("Contoso.MyClass.v1");
}
public void RunSample()
{
Console.WriteLine("Enter input:");
string input = Console.ReadLine();
var protectedPayLoad = _protector.Protect(input);
Console.WriteLine($"Protect returned: {protectedPayLoad}");
var unprotectedPayLoad = _protector.Unprotect(protectedPayLoad);
Console.WriteLine($"Unprotect returned: {unprotectedPayLoad}");
}
}
How can I run it?
UPDATE:
Trying to run it from Program.cs, I have the following but .MyClass has a "cannot resolve" syntax error / red underline on it.
using System;
namespace encrypttest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
new Program().MyClass.RunSample();
}
}
}
UPDATE 2:
I created my app by going to new project, .NET Core and then Console App (.NET Core):
UPDATE 3:
I've changed my code as suggested but have the following error:
UPDATE 4:
I've removed the using as suggested, but now I get:
you need DI your IDataProtector in Program.cs
class Program
{
static void Main(string[] args)
{
ServiceCollection services = new ServiceCollection();
services.AddScoped<IDataProtector>();
services.AddScoped<MyClass>();
using (var serviceProvider = services.BuildServiceProvider())
{
var service = serviceProvider.GetService<MyClass>();
service.RunSample();
}
}
}
update - 1:
Install from NuGet
Install-Package Microsoft.Extensions.DependencyInjection
update - 2:
remove using like this
var serviceProvider = services.BuildServiceProvider()
var service = serviceProvider.GetService<MyClass>();
service.RunSample();
update - 3:
My code is DI example...
you need change like this
ServiceCollection services = new ServiceCollection();
services.AddDataProtection();
services.AddScoped<MyClass>();
var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetService<MyClass>();
service.RunSample();
Install from NuGet
Install-Package Microsoft.AspNetCore.DataProtection
The complaint about making RunSamples static is because you're not newing up the class that defines it. If it's not an instance, then the method must be static to access it. However, since the class (and method) has a dependency that needs to be satisfied, you cannot make it static. Simply, you need to use dependency injection to create an instance of your class with its dependency satisfied in order to call the RunSamples method.
To use dependency injection in a console app, it's as simple as adding the Microsoft.Extensions.DependencyInjection NuGet package, and then:
var services = new ServiceCollection()
.BuildServiceProvider();
However, that's not very useful as you haven't registered anything, so just do all that before calling BuildServiceProvider:
var serviceProvider = new ServiceCollection()
.AddDataProtection()
.AddScoped<MyClass>()
.BuildServiceProvider();
Since you want to utilize data protection, you'll obviously need the Microsoft.AspNetCore.DataProtection NuGet as well.
Then, when you want an instance of MyClass:
using (var scope = serviceProvider.CreateScope())
{
var myClass = scope.GetRequiredService<MyClass>();
myClass.RunSamples();
}
I guess this is what you are missing (from this link: https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/using-data-protection?view=aspnetcore-2.1)
public static void Main(string[] args)
{
// add data protection services
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection();
var services = serviceCollection.BuildServiceProvider();
// create an instance of MyClass using the service provider
var instance = ActivatorUtilities.CreateInstance<MyClass>(services);
instance.RunSample();
}
You have to initiate an instance of IDataProtectionProvider by calling ActivatorUtilities.CreateInstance
I have an application which receive messages through a Azure ServiceBus using the Rebus library.
I observe a different behaviour when the application runs on Azure as a Web app, compared to when I run it locally.
When it runs in Azure, my message handlers are not being invoked.
Everything works as expected when the application runs local.
This is my setup.
In my web project Portal, Global.asax.cs:
protected void Application_Start()
{
_webContainer = new WindsorContainer();
_queueContainer = new WindsorContainer();
ConfigureWebContainer();
_queueContainer.Install(FromAssembly.Containing<ServiceBusInstaller>());
ApplicationInsightsConfig.Configure();
}
In the project ServiceBus (where ServiceBusInstaller lies)
public void Install(IWindsorContainer container, IConfigurationStore store)
{
var adapter = new CastleWindsorContainerAdapter(container);
var connectionString = CloudConfigurationManager.GetSetting("QueueUrl");
var configurer = Configure
.With(adapter)
.Options(o =>
{
o.SimpleRetryStrategy(maxDeliveryAttempts: 0);
})
.Logging(l => l.Log4Net())
.Routing(r => r.TypeBased()
.MapAssemblyOf<ResetPasswordMessage>("Email"))
Transport(t => t.UseAzureServiceBus(connectionString, "Email"));
// Create and starts the bus
configurer.Start();
}
My ResetPasswordHandler looks like this:
public class ResetPasswordHandler : IHandleMessages<ResetPasswordMessage>
{
private readonly IContactAndEmailService _contactAndEmailService;
public ResetPasswordHandler(IContactAndEmailService contactAndEmailService)
{
_contactAndEmailService = contactAndEmailService;
}
public async Task Handle(ResetPasswordMessage message)
{
_contactAndEmailService.SendEmail(message);
}
}
I am using Service Bus Explorer to connect to the service bus, and I can see that the message is in the "email" queue. It does however stay there, and thus is not consumed.
Any pointers to either setup or azure subscribtion limitations or something else that can help me move forward is highly appreciated.
I think it might have something to do with the casing of the queue name, Email.
Could you try and make it lowercase throughout?
You'll have to excuse the strange title but after several hours of looking at the same issue, it's the best I could come up with!
I initially had signalr embedded with my MVC project but moved it out into an self-hosted OWIN application.
When ever a hub is called upon, on the initial load it will load the hub twice and subsequent calls it will load it three times.
Here is my hub, and I followed the documentation to the tee:
public class TestHub : Hub
{
private readonly ILifetimeScope _scope;
private ITestService _testService;
public TestHub(ILifetimeScope scope)
{
_scope = scope.BeginLifetimeScope();
_testService = _scope.Resolve<ITestService>();
}
public void SignalRTest()
{
var types = _testService.SomeMethod();
Clients.Caller.populateSignalRTest(types);
}
protected override void Dispose(bool disposing)
{
if (disposing && _scope != null)
{
_scope.Dispose();
}
base.Dispose (disposing);
}
}
Here is the OWIN configuration:
var listener = (HttpListener)appBuilder.Properties[typeof(HttpListener).FullName];
listener.AuthenticationSchemes = AuthenticationSchemes.Ntlm;
appBuilder.UseCors(CorsOptions.AllowAll);
var builder = new ContainerBuilder();
// ... Other modules being imported ...
builder.RegisterModule<NLogModule>();
builder.RegisterType<TestService> ().As<ITestService> ();
builder.RegisterHubs(Assembly.GetExecutingAssembly());
var config = new HubConfiguration();
var container = builder.Build();
config.Resolver = new AutofacDependencyResolver(container);
config.EnableDetailedErrors = true;
appBuilder.UseAutofacMiddleware(container);
appBuilder.MapSignalR("/signalr", config);
Autofac has to resolve all dependencies before running any methods - the application calls upon mulitple Hubs and this takes a long time to resolve.
Here are the versions I am using:
SignalR 2.1.2 (Also tried 2.2.0)
AutoFac 3.5.2
Has anyone come across this or know why this is happening?
Thanks
A colleague did a bit of debugging whilst I raged and did something else and discovered it was one of the services rather than AutoFac or signalR. We have an Index and the constructor method of the service was verifying the index was valid. Remove it and it was fine.