I'm trying to figure out how to log from my Mobile Service with a .NET backend. So far, I've read that you can do this by calling:
ApiController.ApiServices.Log.Info("log string")
This is fine if you're in an ApiController as you can simply use this.Services, but what would you do if you wanted to log from somewhere besides a controller? Is this even possible?
If by "besides a controller" you mean in a custom controller, you can declare
public ApiServices Services { get; set; }
and then use the Services.Log.Error("your error")
If this is not what you're asking, please give some more information.
Related
I have a ASP.NET Core 2.1 (API) application running in Azure Web App.
My application errors follow a standard json structure. When there is an error (for example a 500) in IIS, it returns a xhtml page with the description of the error.
How can I set Azure Web App, or my application, so that on IIS errors, it returns a json string defined by me.
Thanks in advance.
We can use this method to achieve this: IApplicationBuilder.UseStatusCodePagesWithRedirects()
Here is a sample for your reference:
We can create Controller to handle the error output:
[Route("Home/Error/{statusCode}")]
public IActionResult Error(int statusCode)
{
JsonResult jsonResult = new JsonResult(new object());
return jsonResult;
}
Then We can use it in Configure(IApplicationBuilder app, IHostingEnvironment env) as below:
app.UseStatusCodePagesWithRedirects("/Home/Error/{0}");
Hope this would be helpful to you.
I want to build an Azure Function that responds to HTTP POST requests coming from another Azure function i.e. MasterFunction calling NotificationsFunction.
Say, I have the following simple POCO object:
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public int Mileage { get; set; }
}
Both functions will be sharing the same class library containing these POCO objects.
Am I right to assume that in the MasterFunction, I'll have to serialize my Car object to JSON, then make an HTTP call?
Could someone point me to some code samples on a similar scenario?
If both of your functions are in the same azure function app (which I think that's the case), I'd say the best way of calling other functions is using Queues.
In the sense that you put your POCO into a queue and define your second function with a QueueTrigger. So once an object gets into the queue, the other function gets called automatically and the object get dequeued.
You can find samples and more details in here : https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue
If I understand correctly you want service communication between services/endpoints.
You can use Orchestration or choreography for service communication.
orchestration in azure using 1. Durable function 2. Logic app
Choreography in azure using 1. Storage queue 2. Service Bus.
Hope it will help
I haw an aggregate in my domain that have some Guid secondary keys.I want to get more info about these keys from another domains via RESTfull.
public class ProductAggregate
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public Guid AgencyId { get; set; }
public string AgencyTitle { get; set; }
}
In above code the AgencyTitle not exists in current domain and I want to get it from another live service via Restfull and send aggregated object to the client.
Is it a acceptable way?
It sounds like you are talking about a read model that you present to a user rather than an aggregate.
There are various ways you can handle this:
Local Caching
Keep an in-memory cache locally in your service that maps between AgencyId and AgencyTitle - this can either be through:
listening to an event (i.e. AgencyCreated). This would be preferred if you are have an event-driven system. You could also listen to other events (i.e. AgencyTitleUpdated) if relevant.
by making a web request to the external services. You would query your local cache first, and then decide if to call the external service. You'd need to think about how stale you allow your cache to become.
Denormalising the data
You could duplicate the data by saving the AgencyTitle alongside the AgencyId. This way you have no call to an external service. The tradeoff is you need to consider how often the AgencyTitle is likely to change, and how you handle that change.
Reporting "domain"
You could have a completely separate service that listens for data from other services and maintains view models for UIs. This would keep your other services ignorant of other service's concerns. When using an event-driven system, you'd be listening for events form other services that allow you to build a view model for the UI
I am looking for an example implementation for resolving tenants in a multi-tenant ServiceStack API layer.
If you've got your Api host setup and you've providing an implementation of AppHostBase, you can override the Configure method like so;
public class ApiHost : AppHostBase
{
public ApiHost() : base("Service Name", typeof(ApiHost).Assembly) { }
public override void Configure(Funq.Container container)
{
//resolve your tenant here..
}
}
Now you probably want some code to resolve your tenant. Say you were doing this via subdomain, you want something like this;
string subdomain = HttpContext.Current.Request.Url.Host.Split('.')[0].ToLower();
You should probably perform some checks to ensure validity of the url too. Then just use your repository or DAL to resolve your tenant with a relevant query.
After that you need to decide how you're going to pass your tenant about to your services and such. Question for another time, probably :)
I have sample ASP.NET Web API with get method, I have prefixed a [Authorize] attribute on top of the method. Can I please know how should I call this method from browser or fiddler? Also, I am hosting these API's on Windows Azure
public class ValuesController : ApiController
{
// GET api/values
[Authorize]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
Depending on the type of authorization you are using there might be different ways. But if you are using default routing you could call your method at the following url:
/api/values
You might of course need to pass additional headers depending on the authorization mechanism you choose. The [Authorize] attribute doesn't do anything unless you have configured some authorization. You may take a look at the following article for an example of how you could use tokens to authenticate your users.