I am learning C #, with this class I get information in JSON from an API, then from the form I call my class "MyClass" to put the values in the corresponding fields, but I do not know how to detect if the connection with the API was unsuccessful, I know that try and catch must be used and that's why I put it in my "MyClass" class but I do not know how to do the verification correctly:
I have these defined classes:
public class Lawyer
{
public string type { get; set; }
public string numdoc { get; set; }
public string name { get; set; }
public string date { get; set; }
}
public class Worker
{
public string time { get; set; }
public string service { get; set; }
}
public class Result
{
public string id { get; set; }
public string name { get; set; }
public string Condition { get; set; }
public string PLE { get; set; }
public List<Lawyer> lawyers { get; set; }
public List<Worker> workers { get; set; }
}
public class RootObject
{
public bool success { get; set; }
public Result result { get; set; }
}
And it is my "MyClass" (I tried using the "try" and the "catch" but I do not know if it is the correct way to do it...):
class MyClass
{
public RootObject MyMethod(int inRUC){
try {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(#"https://www.example.com/api/?get=" + inRUC);
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
{
var json = reader.ReadToEnd();
return JsonConvert.DeserializeObject<RootObject>(json);
}
}
catch (Exception)
{
return null;
}
}
}
This is the section of my form that calls the class, it works fine but I do not know how to verify from here that the connection with the API was successful:
private void Button1_ClickBefore(object sboObject, SAPbouiCOM.SBOItemEventArg pVal, out bool BubbleEvent)
{
if (string.IsNullOrEmpty(txtRuc1.Value.ToString()))
{
BubbleEvent = false;
}
else
{
BubbleEvent = true;
int para = 0;
int.TryParse(txtRuc1.Value, out para);
MyClass obj = new MyClass();
EditText1.Value = obj.MyMethod(para).result.name;
}
}
Do not do this:
catch (Exception)
{
return null;
}
This will simply "swallow" any runtime errors. If the connection fails, obj.MyMethod(para).result.name; will give you a NullpointerException that doesn't tell you what exactly went wrong. Instead take a look at what could possibly go wrong within "MyMethod" and which specific exceptions that would cause. Catch only exceptions that you know how to handle. If you don't (yet) know how to handle a specific exception, don't catch it. This will cause your program to crash with a somewhat meaningful error message. Work your your way from there. Try everything you can come up with to make the connection fail and observe which exceptions are thrown, so you might come up with a way to prevent those situations or recover from them.
Your doubt is right. This is not correct way to handle exception in your case.
You should use WebException which will give you an opportunity to read the response sent by server. Here, you can check details of the errorsome response received from the API and take appropriate action. Below is sample code for your reference. You can tweak it to your need.
catch (WebException e)
{
using (WebResponse response = e.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream data = response.GetResponseStream())
{
string text = new StreamReader(data).ReadToEnd();
Console.WriteLine(text);
}
}
}
You can get more details about WbeException from here: https://learn.microsoft.com/en-us/dotnet/api/system.net.webexception?view=netframework-4.7.2
And some more details with code sample here: https://learn.microsoft.com/en-us/dotnet/framework/network-programming/handling-errors
You should not create a class just for a method. You can put this method aside your UI event handler - if it is used only there. Or create a static helper class for all your API calls - or not static if you are reusing something between calls, like cookies. But then create an instance for the whole UI or at most for the form.
Please note, that API call might succeed technically and you still get an error message back. Check StatusCode property before trying to deserialize. You will most likely get IOException or WebException if there is a network or http level problem. If the server code is faulting or you passed invalid parameters, you will get a status code different than 200 (OK) - there are other success status codes trough. But you can get also content parsing exceptions. Always log such exceptions, even temporarily, but never handle with just noticing it!
But I suggest you using HttpClient class instead with HttpContentExtensions methods. There are many advantages, as this is optimized for API calls and it supports async for all operations. And the later package makes it easy to handle custom objects when reading or sending with any method. Beware, HttpClient instances are optimized for reusability even across threads - thus you can have only one or few in your application. Unfortunately, I haven't found any complete tutorial in this topic to link to.
Related
I googled a lot C# articles how to proceed with that with interceptors. I can divide them on 2 types:
Rethrow RPCException
return default
My problem, i want to return back some common API response object.
public class GrpcResponseBase
{
public int StatusCode { get; set; }
public string ErrorMessage { get; set; }
}
public class GrpcResponse<TData> : GrpcResponseBase
{
public TData Data { get; set; }
...
}
So all the objects i return to client need to be based on that. And the object returned from exception handler too.
But that's the problem. The response objects are autogenerated by protobuf compiler from proto files. Proto doesn't support inheritance, and i don't want to copy-paste those 2-3 fields each time for each "message". And i think it doesn't support generics too.
What can i do ? Maybe don't use interceptors, but use something else ? Please suggest
Here: Recommended ServiceStack API Structure and here: https://github.com/ServiceStack/ServiceStack/wiki/Physical-project-structure are recommendations for how to structure your projects for C# clients to reuse DTOs.
Apparently this is done by including a dll of the DTO assembly. I have searched the web for one example, just Hello World that uses a separate assembly DTO for a C# client in ServiceStack. Perhaps I should be able to break this out myself but so far it has not proven that easy.
Almost all client descriptions are for generic and non-typed JSON or other non-DTO based clients. No one appears interested in typed C# clients like I am (even the ServiceStack documentation I have found). So I thought this would be a good question even if I figure it out myself in the end.
To be clear, I have built and run the Hello World example server. I have also used a browser to attach to the server and interact with it. I have also created a client empty project that can call
JsonServiceClient client = new JsonServiceClient(myURL);
Then I tried to copy over my DTO definition without the assembly DLL as I don't have one. I get ResponseStatus is undefined.
Clearly there is something missing (it appears to be defined in ServiceStack.Interfaces.dll) and if I could create a dll of the DTO I think it would resolve all references.
Can anyone give insight into how to create the DTO assembly for the simple Hello World?
Edited to add code:
using ServiceStack.ServiceClient.Web;
namespace TestServiceStack
{
class HelloClient
{ public class HelloResponse
{
public string Result { get; set; }
public ResponseStatus ResponseStatus { get; set; } //Where Exceptions get auto-serialized
}
//Request DTO
public class Hello
{
public string Name { get; set; }
}
HelloResponse response = client.Get(new Hello { Name = "World!" });
}
}
Where the ResponceStatus is undefined.
I was able to find the missing symbol ResponseStatus by adding:
using ServiceStack.ServiceInterface.ServiceModel;
Here is the full code that built. Keep in mind that I found out something else in the process. Once this built it then failed because I was using a DTO from a .NET 4.0 environment in a .NET 3.5 environment. But that is an unrelated issue. Also note that this test code does nothing with the response, it is just an example to get the build working.
using ServiceStack.ServiceClient;
using ServiceStack.ServiceInterface;
using ServiceStack.Text;
using ServiceStack.Service;
using ServiceStack.ServiceHost;
using ServiceStack.WebHost;
using ServiceStack;
using ServiceStack.ServiceClient.Web;
using RestTestRoot; // This is the name of my DTO assembly. You will need to insert your own here.
using ServiceStack.ServiceInterface.ServiceModel;
namespace WebApplicationRoot
{
class HelloClient
{
JsonServiceClient hello_client;
//Request DTO
public class Hello
{
public string Name { get; set; }
}
//Response DTO
public class HelloResponse
{
public string Result { get; set; }
public ResponseStatus ResponseStatus { get; set; } //Where Exceptions get auto-serialized
}
//Can be called via any endpoint or format, see: http://mono.servicestack.net/ServiceStack.Hello/
public class HelloService : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
//REST Resource DTO
[Route("/todos")]
[Route("/todos/{Ids}")]
public class Todos : IReturn<List<Todo>>
{
public long[] Ids { get; set; }
public Todos(params long[] ids)
{
this.Ids = ids;
}
}
[Route("/todos", "POST")]
[Route("/todos/{Id}", "PUT")]
public class Todo : IReturn<Todo>
{
public long Id { get; set; }
public string Content { get; set; }
public int Order { get; set; }
public bool Done { get; set; }
}
public HelloClient(){
// ServiceStack gateway = new ServiceStack.ClientGateway(
// location.protocol + "//" + location.host + '/ServiceStack.Examples.Host.Web/ServiceStack/');
hello_client = new JsonServiceClient("http://tradetree2.dnsapi.info:8080/");
hello_client.Get<HelloResponse>("/hello/MyTestWorld!");
}
}
}
I currently have a method to save a user.
public PersonDto Save(PersonDto personDto)
However, if, while saving, I find there is a duplicate username, or some other issue with the data - my only way to respond to this is to throw an exception.
throw new Exception("Username exists");
I have read that exceptions shouldn't be used for 'Business Requirement Transgressions'.
Is there a better way to return results to my calling methods? I need to return to PersonDto, but also, some information about any issues. Is there a common practice or model for doing this? Maybe return a 'SaveResult' object, which contains a Object SavedObject (in this case, my PersonDto), as well as some other properties like string SaveResult and bool Success?
Maybe implement a generic wrapper for use with all such requests.
enum Disposition
{
OK,
Warning,
Error
}
class Response<T>
{
public T Result { get; set; }
public Disposition Disposition { get; set; }
public string Message { get; set; }
}
i.e.:
public Response<PersonDto> Save(PersonDto personDto)
This way you can specify some metadata for each of your return values.
I recommend using a SaveResult approach to your problem e.g.
public class SaveResult
{
public PersonDto { get; set; }
public bool Success { get; set; }
public string ErrorMessage { get; set; }
}
var result = Save(person);
if (!result.Success)
{
Console.WriteLine(result.ErrorMessage);
}
Or something like that. This will allow you to pass back the PersonDto, but also signal to the API caller that an error occurred when trying to save.
There's nothing really wrong with throwing an exception, but as you also want to return the person DTO, then you might as well use a specific method return type for that purpose.
I have created a custom retrieve entity response class for crm2011 so as to serialize the class. The entity response class is derived from OrganizationRequest class. Its as shown below:
public partial class RetrieveEntityRequest : OrganizationRequest
{
public RetrieveEntityRequest()
{
}
private System.Guid metadataIdField;
public System.Guid MetadataId
{
get
{
return this.metadataIdField;
}
set
{
this.metadataIdField = value;
}
}
public EntityFilters EntityFilters { get; set; }
public string LogicalName { get; set; }
public bool RetrieveAsIfPublished { get; set; }
}
Now when i run the code shown below
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
{
try
{
serviceProxy.EnableProxyTypes();
request = new CrmUtilities.RetrieveEntityRequest();
request.LogicalName=entityName;
request.EntityFilters = EntityFilters.Entity;
request.RequestName = requestName;
//Execute Request
retrieveEntityResponse = (CrmUtilities.RetrieveEntityResponse)serviceProxy.Execute(request);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
It says that MetadataId which is a required field is missing.The exception thrown is OrganizationServiceFault was caught //Required field 'MetadataId' is missing.
How do i create a metadataId for this custom object in this case?
Check out the MSDN documentation for OrganizationRequest. One of the properties is Parameters, which is a collection of all the data needed for a request to work.
Your getter and setter should set (or retrieve) the value from that collection. You can't just create a private field and expect it to work. ;)
For the record - all the other request classes available in CRM SDK follow the same pattern - they derive from OrganizationRequest and the extra properties are just shortcuts to manipulate the required Parameters.
Just guessing based on the exception your getting, because I don't know crm2011. But the exception is saying that the field is missing, what you have is a property. Whilst the difference may seem trivial there is a big difference when using reflection.
What you might need to do is:
public Guid MetadataId;
And remove your property.
I've had a look at a few threads but what I'm aiming for I can't seem to find.
I have the following JSON strings returned:
On success:
{"success":{"username":"key"}}
On Error:
{"error":{"type":101,"address":"/","description":"link button not pressed"}}
I need to be able to de-serialize these into a class and determine whether I've got an error or a success message to carry on doing it. Any ideas on how to achieve this?
thanks,
Adam
No need to declare a lot of tiny classes. dynamic keyword can help here.
dynamic jObj = JObject.Parse(json);
if (jObj.error!= null)
{
string error = jObj.error.description.ToString();
}
else
{
string key = jObj.success.username.ToString();
}
One option is to use http://nuget.org/packages/newtonsoft.json - you can either create your own custom class to deserialize into or use dynamic as the target type.
var result = JsonConvert.DeserializeObject<Result>(jsonString);
class Result
{
public SuccessResult success { get; set; }
public ErrorResult error { get; set; }
}
class SuccessResult
{
public string username { get; set; }
}
class ErrorResult
{
public int type { get; set; }
public string address { get; set; }
public string description { get; set; }
}
If you need just to check for success, it is possible to just check result.StartsWith("{\"success\":") to avoid unnecessary parsing. But this should only be done if you have guarantee that the JSON string will always be exactly like this (no extra whitespaces etc.) - so it is usually only appropriate if you own the JSON generation yourself.
This answer covers most options, including rolling your own parser and using JSON.Net:
Parse JSON in C#
You could also just write a regex if the format is going to be that simple...