Not getting Custom Error Message in REST - asp.net

I am developing a REST API.
Below is my code:
In IService.vb
<OperationContract(),
WebGet(UriTemplate:="/DCU/{ClientID}",
RequestFormat:=WebMessageFormat.Xml,
ResponseFormat:=WebMessageFormat.Xml,
BodyStyle:=WebMessageBodyStyle.Bare)>
Function GetAllData(ByVal ClientID As String) As List(Of Data)
In Service.vb
Friend Class Service
Implements IService
Public Function GetAllData(ByVal intClientID As String) As System.Collections.Generic.List(Of Data) Implements ILightingGaleService.GetAllDCUs
Dim lstdata As New List(Of Data)()
If IsNumeric(intClientID) Then
Else
Throw New FaultException("Invalid Client ID")
End If
End Class
In Web.Config file
<serviceDebug includeExceptionDetailInFaults="true"/>
I am getting
Request Error:The server encountered an error processing the request. See server logs for more details.
When I am passing a String value in Client ID. Though I have implemented FaultExpcetion i never get "Invalid Client ID" message in POSTMAN or Web Client.
Can anyone help me on how can I get my custom message on POSTMAN or web client?

I was able to resolve this by implementing below:
I generated a class CustomError:
<DataContract> _
Public Class CustomError
Public Sub New(strError As String, strErrorDesc As String, strErrorCode As Long)
ErrorInfo = strError
ErrorDetails = strErrorDesc
ErroCode = strErrorCode
End Sub
<DataMember> _
Public Property ErrorInfo() As String
Get
Return m_ErrorInfo
End Get
Private Set(value As String)
m_ErrorInfo = value
End Set
End Property
Private m_ErrorInfo As String
<DataMember> _
Public Property ErrorDetails() As String
Get
Return m_ErrorDetails
End Get
Private Set(value As String)
m_ErrorDetails = value
End Set
End Property
Private m_ErrorDetails As String
<DataMember> _
Public Property ErroCode() As Long
Get
Return m_ErroCode
End Get
Private Set(value As Long)
m_ErroCode = value
End Set
End Property
Private m_ErroCode As Long
End Class
In Service.vb
Friend Class Service
Implements IService
Public Function GetAllData(ByVal intClientID As String) As System.Collections.Generic.List(Of Data) Implements ILightingGaleService.GetAllDCUs
Dim lstdata As New List(Of Data)()
If IsNumeric(intClientID) Then
Else
Dim ExceptionError As New CustomError("Client ID not found.", "The Client ID is not found in system.", HttpStatusCode.NotFound)
Throw New WebFaultException(Of CustomError)(ExceptionError, HttpStatusCode.NotFound)
End If
End Class
So this will generate a Webexception for my Custom Error Message which I wanted to display on my Client Application:
Now on my client application I used below
Catch ex As WebException
Dim strError = New StreamReader(ex.Response.GetResponseStream()).ReadToEnd()
lblErrorMsg.Text = strError
End Try
Hope this helps to someone.

Related

System.NullReferenceException on remote Machine?

I have created a web site with window form, I have an error of System.NullReferenceException only in remote page, if I try to open the page with visual studio, in debug mode, there is not problems! WHY!!?!?!?!
[NullReferenceException: Riferimento a un oggetto non impostato su un'istanza di oggetto.]
GeCo.DetaglioMilitare.Page_Load(Object sender, EventArgs e) in C:\Users\j972537\Documents\
Visual Studio 2010\Projects\GeCo\Admin\DettaglioMilitare.aspx.vb:45
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(
Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
this is the code with the error:
Dim militare As New ldap.utente
militare = ldap.ldap_utente(matricola2)
Label7.Text = militare.grado
Label8.Text = militare.cognome
Label9.Text = militare.nome
Label10.Text = militare.codice_reparto
Label11.Text = militare.intestazione_reparto
Label12.Text = militare.email
this is the class module that I created:
Public Class ldap
Public Class utente
Private _matricola As String = ""
Public Property matricola() As String
Get
Return _matricola
End Get
Set(ByVal val As String)
_matricola = val
End Set
End Property
Private _grado As String = ""
Public Property grado() As String
Get
Return _grado
End Get
Set(ByVal val As String)
_grado = val
End Set
End Property
Private _cognome As String = ""
Public Property cognome() As String
Get
Return _cognome
End Get
Set(ByVal val As String)
_cognome = val
End Set
End Property
Private _nome As String = ""
Public Property nome() As String
Get
Return _nome
End Get
Set(ByVal val As String)
_nome = val
End Set
End Property
Private _codice_reparto As String = ""
Public Property codice_reparto() As String
Get
Return _codice_reparto
End Get
Set(ByVal val As String)
_codice_reparto = val
End Set
End Property
Private _intestazione_reparto As String = ""
Public Property intestazione_reparto() As String
Get
Return _intestazione_reparto
End Get
Set(ByVal val As String)
_intestazione_reparto = val
End Set
End Property
Private _email As String = ""
Public Property email() As String
Get
Return _email
End Get
Set(ByVal val As String)
_email = val
End Set
End Property
End Class
' funzione per estrarre i dati relativi ad una matricola
Public Shared Function ldap_utente(matricola As String) As utente
Dim directory As DirectoryServices.DirectorySearcher
Dim result As DirectoryServices.SearchResult
Dim militare As utente = New utente
Try
directory = New DirectoryServices.DirectorySearcher("(cn=" & matricola & ")")
result = directory.FindOne
militare.matricola = matricola
militare.grado = result.Properties("title")(0).ToString
militare.cognome = result.Properties("sn")(0).ToString
militare.nome = result.Properties("givenname")(0).ToString
militare.codice_reparto = Left(result.Properties("physicaldeliveryofficename")(0).ToString, 5)
Dim lenght_string As Integer = result.Properties("description")(0).ToString.Length
militare.intestazione_reparto = Trim(Right(result.Properties("description")(0).ToString, lenght_string - 7))
militare.email = result.Properties("mail")(0).ToString()
Return militare
Catch ex As Exception
Return Nothing
End Try
End Function
Because you did not include information which line is DettaglioMilitare.aspx.vb:45, I can only diagnose what probably happened.
In you function ldap_utente when wxception happened you return Nothing which is same as Null. So to investigate what is wrong in your code you have two options:
Debug your code to see what exception you got
If debugging is not possible add logging to your code or throw this exception to see it in browser window\
I am almost sure that you have problem with connecting to Active Directory, because in 99% your page is installed as IIS user which doesn't have access to Active Directory. You can try to change application pool user to domain user.

ASP.Net Accessing Object Properties in Control Attribute

I've two classes as these
Namespace Business
Public Class Core
Public Shared ReadOnly Property Settings As DBManager.Settings
Get
Return DBManager.Settings.GetSettings
End Get
End Property
End Class
End Namespace
Namespace DBManager
Public Class Settings
Private _Name As String
Public Property Name As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _Title As String
Public Property Title As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Public Shared Function GetSettings() As Settings
Return New Settings With {.Name = "Website", .Title = "My Product Site"}
End Function
End Class
End Namespace
Now, I wish to create a DataBound Label control with a property name as DataProperty where I can pass the full path of the property name.
Namespace Application.Controls
Public Class ExtendedLabel
Inherits Label
Public Property DataProperty As String
Get
If ViewState("DataProperty") Is Nothing Then
Return String.Empty
Else
Return CStr(ViewState("DataProperty"))
End If
End Get
Set(ByVal value As String)
ViewState("DataProperty") = value
End Set
End Property
Private Sub ExtendedLabel_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(DataProperty) Then
Me.Text = GetReflectedValue()
End If
End Sub
Private Function GetReflectedValue() As String
//'Need suggestion here
End Function
End Class
End Namespace
usage will be something like this
<cc:ExtendedLabel id="elName" runat="server" DataProperty="Business.Core.Settings.Name" />
Kindly suggest a way to access this value using Reflection.
Just to clarify, I want to be able to access any property in any class of any namespace, static or instantiated. Therefore I cannot use a declarative format as given in
get value of a property by its path in asp.net
Get property Value by its stringy name

The specified string is not in the form required for an e-mail address

i have a problem in this code
Imports Microsoft.VisualBasic
Imports System.Net.Mail
Public Class SendEmail
Private _Mailto As String = ""
Public Property Mailto() As String
Get
Return _Mailto
End Get
Set(ByVal value As String)
_Mailto = value
End Set
End Property
Private _MailSub As String = ""
Public Property MailSub As String
Get
Return _MailSub
End Get
Set(ByVal value As String)
_MailSub = value
End Set
End Property
Private _MailBody As String = ""
Public Property MailBody As String
Get
Return _MailBody
End Get
Set(ByVal value As String)
_MailBody = value
End Set
End Property
Private _Msg As String = ""
Public ReadOnly Property Msg As String
Get
Return _Msg
End Get
End Property
Public Sub email()
Try
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient("smtp.gmail.com")
mail.From = New MailAddress("email#gmail.com")
mail.[To].Add(_Mailto)
mail.Subject = _MailSub
mail.Body = _MailBody
' mail.Headers.Add("In-Reply-To", 1)
SmtpServer.Port = 587
SmtpServer.Credentials = New System.Net.NetworkCredential("email#gamil.com", "password")
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
_Msg = " Check Your Mail "
Catch ex As Exception
_Msg = ex.Message
End Try
End Sub
End Class
and the exception is :The specified string is not in the form required for an e-mail address.: any soultion??
It looks like you're initializing _MailTo to an empty string (""), then setting the "To" email address to that value. Unless you're setting the value of that field to some valid email address, the email sender won't recognize an empty string as a valid email address.
(If you are setting the MailTo property from outside the given code, you'll need to provide code to show where you're setting it and what the value is.)

Consume ASMX Webservice From Classic ASP Using SOAP Client 3.0

I made a webservice in VB.Net with a method returning a custom class or object.
<WebMethod()> _
Public Function CreatePerson(ByVal LastName As String, ByVal FirstName As String) As Person
Return New Person(LastName, FirstName)
End Function
Public Class Person
Public Sub New()
End Sub
Public Sub New(ByVal LastName As String, ByVal FirstName As String)
_LastName = LastName
_FirstName = FirstName
End Sub
Private _LastName As String
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
Private _FirstName As String
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName= value
End Set
End Property
End Class
I can consume it from another ASP.NET Application but the problem is when i try to consume it from Classic ASP through SOAP Client 3.0
<%
Dim Result, oSoapClient
Set oSoapClient = Server.CreateObject("MSSOAP.SoapClient30")
oSoapClient.ClientProperty("ServerHTTPRequest") = True
Call oSoapClient.mssoapinit ("http://MyServer/MyWebService/MyWebService.asmx?WSDL")
Result = oSoapClient.CreatePerson("Sassaroli", "Rinaldo")
Response.Write(Result.LastName)
%>
I get an error:
Microsoft VBScript runtime error '800a01a8'
Object required
at "Response.Write(Result.LastName)" Line.
I can see Result is a string array with no elements
I believe that the root cause of the error is the lack of a Set keyword on the line that invokes the web service method. It should look like:
Set Result = oSoapClient.CreatePerson("Sassaroli", "Rinaldo")
That will get you past your initial problem. After that you'll need to read the result object. Your subsequent line of code:
Response.Write(Result.LastName)
will most likely result in another error. That's because the result you're getting is not really a "Person" object, it's an XML representation of that object. You can verify this with the following code:
<%
Dim Result, oSoapClient
Set oSoapClient = Server.CreateObject("MSSOAP.SoapClient30")
oSoapClient.ClientProperty("ServerHTTPRequest") = True
Call oSoapClient.mssoapinit ("http://MyServer/MyWebService/MyWebService.asmx?WSDL")
Set Result = oSoapClient.CreatePerson("Sassaroli", "Rinaldo")
Response.Write( TypeName( Result ) & "<br/>" & vbCrLf )
Response.Write( Result.item(0).text )
%>
The type that will be shown by the TypeName call will be IXMLDomSelection. You can access nodes for this object through methods and properties that are documented here.
The last line of code will display the value of the LastName property.
Hope this leads you in the correct direction.

WCF - Can I use an existing type to be passed through my WCF service

I have a service. I have an existing class of business objects. What I would like to know is how can I pass a class through WCF from the business object assembly without having to create a new class in my WCF site while appending or tags?
Here is an existing UDT:
Namespace example: Application.BusinessObjects.Appointments
Public Structure AppointmentResource
Private _id As String
Private _type As ResourceTypeOption
Private _name As String
Property id() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
Property type() As ResourceTypeOption
Get
Return CType(_type, Int32)
End Get
Set(ByVal value As ResourceTypeOption)
_type = value
End Set
End Property
Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Sub New(ByVal id As String, ByVal type As ResourceTypeOption, ByVal name As String)
_id = id
_type = type
_name = name
End Sub
End Structure
Here is the same one I created with the data contract attributes:
Namespace example: Application.Service.Appointments
<DataContract()> _
Public Structure AppointmentResource
Private _id As String
Private _type As ResourceTypeOption
Private _name As String
<DataMember()> _
Property id() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
<DataMember()> _
Property type() As ResourceTypeOption
Get
Return CType(_type, Int32)
End Get
Set(ByVal value As ResourceTypeOption)
_type = value
End Set
End Property
<DataMember()> _
Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Sub New(ByVal id As String, ByVal type As ResourceTypeOption, ByVal name As String)
_id = id
_type = type
_name = name
End Sub
End Structure
There is an easy way to share types between client and service, just by adding reference to shared type assembly to your client BEFORE adding the service reference.
You can find the detailed scenario and sample project there:
http://blog.walteralmeida.com/2010/08/wcf-tips-and-tricks-share-types-between-server-and-client.html
ResourceTypeOption also appears to be a custom class, so you would to define that as part of the contract in its own class. The client has to know about that and so it needs its own contract. Clients already know how to deal with CLR types like string. Any other custom types would also have to be defined in the contract.

Resources