I'm having trouble enabling logging for a ServiceStack.JsonServiceClient. I'm working from the documentation Capture HTTP Headers in .NET Service Clients but I must be missing something becasue I'm only ever getting an empty string.
Here's my code:
public class Client
{
private bool Logging { get; }
public Client(bool logging = false)
{
Api = new("https://api.service.com/");
Logging = logging;
if (Logging) Api.CaptureHttp(true, true);
}
public string GetInfo(string name)
{
if (Logging) Api.HttpLog.Clear();
var response = Api.Get(new Requests.GetInfo(name));
Debug.WriteLine(Api.HttpLog.ToString());
return response.Result;
}
}
[Route("/v1/getinfo/{name}")]
[DataContract]
public class GetInfo : Base, IReturn<Responses.GetInfo>
{
public GetInfo(string name) => Name = name;
[DataMember(Name = "name")]
public string Name { get; init; }
}
[TestMethod]
public void Client_GetInfo()
{
var client = new Client(true);
client.GetInfo()
}
Because I have Api.CaptureHttp(true, true) it is logging the info to the test log but I want to be able to capture the httpLog in code and like I said, it's only ever an empty string there.
CaptureHttp has 3 flags:
public void CaptureHttp(bool print = false, bool log = false, bool clear = true)
You're only setting Api.CaptureHttp(print:true,log:true) which should print to the console and log to your logger if IsDebugEnabled, your call only sets the first 2 flags but you're code is relying on capturing the log to print it out in which case you wanted to specify that it shouldn't clear the HttpLog after each request with:
Api.CaptureHttp(clear:false)
Related
I created a function that add a new user with Angular/WebAPI , so in angular i send the object like this :
AddUser(person : person):Observable<person>
{
return this.http.post<person>(this.baseUrl,person);
}
and in WebAPI i got the data :
[HttpPost]
public async Task<ActionResult<Person>> PostPerson(Person person)
{
Now I want to add a picture of each user, and i don't know how it's gonna be the objet in angular, should i add a property imageProfile as File which i'm not sure if it's possible , or it should be a string of the uploaded file.
export class person {
idPerson:number=0;
fname : string ="" ;
lname : string ="";
password : string ="";
idCategory? :number;
nameCategory:string="";
imageProfile :
}
That's very good info from Codemaze, as always. The remark from Lavstsen is not correct, there's no need for a separate call.
For example, you could have a form linked to the structure of the person-type:
initializeForm() {
this.personForm = this.form.group({
fname: [''],
})
}
In your onSubmit-method, you seize the date from your form and put in a dto, in this example through a getPersonDto-method
private getPersonDto(): PersonDto {
let dto = new PersonDto();
dto.fname = this.imageForm.controls.fname.value;
return dto;
}
and next you can solve it like this:
const formData = new FormData();
formData.append('Imagefile', this.selectedFile, this.selectedFile.name);
formData.append('fname', personDto.fname); // needs to be strings => toString()
this.personApiClient.uploadImageFile(this.personId, formData)
.subscribe((res: any) => {
this.uploadOrDeleteResults = res;
this.showToastrMessageSuccess("PAGES.COMMON.TOASTR.TITLE.SUCCESSFULCREATE", "PAGES.COMMON.TOASTR.MESSAGE.SUCCESSFULCREATE");
},
(error) => {
this.crudHasErrors = true;
this.errorHttpErrorResponse = error;
this.errors = this.errorHttpErrorResponse.error;
},
you don't need the dto-step as such, but if you structurally always use dto's in your application, it would make more sense
Take care and good luck
I have another workaround but this will only work for very small files where you don't need upload percentage to show on UI.
I have my controller like this:
[HttpPost("/api/sign-up")]
public void SaveUser([FromForm] UserModel info)
{
_logger.LogDebug(null, "Hello", info); // Just to see contents of info object in debugger
}
and make your model like this:
public class UserModel
{
[FromForm(Name = "avatar")]
public IFormFile Avatar { get; set; }
[FromForm(Name = "email")]
public string Email { get; set; }
}
Basically, I have OBS running on a computer and I need to interact with it through a second computer.
I am using an OBS-WebSocket plugin which is creating a websocket server on OBS to send remote commands.
My goal is to set visible / invisible a source on OBS whenever I press a key on the second (remote) computer.
I have no issue with that key press part. My issue is that I have no idea how to correctly send a request/command to the websocket plugin (it requires JSON format as this is how data is formatted).
Main code (C#)
static void Main(string[] args)
{
using (var ws = new WebSocket("ws://192.168.1.1:1122"))
{
ws.Connect();
ws.OnMessage += (sender, e) =>
Console.WriteLine(e.Data);
ws.Send("REPLACE WITH JSON REQUEST DATA");
Console.ReadKey(true);
}
}
Down below is an example of data received from the websocket whenever I turn on or off a source in OBS.
You can clearly see that the 'visibility' is either false or true.
*This 'item-visible' is the setting I need to mess with to tell OBS to hide or show the source.*
↓ Source is visible in OBS ↓
{
"item-id": 10,
"item-name": "map",
"item-visible": true,
"scene-name": "ONHOLD",
"update-type": "SceneItemVisibilityChanged"
}
↓ Source is hidden in OBS ↓
{
"item-id": 10,
"item-name": "map",
"item-visible": false,
"scene-name": "ONHOLD",
"update-type": "SceneItemVisibilityChanged"
}
You can see example test usages in the official Github repo.
If you look there, you'd see that that e.Data should be parsed. Then, you can assign it as needed, for example:
public string SetIsVisible(MessageEventArgs e, bool isVisible)
{
JObject body = JObject.Parse(e.Data);
body["item-visible"] = isVisible;
return body.ToString();
}
... and then just send the returned string into your websocket:
ws.Send(SetIsVisible(e, isVisible)); // isVisible is boolean you should assign
P.S. If you'd like to work with a static-type DTO, simply generate one (e.g. here):
public partial class OBSSource
{
[JsonProperty("item-id")]
public long ItemId { get; set; }
[JsonProperty("item-name")]
public string ItemName { get; set; }
[JsonProperty("item-visible")]
public bool ItemVisible { get; set; }
[JsonProperty("scene-name")]
public string SceneName { get; set; }
[JsonProperty("update-type")]
public string UpdateType { get; set; }
}
... and then:
public string SetIsVisible(MessageEventArgs e, bool isVisible)
{
OBSSource obsSource = JsonConvert.DeserializeObject<OBSSource>(e.Data);
obsSource.ItemVisible = isVisible;
return JsonConvert.SerializeObject(obsSource);
}
... and then send it serialized into the websocket as above.
this is part of my source code:
var result = DiceWebAPI.PlaceAutomatedBets(
Session, baseBet, guessLow, guessHigh,
betCount > Session.MaxBetBatchSize ? Session.MaxBetBatchSize : betCount,
resetOnWin, resetOnLoss,
increaseOnWin, increaseOnLoss,
maxBet, resetOnMaxLoss, stopOnMaxLoss, stopMaxBalance);
the viusal c# studio 2010 says:
Error 1 No overload for method 'PlaceAutomatedBets' takes 13 arguments D:\Downloads\SampleBot_NET_3_5\SampleBot_NET_Source\Dice.Sample.Bot.3_5\Main.cs 359 30 DiceSampleBot35
I discovered that all arguments of method have definition except the Session one. Can anybody tell me how to write and where to place the definition?
maybe that would help:
in the other file
readonly SessionInfo Session;
and in another one
namespace Dice.Client.Web
{
public sealed class SessionInfo : INotifyPropertyChanged
{
public string AccountCookie { get; }
public long AccountId { get; }
public decimal Balance { get; }
public long BetCount { get; }
public decimal BetPayIn { get; }
public decimal BetPayOut { get; }
public long BetWinCount { get; }
public long ClientSeed { get; }
public string DepositAddress { get; }
public string Email { get; }
public string EmergencyAddress { get; }
public int MaxBetBatchSize { get; }
public string SessionCookie { get; }
public string Username { get; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
UPDATE:
PlaceAutomatedBets.. definition
public static PlaceAutomatedBetsResponse PlaceAutomatedBets(SessionInfo session, AutomatedBetsSettings settings);
You need to go to PlaceAutomatedBets method, and, you will find that your PlaceAutomatedBets only takes 12 arguments, you will need to add the one who is missing.
you are passing extra arguments. Exception is self explanatory. and there is no need to re-define HttpContext.Session Property it's already there in System.Web if you haven't included this just put this line on top of your class Using System.Web;
The problem here is that you are calling a method that has a signature of two parameters:
public static PlaceAutomatedBetsResponse PlaceAutomatedBets(
SessionInfo session,
AutomatedBetsSettings settings);
However, you are calling it with 12:
var result = DiceWebAPI.PlaceAutomatedBets(
Session, baseBet, guessLow, guessHigh,
betCount > Session.MaxBetBatchSize ? Session.MaxBetBatchSize : betCount,
resetOnWin, resetOnLoss,
increaseOnWin, increaseOnLoss,
maxBet, resetOnMaxLoss, stopOnMaxLoss, stopMaxBalance);
C# cannot take your 12 parameters and automatically form them into an object. Instead, you have to build an AutomatedBetsSettings object and pass that in with Session. That's pretty standard in software design, where you want to reduce the number of parameters. The object with all the detail in is often called a context.
For example, you need to do something like:
// I am guessing the content of AutomatedBetsSettings here, you will need
// to check the actual property values.
var betSettingsContext = new AutomatedBetsSettings() {
BaseBet = baseBet,
GuessLow = guessLow,
GuessHigh = guessHigh,
BestCount = betCount > Session.MaxBetBatchSize ? Session.MaxBetBatchSize : betCount,
ResetOnWin = resetOnWin,
ResetOnLoss = resetOnLoss,
IncreaseOnWin = increaseOnWin,
IncreaseOnLoss = increaseOnLoss,
MaxBet = maxBet,
ResetOnMaxLoss = resetOnMaxLoss,
StopOnMaxLoss = stopOnMaxLoss,
StopMaxBalance = stopMaxBalance
};
var result = DiceWebAPI.PlaceAutomatedBets(Session, betSettingsContext);
Try to use like this.
var result = DiceWebAPI.PlaceAutomatedBets(
HttpContext.Current.Session, baseBet, guessLow, guessHigh,
betCount > Session["MaxBetBatchSize"] ? Session["MaxBetBatchSize"] : betCount,
resetOnWin, resetOnLoss,
increaseOnWin, increaseOnLoss,
maxBet, resetOnMaxLoss, stopOnMaxLoss, stopMaxBalance);
I have a problem with my web api application. I get an internal error 500 when i try to post(save) a new user in my db.
The function bellow is the one that i use to make the client call.
public void InsertNewUser(RegisterModel pNewUser, string pEmail)
{
// Build rest uri
string lREST_Uri_Browse = string.Format(#"api/accountapi/saveuserdata"
// User data
/*pModelSerialized*/);
// Complete URI
string lREST_Uri = Helpers_API.endPoint + lREST_Uri_Browse;
var client = new HttpClient();
client.BaseAddress = new Uri(Helpers_API.endPoint);
var newUser = new Models.Models_API.Users
{
Email = pNewUser.Email,
FName = pNewUser.FName,
LName = pNewUser.LName,
Inserted = DateTime.Now,
ActiveAcc = true,
AccType = pNewUser.AccType,
BCompanyID = pNewUser.CompanyID,
PID = pNewUser.PID,
Password = pNewUser.Password,
Token = GetToken(pEmail),
ThirdParty = 0,
Gender = pNewUser.Gender,
BirthDate = pNewUser.BirthDate
};
// Add an Accept header for JSON format.
//client.DefaultRequestHeaders.Accept.Add(
// new MediaTypeWithQualityHeaderValue("application/json"));
// Create the JSON formatter.
MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
// Use the JSON formatter to create the content of the request body.
HttpContent content = new ObjectContent<Models.Models_API.Users>(newUser, jsonFormatter);
var result = client.PostAsync(lREST_Uri_Browse, content).Result;
}
This is the model
public class Users
{
public int BrokerID { get; set; }
public DateTime Inserted { get; set; }
public string Email { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public bool ActiveAcc { get; set; }
public int BCompanyID { get; set; }
public int PID { get; set; }
public int AccType { get; set; }
public string Password { get; set; }
public string Token { get; set; }
public int Gender { get; set; }
public DateTime BirthDate { get; set; }
public int ThirdParty { get; set; }
}
And bellow is the POST in APIController:
public HttpResponseMessage SaveUserData(Users pNewUser)
{
bool createUser = false;
// First check for provided email in DB
Users existingUser = asigCtx.Users.Where(u => u.Email == pNewUser.Email).SingleOrDefault();
if (existingUser == null)
createUser = true;
else if (existingUser.ActiveAcc)
createUser = true;
if (createUser)
{
using (asigCtx = new AsigPrimeContext())
{
Users user = new Users()
{
Email = pNewUser.Email,
FName = pNewUser.FName,
LName = pNewUser.LName,
Inserted = DateTime.Now,
ActiveAcc = true,
AccType = pNewUser.AccType,
BCompanyID = pNewUser.BCompanyID,
PID = pNewUser.PID,
Password = pNewUser.Password,
Token = pNewUser.Token,
ThirdParty = 0,
Gender = pNewUser.Gender,
BirthDate = pNewUser.BirthDate,
};
asigCtx.Users.Add(user);
asigCtx.SaveChanges();
}
}
var response = Request.CreateResponse<Users>(HttpStatusCode.Created, pNewUser);
return response;
}
Can anyone give me piece of advice with this code because i'm new in this and i just want to do it wright. TNX
You have an error in your code. A 500 error indicates that your code contains an unhandled exception that killed its worker process.
Change your web.config file so that your application outputs the full error message.
A few things I would check/try to get to the bottom of the issue:
Is the code above exactly the same as in your application or have you changed anything (even if only to make it simpler)?
Is the Users object used in SaveUserData controller method definitely from the same assembly as the one that you are posting from the InsertNewUser method?
Is the Users object complete on the listing (e.g. does it have any constructors)?
Have you tried executing the request to the endpoint through fiddler? This way you take any potential bugs in the client call out of the equation to see if the controller method on its own works.
I've noticed this line:
string lREST_Uri_Browse = string.Format(#"api/accountapi/saveuserdata"
// User data
/*pModelSerialized*/);
Are you formatting the url and passing any params to it? If so, what are the params and what does your WebApi route look like?
That should be enough for a start - let me know how you get on.
BTW: Two things that strike me in your code (unrelated to the question):
It's very confusing to have a class called 'Users' representing a single user. If it's you're code I'd advise to change that to singular.
the properties on the Users object are using abbreviations - I don't think it's that expensive to spell them out and I can guarantee you that anyone new to this code will be grateful if you put a full name rather than a mysterious BCompanyID, or less mysterious but still hard to read (and write for that matter) FName
I had the same problem a few weeks ago. After doing a few tests, I've realized that my WebApi application was using DataContractJsonSerializer by default, instead of Newtonsoft. To fix it, you can change the default serializer on your server to NewtonSoft or to use DatacontractJsonSerializer to serialize your object and pass it to HttpWebRequest.
I would use jQuery to post some Json to the same URL ($.post(#"api/accountapi/saveuserdata", { Email: "e#example.com", FName = "Vlad" ... })), thus eliminating InsertNewUser from the equation. If you still get a null pNewUser parameter in SaveNewuser, then I would look at your API's route configuration to make sure the server expects SaveUserData(Users). If you get a non-null pNewUser from jQuery, then I would look closer at the output from InsertNewUser. If I had to bet, I would put my money on the routing configuration.
it seemed that i had an error with the LINQ code
Users existingUser = asigCtx.Users.Where(u => u.Email == pNewUser.Email).SingleOrDefault();
After i solved this the request was ok.
Tnx for the help :) Appreciate.
I have a question regarding inheritance, so I will describe the scenario below:
I am reading a text file containing logs. (One log per line)
Each log-line will have the following format:
"Date Type Description"
However, depending on the "Type" of log, I will have to parse the "Description" differently and pull out different fields.
Here are some examples:
5/1/2011 Information Field1, Field2, Field3
5/2/2011 Error Field1
--
So, what I tried to do was this:
-Get a line out of the log
-Parse it according to the pattern "Date Type Description"
-Look at the "Type" field, and create new objects/parse description as necessary
public class Log
{
public DateTime Date;
public String Type;
public String Description;
public Log(String line)
{
this.Date = GetDate();
this.Type = GetType();
this.Description = GetDescription();
}
}
public class InformationLog : Log
{
public String Field1;
public String Field2;
public String Field3;
public InformationLog(Log log)
{
this.Field1 = GetField1(log.Description);
this.Field1 = GetField2(log.Description);
this.Field1 = GetField3(log.Description);
}
}
public class Client
{
public void Main()
{
String line = ReadFileAndGetLine(); // Get a line from the file
Log log = new Log(line);
if(log.Type == "Information")
log = new InformationLog(log); // Is this right?
}
}
This works how I want it to, but it seems like this cannot be a good practice. The "log" variable is using itself as a parameter to its own constructor.
My question is:
Is there a standard way of doing this? Or, is there anything wrong with this implemenation?
--
Edit:
Also, I should mention: My reasoning was that I would parse the line once to get out the date and type, and then parse it again to get the finer details.
I decided to use inheritance so I wouldn't have to parse out the Date and Type fields twice.
Try to use Factory pattern
static class LogFactory
{
public static Log Create(String line)
{
if(GetType(line) == "Information")
return CreateInformationLog(line);
return CreateLog(line);
}
private static Log CreateLog(String line)
{
return new Log(line);
}
private static Log CreateInformationLog(String line)
{
return new InformationLog(line);
}
}
And then try to use
String line = ReadFileAndGetLine(); // Get a line from the file
Log log = LogFactory.Create(line);
As per my comment, why not just do something a little like this:
public enum LogEntryType
{
Error = -1,
Information = 0,
}
public class LogEntry
{
public string Raw;
public DateTime Date;
public LogEntryType Type;
public string Description;
public LogEntry(String line)
{
Raw = line;
Date = ParseDate();
Type = ParseType();
Description = ParseDescription();
}
public string ParseDescription()
{
var result = string.Empty;
switch(Type)
{
case LogEntryType.Error:
//parse here
break;
case LogEntryType.Information:
//parse here
break;
}
return result;
}
}
I notice you have fields in the derivative class, but the description could be parsed here; though, I can see why people may want to shift it to the place that actually knows how the description should be parsed, in which case you could use a factory pattern suggested in another answer, or implement a 'property bag' type scenario - but drifting away from strong typing is generally frowned upon these days, I reckon.
Another suggestion, though very similar to your initial attempt, tends to encapsulate management of the types, as opposed to having a detached class handle such stuff - a pattern a little (superficially) like Exception where you have a root entry and inner entries:
public enum LogEntryType
{
Error = -1,
Information = 0,
}
public class LogEntry
{
public string Raw;
public DateTime Date;
public LogEntryType Type;
public string Description;
public InnerLogEntry InnerEntry;
public LogEntry(String line)
{
Raw = line;
Date = ParseDate();
Type = ParseType();
//parse the 'raw' description...
Description = ParseDescription();
//determine the inner entry type...
switch (Type)
{
case LogEntryType.Error:
InnerEntry = new ErrorLogEntry(this);
break;
case LogEntryType.Information:
InnerEntry = new InformationLogEntry(this);
break;
}
}
}
public abstract class InnerLogEntry
{
protected LogEntry Parent;
public InnerLogEntry(LogEntry logEntry)
{
Parent = logEntry;
}
}
public class InformationLogEntry : InnerLogEntry
{
public InformationLogEntry(LogEntry logEntry)
: base(logEntry)
{
//parse custom data
}
}
public class ErrorLogEntry : InnerLogEntry
{
public ErrorLogEntry(LogEntry logEntry)
: base(logEntry)
{
//parse custom data
}
}