Add SignalR client functionality to a Fable.Elmish application - signalr.client

I found 2 resources from the community
1.Fable.SignalR - A functional type-safe wrapper for SignalR and Fable. I am using Elmish so the package is Fable.SignalR.Elmish
2.fable-signalr - Fable bindings for SignalR
2 Is declared to work only before Fable 3, which is not my case.
With 1 I have a problem: I do not control the server side. The server application is a standard C# ASP.NET core Web Api application that uses SignalR.
All the examples I found for 1 require a shared F# library, which cannot be done in this case.
Let's suppose to have a simple Hub like in the official docs:
using Microsoft.AspNetCore.SignalR;
namespace SignalRChat.Hubs
{
public class ChatHub : Hub {}
}
and an Api controller that receives the hub context through DI and simply broadcasts a message:
class YatpApiController(IHubContext<ChatHub> hubContext) : ControllerBase() {
[HttpPost("signalr-broadcast")]
public SignalrBroadcast()
{
hubContext.Clients.All.SendAsync("Method1", "Message1");
}
}
Can someone please
Show how to use Fable.SignalR.Elmish client side with this very simple case?
Give advise on an alternative way to connect to this simple hub from a Fable.Elmish application, without writing bindings to the javascript signalr package?

Related

How do I get a connection string in a .net core standard class library from the configuration file in a .net core 2.0 web app?

I have .net core standard class library which is essentially a DAL with several class methods that return collections and objects from a database. The connection string is in the appsettings.json file of the ASP.net 2 core web app. I also want to access this class library from a console app project where the configuration file with the connection string will be present in that console app project.
This was simple in .net prior to .net core. The DAL class library would just access the web.config from a web project and an app.config from a console application as it the library is referenced in both the web app and console apps. But it doesn't seem like this is at all possible.
I'm looking for the simple solution in .net core to get a connection string from web app or console app as the case may be.
Where you're probably going wrong is that you want to access configuration from your class library, but then you want to leak details specifically about the caller (That it will have a web.config).
But what if you decide in your Web Application you want to use Azure Key Vault or another secrets mechanism? Does your class library need to then change it's entire implementation to use Key Vault? And then does that mean your console application also has no option but to use Key Vault too?
So the solution is to use dependency inversion. Put simply, let's say I have code like the following :
interface IMyRepositoryConfiguration
{
string ConnectionString {get;}
}
class MyRepositoryConfiguration : IMyRepositoryConfiguration
{
public string ConnectionString {get;set;}
}
class MyRepository
{
private readonly IMyRepositoryConfiguration _myRepositoryConfiguration;
public MyRepository(IMyRepositoryConfiguration myRepositoryConfiguration)
{
_myRepositoryConfiguration = myRepositoryConfiguration;
}
}
Now in my startup.cs I can do something like :
services.AddSingleton<IMyRepositoryConfiguration>(new MyRepositoryConfiguration {//Set connection string from app settings etc});
And now my class library doesn't need to know exactly how those configuration strings are stored or how they are fetched. Just that if I request an instance of IMyRepositoryConfiguration, that it will have the value in there.
Alternatively of course, you can use the Options class too, but personally I prefer POCOs. More info here : https://dotnetcoretutorials.com/2016/12/26/custom-configuration-sections-asp-net-core/
It is very much possible to access "connection strings" or other configuration data easily in .Net core without much additional effort.
Just that the configuration system has evolved (into something much better) & we have to make allowances for this as well (& follow recommended practices).
In your case as you are accessing the connection string value in a standard library (intended to be reused), you should not make assumptions as how the configuration values will be "fed" to your class. What this means is you should not write code to read a connection string directly from a config file - instead rely on the dependency injection mechanism to provide you with the required configuration - regardless of how it has been made available to your app.
One way to do this is to "require" an IConfiguration object to be injected into your class constructor & then use the GetValue method to retrieve the value for the appropriate key, like so:
public class IndexModel : PageModel
{
public IndexModel(IConfiguration config)
{
_config = config;
}
public int NumberConfig { get; private set; }
public void OnGet()
{
NumberConfig = _config.GetValue<int>("NumberKey", 99);
}
}
In .net core, before the app is configured and started, a "host" is configured and launched. The host is responsible for app startup and lifetime management. Both the app and the host are configured using various "configuration providers". Host configuration key-value pairs become part of the app's global configuration.
Configuration sources are read in the order that their configuration providers are specified at startup.
.Net core supports various "providers". Read this article for complete information on this topic.

register server wide javax.ws.rs.client.ClientRequestFilter on JBoss EAP 7

Is it possible to register a javax.ws.rs.client.ClientRequestFilter server wide on JBoss EAP 7? I would like to intercept all outbound JAX-RS calls to dynamically add some context information in HTTP headers.
For JAX-WS calls I was able to do this with https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.0/html-single/developing_web_services_applications/#jax_ws_handler_chains. I can't find any documentation on a similar mechanism for JAX-RS.
Or alternatively, is there maybe another way to intercept outbound HTTP calls in general?
For a per server solution, according to Using HttpHandler class in Undertow "you need to package your handler(s) into a module, and configure custom-filter in undertow subsystem."
The module.xml example and undertow configuration has been given as well as filter source code!
Update
There's an example of using the HTTPExchange here though I dont really care much for that site. SO also has this slightly related example - it does look like it can work similarly to the JAX-WS Handlers/Interceptor How to properly read post request body in a handler
Another good example file upload using httphandler I know they're different that dealing with JAX-RS but still may apply.
I implemented it by creating a module with the following contents:
package be.fgov.kszbcss.tracer.jaxrs;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
public class TracerResteasyClientBuilder extends ResteasyClientBuilder {
#Override
public ResteasyClient build() {
return super.build().register(TracerJaxRsClientRequestFilter.class);
}
}
/META-INF/services/javax.ws.rs.client.ClientBuilder
be.fgov.kszbcss.tracer.jaxrs.TracerResteasyClientBuilder
And registering it as a global module on JBoss EAP.

Is there an Azure Websites functionality equivalent to Azure Web Roles RoleEntryPoint

I want to run some code from a library before Application_Start and i was wondering if that is possible using just Azure websites or if I have to purchase an Azure Web Role instance and use RoleEntryPoint?
Have you tried using the WebActivator NuGet package? Have a look on GitHub for further details but the basics of it are simply adding an attribute and an initialisation method to your application. For example:
using System;
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.Bootstrapper), "PreStart")]
namespace MyApp {
public static class Bootstrapper {
public static void PreStart() {
// Add your start logic here
}
}
}
The code in PreStart will run before Application_Start.
There are other attributes you can use for doing things on shutdown (ApplicationShutdownMethodAttribute) and for post startup (PostApplicationStartMethodAttribute).

Do I have to do the configuration for nservicebus on mvc4 that would be handled by the EndpointConfig.cs?

In the examples, most of the config is done by the dev by changing AsA_Server to AsA_Client.
public class EndpointConfig : IConfigureThisEndpoint, AsA_Client { }
However, I can't seem to do that with an ASP.NET MVC4 app.
Do I have to manually configure everything in a web environment?
Yes, here are links to the documentation:
http://support.nservicebus.com/customer/portal/articles/894008-using-nservicebus-with-asp-net-mvc
http://support.nservicebus.com/customer/portal/articles/894123-injecting-the-bus-into-asp-net-mvc-controller
You can also have a look at our sample projects for examples on how to do it, see https://github.com/NServiceBus/NServiceBus/tree/master/Samples/AsyncPagesMVC3
There is also a sample that uses MVC4 but that is against NServiceBus v4 which has not been released yet, see https://github.com/Particular/NServiceBus/tree/develop/Samples/VideoStore.Msmq/VideoStore.ECommerce

How to host Web API in Windows Service

I have several resources that I'd like to expose using the WCF Web API. I've investigated the Web API using a Web host but our services all run as Windows Services in production so it's time for me to put the tests aside and verify that everything will work as we need it. I've looked as the sample app here: http://webapicontrib.codeplex.com/SourceControl/changeset/view/2d771a4d6f6f#Samples%2fSelfHosted%2fserver%2fProgram.cs but this does not work with the current version (preview 5) because the HttpConfigurableServiceHost class is not accessible from our code.
One of the most appealing aspects of the Web API is the simple startup using MapServiceRoute and the new WebApiConfiguration. I don't see, however, a way to define the base url and port for the services. Obviously, hosting the service in IIS eliminates this because we configure this information in IIS. How can I accomplish this when hosting in a Windows Service?
It's actually pretty simple. In a nutshell you need to instantiate HttpSelfHostServer and HttpSelfHostConfiguration and then call server.OpenAsync().
public void Start()
{
_server.OpenAsync();
}
public void Stop()
{
_server.CloseAsync().Wait();
_server.Dispose();
}
For an example on how to do this using Windows service project template and/or Topshelf library see my blog post: http://www.piotrwalat.net/hosting-web-api-in-windows-service/
The latest version just uses HttpServiceHost. http://webapicontrib.codeplex.com/SourceControl/changeset/view/ddc499585751#Samples%2fSelfHosted%2fserver%2fProgram.cs
Ping me on twitter if you continue to have problems.
This is the basic code using a console app. A Windows Service uses the same basic approach except you use the start and stop methods to start and stop the service and don't need to block.
static void Main(string[] args)
{
var host = new HttpServiceHost(typeof(PeopleService), "http://localhost:8080/people");
host.Open();
foreach (var ep in host.Description.Endpoints)
{
Console.WriteLine("Using {0} at {1}", ep.Binding.Name, ep.Address);
}
Console.ReadLine();
host.Close();
}
See this blog post.