Resolving WCF endpoint address dynamically with autofac - wcf

I have a WCF client used in MVC application which can get data from multiple WCF services, the services are configured the same way and Implement the same Interface the only difference is the address of the exposed endpoint.
This is what I tried:
builder.Register(c => new ChannelFactory<IService>(
new BasicHttpBinding(),
new EndpointAddress("http://service.com/Service")))
.InstancePerHttpRequest();
builder.Register(c => c.Resolve<ChannelFactory<IService>>().CreateChannel())
.UseWcfSafeRelease();
The thing here is that IService will always get data from http://service.com/Service since the address is hardcoded somewhere in the Application_Start method of the MVC application.
Then i tried using metadata:
builder.Register(c => new ChannelFactory<IService>(
new BasicHttpBinding(),
new EndpointAddress("http://foo.com/Service")))
.SingleInstance().WithMetadata("name", "fooservice");
builder.Register(c => new ChannelFactory<IService>(
new BasicHttpBinding(),
new EndpointAddress("http://bar.com/Service")))
.SingleInstance().WithMetadata("name", "barservice");
builder.Register(c => c.Resolve<ChannelFactory<IService>>().CreateChannel())
.UseWcfSafeRelease();
But this way I will have to edit the code every time I want to add the same WCF service
implemented on a different server.Instead I want to get the address from the database.
Is there any way I can change the address per service call or at least when the instance of the client is created.
Additional explanation:
Lets say I have five exact copies of a website each with it's own domain name and database I want to be able to do the following:
foreach(Provider provider in providers)
{
SetServiceAddress(provider.Address);//how can i do that
_service.GetData()
}

Under the assumptions that:
The binding doesn't change when the address changes (e.g., it doesn't switch from HTTP to HTTPS)
The address might change on a per-request basis
Then I'd probably solve it with a combination of lambdas and a small interface.
First, you'd want something that retrieves the address from your data store:
public interface IAddressReader
{
Uri GetAddress();
}
The implementation of that would read from the database (or environment, or XML config, or whatever).
Then I'd use that in my registrations:
builder
.RegisterType<MyDatabaseAddressReader>()
.As<IAddressReader>();
builder
.Register(c => new ChannelFactory<IService>(new BasicHttpBinding()))
.SingleInstance();
builder
.Register(c =>
{
var reader = c.Resolve<IAddressReader>();
var factory = c.Resolve<ChannelFactory<IService>();
var endpoint = new EndpointAddress(reader.GetAddress());
return factory.CreateChannel(endpoint);
})
.As<IService>()
.UseWcfSafeRelease();
That way you can just take in an IService (or Func<IService>) as a constructor parameter and your calling class won't know about Autofac, service location, or endpoints.
If the binding also changes, it gets a little more complicated. You probably don't want a brand new channel factory spun up for every channel, so you'd want to have some sort of caching mechanism where you:
Get the settings from the configuration source.
Compares those settings against the settings currently in use.
If the settings don't match...
Dispose of the previous channel factory.
Create a new channel factory with the new settings.
Cache the channel factory for later reuse.
Return the current channel factory.
If you can use cache dependencies on the settings, all the better, but not every configuration source supports that, so YMMV. I'd probably implement a custom module for that to encapsulate the logic, but I won't write all that out here.

If you want to set the endpoint just before the call each time, you can do this:
containerBuilder
.Register(c => new ChannelFactory<IService>(new BasicHttpBinding()))
.SingleInstance();
containerBuilder.Register((c, p) =>
{
var factory = c.Resolve<ChannelFactory<IService>>();
var endpointAddress = p.TypedAs<string>();
return factory.CreateChannel(new EndpointAddress(endpointAddress));
})
.As<IService>()
.UseWcfSafeRelease();
Then you inject this:
Func<string, IService> getService
Then call it like this:
string endpoint = getDataDependentEndpointFromSomewhere();
var service = getService(endpoint);

I have a service that is running on multiple sites, and at start-up the app needs to determine at which site it is running. It does so using a start-up parameter, and based on this the endpoint address can be set dynamically in a property or method, like GetEndPointAddressForService().
In your case it seems that you need to call n services at different sites consecutively. You could definitely configure these in a database or a simple configuration file on disk, load the service definitions including their endpoint addresses at start-up, keep them in a list and do a foreach when collecting data from all existing servers.
The key part of your logic is in the following part of your code:
new EndpointAddress("http://bar.com/Service")
Do a
foreach (ServiceDefinition sd in ServiceDefinitions)
{
builder.Register(c => new ChannelFactory<IService>(
new BasicHttpBinding(),
new EndpointAddress(sd.EndPointAddress)))
.InstancePerHttpRequest();
builder.Register(c => c.Resolve<ChannelFactory<IService>>().CreateChannel())
.UseWcfSafeRelease();
GoGetTheData();
}

At the end I have used the following implementation:
On application start I register the ChannelFactory type without the endpoint address.
And I use named parameter to register the client so i can be able to assign the address later when I actually call the service.
builder.RegisterType<ChannelFactory<IService>>(new BasicHttpBinding())
.SingleInstance();
builder.Register((c, p) => c.Resolve<ChannelFactory<IService>>().CreateChannel(p.Named<EndpointAddress>("address")))
.UseWcfSafeRelease();
and then I use the service client at runtime like this:
public Data GetData(string url)
{
EndpointAddress address = new EndpointAddress(url);
NamedParameter parameter = new NamedParameter("address", address);
var service = _autofacContainer.Resolve<IService>(parameter);//this is what I have been looking for
Response response = service.GetData();
return CreateDataFromResponse(response);
}
this way I can call the GetData method for each address in the database. And I'm going to able to add more addresses at runtime without code or configuration editing.

Related

Http Client registration based on http header received from a middleware

I am having below situation : I am using web api core 3.1 framework (c#)
I am using typed httpclient registered in the startup . while registering typed client on startup, i am not able to provide the base URL and credentials because I am getting thru a service called configread and it reads the data from the header , which will be only available when one of our middle ware runs and sets it.
in my case base address, user id and passwords are coming from a service call but service calls depends on the request header (httpContext object). in the configureService methods , request context is not available.
Right now i am having trouble to get the httpClient from the startup.
Any guidance would be appreciated.
Update1:
I am adding a typed client as below
service.AddHttpClient<IAgencyServiceAgent,AgencyServiceAgent> (GetAgencyAgentHttpClient()).
ConfigurePrimaryHttpMessageHandler(GetAgencyHttpMessageHandler()) private Action<HttpClient> GetAgencyAgentHttpClient ()
{
var configUrl = Environment.GetEnvironmentVariable(ConfigConstants.CONFIGSERVICE URL)
return httpClient => {
// Here the base address is availble thru another service // which accept the data from the httpContext and based on the values / It pulls the base address and request header etc...
}
}
Update2:
I am having difficulty in setting this httpclient in the startup beacuse baseUrl and other info depends on the request object. For ex: i am reading a request header called DEV1 and passing it to another service , then it will return me the base address and credentials needed then after i can set the http client My questions are how do go about it . When httpClient configurations are depend on the httpContext object .. then how we should register and use it Thanks
According to your description, I suggest you could try to build ServiceProvider inside your GetAgencyAgentHttpClient method and use GetService method to get which service you want to use.
More details, you could refer to below codes:
services.AddHttpClient("hello", c =>
{
//Build service provider
ServiceProvider serviceProvider = services.BuildServiceProvider();
//Get the ICurrentUserService
var currentUserService = serviceProvider.GetService<ICurrentUserService>();
//Use ICurrentUserService GetIPaddress method
var re= currentUserService.GetIPaddress();
c.BaseAddress = new Uri("http://localhost:5000");
}).AddTypedClient(c => Refit.RestService.For<IHelloClient>(c)); ;
Result:
If you want to check get the http header from current httprequest, you could try to get the httpcontext accessor in the service.
More details, you could refer to below codes:
services.AddHttpClient("hello", c =>
{
ServiceProvider serviceProvider = services.BuildServiceProvider();
//var currentUserService = serviceProvider.GetService<ICurrentUserService>();
//var re= currentUserService.GetIPaddress();
var httpcontext = serviceProvider.GetService<IHttpContextAccessor>();
var re = httpcontext.HttpContext.Request.Headers.ToList();
c.BaseAddress = new Uri("http://localhost:5000");
}).AddTypedClient(c => Refit.RestService.For<IHelloClient>(c));
Result:

How do I call DotNet Core API HealthCheck probes within Controller instead of setting up in Ctartup.cs

I would like to setup Microsoft.Extensions.Diagnostics.HealthChecks so that I can setup response body within controller instead of standard setup in Startup.cs. Is this possible? If so, how can I achieve this?
The thought here is that I would like control over the response payload setter logic, and to do this within a controller action/method.
Online contains clear instructions on how to setup healthcheck probes, but all examples show the setup occuring within Startup.cs.
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-3.1
Are probes strickly setup within startup only? Is this a constraint?
My understanding is that the healtcheck library is middleware that will terminate request from going further down the middleware pipeline, and that perhaps removing the middleware will mean that whatever was setup in startup must now be setup within controller action method.
Is it possible to setup healthcheck probes within controller action methods? Answer is No
You can use app.UseHealthChecks to have custom control on health check enpoint
app.UseHealthChecks("/health-detailed", new HealthCheckOptions
{
ResponseWriter = (context, result) =>
{
context.Response.ContentType = "application/json";
var json = new JObject(
new JProperty("status", result.Status.ToString()),
new JProperty("duration", result.TotalDuration),
new JProperty("results", new JObject(result.Entries.Select(pair =>
new JProperty(pair.Key, new JObject(
new JProperty("status", pair.Value.Status.ToString()),
new JProperty("tags", new JArray(pair.Value.Tags)),
new JProperty("description", pair.Value.Description),
new JProperty("duration", pair.Value.Duration),
new JProperty("data", new JObject(pair.Value.Data.Select(
p => new JProperty(p.Key, p.Value))))))))));
context.Response.ContentType = MediaTypeNames.Application.Json;
return context.Response.WriteAsync(
json.ToString(Formatting.Indented));
}
});
TL&DR: Use this Library: https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks if you want somthing already created.
This website provides a ton of fully functional healthchecks for different services such as PostGres, Redis, S3, and etc.

WCF Discovery and DataService V3

I would like to expose discovery endpoints (both TCP and UDP) for my Data Services v3 and enable services to be discoverable from the client and discover them in another application. The main point in the discovery is to get the service endpoint address at the client.
I have tried to adapt the samples that Microsoft have provided for WCF Discovery, but so far I failed to achieve my goal.
I have created a custom Data Service Host Factory on server side:
public class CustomDataServiceHostFactory : System.Data.Services.DataServiceHostFactory
{
protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
var serviceHost = base.CreateServiceHost(serviceType, baseAddresses);
EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();
// Create XML metadata to add to the service endpoint
XElement endpointMetadata = new XElement(
"Root",
new XElement("Information", "This endpoint is Data Service v3!"),
new XElement("Time", System.DateTime.Now.ToString("MM/dd/yyyy HH:mm")));
// Add the XML metadata to the endpoint discovery behavior.
endpointDiscoveryBehavior.Extensions.Add(endpointMetadata);
//may be this is not the safest way to set the behaviour
foreach (var endpoint in serviceHost.Description.Endpoints)
{
endpoint.Behaviors.Add(endpointDiscoveryBehavior);
}
// Make the service discoverable over UDP multicast
serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
return serviceHost;
}
}
On the client side I have tried the following code:
DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
// Find service endpoints
// ServiceReference.DataModel is the generated class for the Data Service client proxy
FindCriteria findCriteria = new FindCriteria(typeof(ServiceReference.DataModel));
findCriteria.Duration = TimeSpan.FromSeconds(30);
FindResponse findResponse = discoveryClient.Find(findCriteria);
// Check to see if endpoints were found & print the XML metadata in them.
if (findResponse.Endpoints.Count > 0)
{
foreach (XElement xElement in findResponse.Endpoints[0].Extensions)
{
Console.WriteLine("Printing Metadata from ServiceEndpoint:");
Console.WriteLine("Endpoint Information: " + xElement.Element("Information").Value);
Console.WriteLine("Endpoint Started at Time: " + xElement.Element("Time").Value);
Console.WriteLine();
}
}
Unfortunately this does not work. I get InvalidOperationException:
Attempted to get contract type for DataModel, but that type is
not a ServiceContract, nor does it inherit a ServiceContract.
If I am heading in the right direction I need a way to express the type for the service contract for the discovery. Too bad I am not sure that it will even work like the normal WCF Discovery...
Please share your ideas or even better - working solutions.
I think exception message is clear enough.
For service discovery You try to use type of your data model, while You must use type of your WCF service implementation - this is different things.
Basically DataServicesV3 service adapter uses your data model and exposes it as a WCF service with it's own service contract.
Look at DataServiceV3 type declaration see that it is implementing some interface, i just don't remember name, in this interface declaration you will find [ServiceContract] and [ServiceOperation] attributes. This is Your SERVICE CONTRACT for all ancestors of DataServiceV3. They use THE SAME contract. Here stands another problem I haven't managed to solve yet - how to make WS-Discovery work with DataServices if they share same contract. You'd better dig in this way.

How to define configs multiple endpoints for a WCF self-hosted service?

I have two WCF Web API Contracts. Before this, I was happy that I could use TestClient. But after I implemented the second one I had to define endpoints (and could not use the default one) and after that, either I see nothing in browser or this message saying that "This XML file does not appear to have any style information associated with it." when I try to go to the endpoint address. It is the same when I try the config file (although I do not know how to set "EnableTestClient = true"). I really appreciate any help.
var baseurl = new Uri("http://localhost:7000/api/v1.0");
var config = new HttpConfiguration() { EnableTestClient = true };
config.CreateInstance = (type, context, request) => container.Resolve(type);
var host = new HttpServiceHost(typeof(ServiceAPI), config, baseurl);
host.Description.Behaviors.Add(
new ServiceMetadataBehavior() { HttpGetEnabled = true, HttpGetUrl = baseurl });
// Add MEX endpoint
//host.AddServiceEndpoint(
// ServiceMetadataBehavior.MexContractName,
// MetadataExchangeBindings.CreateMexHttpBinding(),
// "mex"
//);
//host.AddServiceEndpoint(typeof(IStatAPI), new WebHttpBinding(), "/stat");
//host.AddServiceEndpoint(typeof(IAlarmAPI), new WebHttpBinding(), "/alarm");
host.Faulted += (s, e) => Debug.WriteLine(e);
host.Open();
I don't believe that multiple endpoints should be used to expose different APIs. They are for exposing the same contract with a different binding.
You should create a new host for each API. You can share the config between them though.

How do I supply a specific instance of a class, to expose as my WCF service

I have a class that implements a plugin for an existing application.
I also have exposed that class as a WCF service. That part is working so far. The problem I am running into is that the application I am plugging into creates the instance of my class that I want to use.
Is there a way to pass an existing class instance to the WCF service host, to expose as a service endpoint?
I know (or can figure out) how to make a singleton instance of a WCF service, but that still won't help me. From what I can tell, the singleton instance will still be created and provided by WCF.
I have thought of other approaches, but I'd rather take this one if it is available to me.
Some code. This is in the constructor of my plugin:
// Setup the service host
var baseAddress = new Uri("http://localhost:8080/MyService/");
this.serviceHost = new ServiceHost(this.GetType(), baseAddress);
// Add our service endpoint
// Todo: Is there somewhere around here that I can provide an instance?
// Maybe in behavior somewhere?
this.serviceHost.AddServiceEndpoint(
typeof(ITheInterfaceMyClassDerivesFrom),
new BasicHttpBinding(),
""
);
// Add metadata exchange (so we see something when we go to that URL)
var serviceMetadataBehavior = this.serviceHost.Description.Behaviors
.Find<ServiceMetadataBehavior>();
if (serviceMetadataBehavior == null)
this.serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
this.serviceHost.AddServiceEndpoint(
typeof(IMetadataExchange),
new CustomBinding(new HttpTransportBindingElement()),
"MEX"
);
This is in the plugin's OnStartedUp method (called by the application I am plugging into):
serviceHost.Open();
You need to use the other constructor for ServiceHost if you want to do this - check out the MSDN docs at http://msdn.microsoft.com/en-us/library/ms585487.aspx
public ServiceHost(
Object singletonInstance,
params Uri[] baseAddresses
)