Change soap prefix to soapenv in .NET Web Service - asp.net

I'm working on a legacy web service that was firstly developed in Java using Axis, which its response was:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:TransaccionResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
<TransaccionReturn xsi:type="xsd:string"><!-- info --></TransaccionReturn>
</ns1:TransaccionResponse>
</soapenv:Body>
</soapenv:Envelope>
And I'm making a .NET Web Service that should be compatible with all current clients, but until now I have:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ns1:TransaccionResponse xmlns:ns1="http://DefaultNamespace">
<TransaccionReturn><!-- info --></TransaccionReturn>
</ns1:TransaccionResponse>
</soap:Body>
</soap:Envelope>
I started with an old ASP.NET Web Service project and I'm wondering if there is a way to replace the soap prefix to soapenv? Also is there any way to force the web service to add the xsi:type declaration?

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Services.Description
Imports System.Xml.Serialization
Imports System.IO
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class ExpedientesService
Inherits System.Web.Services.WebService
Public Sub New()
MyBase.New()
End Sub
<WebMethod()> _
<SoapDocumentMethod("", _
RequestNamespace:="http://DefaultNamespace", _
ResponseNamespace:="http://DefaultNamespace", _
ParameterStyle:=SoapParameterStyle.Bare)> _
Public Function llamarWS( _
<XmlElement("Transaccion", Namespace:="http://DefaultNamespace")> ByVal tr As Transaccion) As _
<XmlElement("TransaccionResponse")> _
RespuestaXML
Return New RespuestaXML(String.format("You sended: '{0}' '{1}' '{2}'", tr.transaccion, tr.usuario, tr.password))
End Function
End Class
'HERE THERE IS A CLASS DECLARATION FOR THE INPUT PARAMETERS OF THE WEB SERVICE
Public Class Transaccion
'CHECK THE DECLARATION OF THE XML NODE AND ITS NAMESPACE
<XmlElement("transaccion", Namespace:="")> _
Public transaccion As String
<XmlElement("usuario", Namespace:="")> _
Public usuario As String
<XmlElement("password", Namespace:="")> _
Public password As String
Public Sub New()
Me.transaccion = "0"
Me.usuario = String.Empty
Me.password = String.Empty
End Sub
Public Sub New(ByVal transaccion As String, ByVal usuario As String, ByVal password As String)
Me.transaccion = transaccion
Me.usuario = usuario
Me.password = password
End Sub
'HERE YOU DECLARE THE NAMESPACES FOR THE XML ELEMENT
<XmlNamespaceDeclarations()> _
Public Property xmlns() As XmlSerializerNamespaces
Get
Dim xsn As New XmlSerializerNamespaces()
xsn.Add("def", "http://DefaultNamespace")
Return xsn
End Get
' needed for xml serialization
Set(ByVal value As XmlSerializerNamespaces)
End Set
End Property
End Class
'HERE THERE IS A CLASS DECLARATION FOR THE OUTPUT RESPONSE
Public Class RespuestaXML
'THIS IS THE SAME AS THE INPUT PARAMETER, THE NODE NAME AND ITS NAMESPACE
<XmlElement("TransaccionReturn", Namespace:="")> _
Public Body As String
Public Sub New()
Me.Body = "##"
End Sub
Public Sub New(ByVal StringReturn As String)
Me.Body = StringReturn
End Sub
'HERE IS THE TRICK, DECLARE THE NAMESPACES FOR THE RESPONSE
<XmlNamespaceDeclarations()> _
Public Property xmlns() As XmlSerializerNamespaces
Get
Dim xsn As New XmlSerializerNamespaces()
xsn.Add("ns1", "http://DefaultNamespace")
Return xsn
End Get
' needed for xml serialization
Set(ByVal value As XmlSerializerNamespaces)
End Set
End Property
End Class

Related

ASP.NET WebForms - VB.NET and SignalR

This is my Hub code (very simple):
Imports System
Imports System.Web
Imports Microsoft.AspNet.SignalR
Imports Microsoft.AspNet.SignalR.Hubs
Imports Microsoft.AspNet.SignalR.Client
Imports Microsoft.AspNet.SignalR.Messaging
Imports System.Threading.Tasks
Namespace SignalRChat
Public Class ChatHub
Inherits Hub
Public Sub Send(userName As String, message As String)
Clients.All.broadcastMessage(userName, message)
End Sub
End Class
End Namespace
This is my Aspx page code:
Imports System.Web.UI.WebControls
Imports Microsoft.AspNet.SignalR.Client
Imports System.Threading.Tasks
Public Class WebForm9
Inherits System.Web.UI.Page
Public Shared hubConnection As HubConnection
Public Shared chatHubProxy As IHubProxy
Public Sub MyChat_init(sender As Object, e As EventArgs) Handles Me.Init
If IsPostBack = False Then
hubConnection = New HubConnection("https://localhost:44343/")
hubConnection.TraceLevel = TraceLevels.All
hubConnection.TraceWriter = Console.Out
chatHubProxy = hubConnection.CreateHubProxy("ChatHub")
hubConnection.Start().Wait()
End If
chatHubProxy.On(Of String, String)("broadcastMessage", Sub(ByVal userName As String, ByVal message As String)
Dim li As ListItem = New ListItem
li.Value = userName & " - " & message
li.Text = userName & " - " & message
ListBox1.Items.Add(li)
End Sub)
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
chatHubProxy.Invoke("Send", "Io", "Messaggio")
End Sub
End Class
I made a lot of tries but always ended up with no result... I added the postback checking because I noticed I was having the connection to the hub starting and starting again on each button_click...
By the way, if I add in the same project a page with JScript code I can catch all the messages sent on the JScript code, but none of the messages sent from the html page is catched by the aspx codebehind...
It's really strange because if I take away the listbox.items.add method and I put a "MsgBox" instead, then it fires up and work... but I have found no way to manage the "messages" from my codebehind and so update controls on my page... Maybe it's a connection mistake? Did anyone of you has any experience with SignalR and WebForms with VB.NET codebehind?
If this helps, I have a working client code (for testing purposes) in VB.NET WinForms app. (My Hub is in C#):
Hub (ASP.NET Core in net5.0 - created using Gerald Versluis' tutorial: https://www.youtube.com/watch?v=pDr0Hx67guk):
using Microsoft.AspNetCore.SignalR;
using System;
using System.Threading.Tasks;
namespace SignalR.Hubs;
public class OneHub : Hub
{
public async Task SendMessage(Message message)
{
Console.WriteLine($"{message.SentDateTime} Sender : {message.SenderId} - {message.MessageText}");
await Clients.All.SendAsync("MessageReceived", message);
}
}
WinForms (net4.8):
Imports Microsoft.AspNetCore.SignalR.Client
Imports SignalRWinForms.Client.Messaging.Models
Public Class Form1
Private connection As HubConnection
Sub New()
InitializeComponent()
connection = New HubConnectionBuilder().WithUrl("http://192.168.1.230:5296/chat").Build()
connection.On(Of Messages)("MessageReceived", Sub(Messages)
Invoke(Sub()
ReceiveMessage(Messages)
End Sub)
End Sub)
connection.StartAsync()
End Sub
Private Sub ReceiveMessage(msg As Messages)
chatMessages.Text &= $"{Environment.NewLine}{msg.MessageText}"
End Sub
Private Async Sub btnSendMessage_Click(sender As Object, e As EventArgs) Handles btnSendMessage.Click
Dim message = New Messages With {
.MessageText = txtMessage.Text,
.SenderId = 1111,
.ReceiverId = 2222,
.Token = "token",
.SentDateTime = DateTime.Now
}
Await connection.InvokeCoreAsync("SendMessage", args:={message})
txtMessage.Text = String.Empty
End Sub
End Class
Form1 is very simple - chatMessages label to populate messages, txtMessage textbox to write message and a btnSendMessage button.
Messages Class (common for both projects)
Public Class Messages
Public Property SenderId As Integer
Public Property ReceiverId As Integer
Public Property MessageText As String
Public Property Token As String
Public Property SentDateTime As DateTime
End Class

How to convert returned asmx class in aspx

I need some pointers with understanding how to convert a returned asmx class from within aspx codebehind. I created a prototype asmx and aspx pages to test this functionality that once sucessfully working I'd like to extend to a project I'm working on.
Although I'm using the same class definition within the asmx and aspx vb codebehind, visual studio is noting a conversion incompatiability error "Error BC30311 Value of type 'websvc_returnvalues' cannot be converted to 'WebServiceConsume.websvc_returnvalues'". This error is denoted in visual studio on the following line in aspx.vb:
rtnvals = websvc.test()
I tried doing a simple type conversion but it has the same kind of error: Unable to cast object of type 'websvctest.websvc_returnvalues' to type 'websvc_returnvalues' ... so obviously I'm not understanding how to convert between the two classes.
Private Function cvt_websvc_returnvalues(i As Object) As websvc_returnvalues
Return CType(i, websvc_returnvalues)
End Function
Thanks in advance for any suggestions I can try! Stackoverflow is my primary source for answering my software questions!
Webservice:
I have the following webservice referenced as websvctest in my project:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://sample.org/")>
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebServiceTest
Inherits System.Web.Services.WebService
<Serializable()>
Public Class websvc_returnvalues
Public w_brtn As Boolean
Public w_rtnval As String
Public w_rtnerr As String
Sub New()
w_brtn = False
w_rtnval = ""
w_rtnerr = ""
End Sub
Public Property Ok As Boolean
Get
Return w_brtn
End Get
Set(value As Boolean)
w_brtn = value
End Set
End Property
Public Property value As String
Get
Return w_rtnval
End Get
Set(value As String)
w_rtnval = value
End Set
End Property
Public Property err As String
Get
Return w_rtnerr
End Get
Set(value As String)
w_rtnerr = value
End Set
End Property
End Class
Public Sub New()
End Sub
<WebMethod()>
Public Function test() As websvc_returnvalues
Dim b As Boolean = False
Dim rtn As websvc_returnvalues = New websvc_returnvalues
Try
b = True
Catch ex As Exception
rtn.err = ex.Message
End Try
rtn.Ok = b
Return rtn
End Function
End Class
WebServiceConsume.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="WebServiceTestConsume.aspx.vb" Inherits="WebServiceConsume" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<label for="websvc_ok">ok</label><asp:Literal ID="websvc_ok" runat="server"></asp:Literal><br />
<label for="websvc_value">value</label><asp:Literal ID="websvc_value" runat="server"></asp:Literal><br />
<label for="websvc_err">err</label><asp:Literal ID="websvc_err" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
WebServiceconsume.aspx.vb
Note the same class definition for websvc_returnvalues here as in the asmx
Partial Class WebServiceConsume
Inherits System.Web.UI.Page
Private websvc As New websvctest.WebServiceTest
Public Class websvc_returnvalues
Public w_brtn As Boolean
Public w_rtnval As String
Public w_rtnerr As String
Sub New()
w_brtn = False
w_rtnval = ""
w_rtnerr = ""
End Sub
Public Property Ok As Boolean
Get
Return w_brtn
End Get
Set(value As Boolean)
w_brtn = value
End Set
End Property
Public Property value As String
Get
Return w_rtnval
End Get
Set(value As String)
w_rtnval = value
End Set
End Property
Public Property err As String
Get
Return w_rtnerr
End Get
Set(value As String)
w_rtnerr = value
End Set
End Property
End Class
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
Dim rtnvals As websvc_returnvalues
Try
rtnvals = websvc.test() ' visual studio error
rtnvals = cvt_websvc_returnvalues(websvc.test()) ' runtime error
Me.websvc_ok.Text = rtnvals.Ok.ToString
simp Me.websvc_value.Text = rtnvals.value.ToString
Me.websvc_err.Text = rtnvals.err.ToString
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Testing")
End Try
End Sub
Private Function cvt_websvc_returnvalues(i As Object) As websvc_returnvalues
Return CType(i, websvc_returnvalues)
End Function
End Class
Doh! I can answer my own question ...
I just needed to type in the correct namespace against the websvc_returnvalues class:
Dim rtnvals As websvctest.websvc_returnvalues

How to select a connection string dynamically from the config file from a session variable?

What am trying to create is a single login screen that connects the users to different databases depending on a certain code that each user has.
i have created some keys in my config file which corresponds to the user codes as follows
<appSettings>
<add key="ch001" value="h001"/>
<add key="ch002" value="h002"/>
</appSettings>
Then i have created connections string as follows
<connectionStrings>
<add name="Dbconn_h001" connectionString="XXX" providerName="XXX"/>
<add name="Dbconn_h002" connectionString="XXX" providerName="XXX"/>
</connectionStrings>
Then i have created a class to get the key value corresponding to the connection string as follows
Imports System.Web.Compilation
Imports System.CodeDom
Imports System.ComponentModel
Public Class ConnStringExpressionBuilder
Inherits ExpressionBuilder
Public Shared Function GetEvalData(ByVal expression As String, ByVal target As Type, ByVal entry As String) As Object
Return System.Configuration.ConfigurationManager.ConnectionStrings("Dbconn_" & System.Configuration.ConfigurationManager.AppSettings(HttpContext.Current.Session("code").ToString))
End Function
Public Overrides Function GetCodeExpression(ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, ByVal context As ExpressionBuilderContext) As CodeExpression
Dim type1 As Type = entry.DeclaringType
Dim descriptor1 As PropertyDescriptor = TypeDescriptor.GetProperties(type1)(entry.PropertyInfo.Name)
Dim expressionArray1(2) As CodeExpression
expressionArray1(0) = New CodePrimitiveExpression(entry.Expression.Trim())
expressionArray1(1) = New CodeTypeOfExpression(type1)
expressionArray1(2) = New CodePrimitiveExpression(entry.Name)
Return New CodeCastExpression(descriptor1.PropertyType, New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType()), "GetEvalData", expressionArray1))
End Function
End Class
The issue is
System.Configuration.ConfigurationManager.AppSettings(HttpContext.Current.Session("code").ToString)
returns a null reference
using(SqlConnection conn = new SqlConnection())
{
var connString=ConfigurationManager.AppSetting["keyname"];
conn.ConnectionString = connString;
// using the code here...
}
and in config file save like
<add key="ch001" value="YourConnectionString" />
After a long hustle i figured it out i created this expression builder class
Public Class ConnStringExpressionBuilder
Inherits ExpressionBuilder
Public Shared Function GetEvalData(ByVal expression As String, ByVal target As Type, ByVal entry As String) As Object
Return System.Configuration.ConfigurationManager.ConnectionStrings(System.Configuration.ConfigurationManager.AppSettings(HttpContext.Current.Session("code").ToString())).ToString()
End Function
Public Overrides Function GetCodeExpression(ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, ByVal context As ExpressionBuilderContext) As CodeExpression
Dim type1 As Type = entry.DeclaringType
Dim descriptor1 As PropertyDescriptor = TypeDescriptor.GetProperties(type1)(entry.PropertyInfo.Name)
Dim expressionArray1(2) As CodeExpression
expressionArray1(0) = New CodePrimitiveExpression(entry.Expression.Trim())
expressionArray1(1) = New CodeTypeOfExpression(type1)
expressionArray1(2) = New CodePrimitiveExpression(entry.Name)
Return New CodeCastExpression(descriptor1.PropertyType, New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType()), "GetEvalData", expressionArray1))
End Function
End Class
Then in my markup i call the class like this
<asp:SqlDataSource ID="Ds" runat="server" ProviderName="Mysql.Data.MysqlClient"
ConnectionString="<%$ ConnStringExpression:Dbconn %>" SelectCommand="XXX"></asp:SqlDataSource>
Then from the code behind
Using conn = getConnect(System.Configuration.ConfigurationManager.AppSettings(Session("code").ToString()))
conn.Open()
Try
//logic
Catch ex As Exception
End Try
conn.Close()
End Using

Google reCaptcha V2 Implementation VB.net

Having a hard time getting reCaptcha to validate on my site :(
I have tried to find other sources for VB.net implementations, but haven't had much luck. Here is what I have tried...
default.aspx.vb
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Web.Script.Serialization
Public Class _Default
Inherits System.Web.UI.Page
Sub reCaptcha_Click(ByVal sender As Object, ByVal e As EventArgs)
If (capValidate()) Then
MsgBox("Valid Recaptcha")
Else
MsgBox("Not Valid Recaptcha")
End If
End Sub
Public Function capValidate() As Boolean
Dim Response As String = Request("g-captcha-response")
Dim Valid As Boolean = False
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(Convert.ToString("https://www.google.com/recaptcha/api/siteverify?secret=THIS IS WHERE MY KEY IS&response=") & Response), HttpWebRequest)
Try
Using wResponse As WebResponse = req.GetResponse()
Using readStream As New StreamReader(wResponse.GetResponseStream())
Dim jsonResponse As String = readStream.ReadToEnd()
Dim js As New JavaScriptSerializer()
Dim data As MyObject = js.Deserialize(Of MyObject)(jsonResponse)
Valid = Convert.ToBoolean(data.success)
Return Valid
End Using
End Using
Catch ex As Exception
Return False
End Try
End Function
Public Class MyObject
Public Property success() As String
Get
Return m_success
End Get
Set(value As String)
m_success = Value
End Set
End Property
Private m_success As String
End Class
And my front page...
<div class="g-recaptcha"
data-sitekey="THIS IS WHERE MY SITE KEY IS"></div>
<asp:Button ID="btnLogin" CssClass="captcha_click" runat="server" Text="Check Recaptcha" OnClick="reCaptcha_Click" TabIndex ="4"/>
My message boxes always return "not a valid recaptcha"
Can anyone shed some light on why I cannot get a valid recaptcha return?
Thanks!
Try:
Dim Response As String = Request("g-recaptcha-response")
Note the re

Design-time trouble with ASP.NET v2.0 Custom Control, with List<T> for child items

Folks,
I am having a devil of a time with a custom control. The control is very simple - it just displays a list of "StepItems" (rendered as table rows), each with an icon. When I first drag it onto a page, and add StepItems to its collection, it renders perfectly. If I provide some text for its Header property, that also renders perfectly.
If I then look at the HTML source view, and then back to the design view, I get an error where my control should be. There are two kinds of errors:
If I set the .Header property, the error reads "StepProgressControl1:'someheadertext' could not be set on property 'Header'.
If I don't set the .Header, but add StepItems to the collection, I get this: "ErrorStepProgressControl1:'StepItems' could not be initialized. Details: Method not found: 'System.Collections.Generic.List`1 StepProgressControl.TKC.Experiment.StepProgressControl.get_StepItems()'."
The complete code for my custom control is below. If you can provide any help, thank you a great deal!
Tom
'================================
Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Security.Permissions
Imports System.ComponentModel
Namespace TKC.Experiment
' THIS IS THE INTERNAL "CHILD" ELEMENT
< _
PersistenceMode(PersistenceMode.InnerProperty), _
TypeConverter(GetType(StepItemConverter)) _
> _
Public Class StepItem
Private _name As String
Public Sub New()
Me.New("")
End Sub
Public Sub New(ByVal name As String)
Me._name = name
End Sub
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
'=====================================================================
' THIS IS THE ACTUAL "PARENT" WEB CONTROL
< _
ParseChildren(True, "StepItems"), _
PersistChildren(False) _
> _
Public NotInheritable Class StepProgressControl
Inherits WebControl
Private _header As String = String.Empty
Private _stepItems As New List(Of StepItem)
Public Sub New()
Me.Header = "StepProgressControl"
End Sub
< _
PersistenceMode(PersistenceMode.Attribute) _
> _
Public Property Header() As String
Get
Return _header
End Get
Set(ByVal value As String)
_header = value
End Set
End Property
< _
DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
PersistenceMode(PersistenceMode.InnerProperty) _
> _
Public ReadOnly Property StepItems() As List(Of StepItem)
Get
If _stepItems Is Nothing Then
_stepItems = New List(Of StepItem)
End If
Return _stepItems
End Get
'Set(ByVal value As List(of stepitem))
' _stepItems = value
'End Set
End Property
Public Overrides Sub RenderControl(ByVal writer As System.Web.UI.HtmlTextWriter)
MyBase.RenderControl(writer)
Dim label As New Label()
label.Text = Header
label.RenderControl(writer)
Dim table As New Table()
Dim htr As New TableRow()
Dim hcell1 As New TableHeaderCell()
hcell1.Text = "Name"
htr.Cells.Add(hcell1)
Dim hcell2 As New TableHeaderCell()
hcell2.Text = "Title"
htr.Cells.Add(hcell2)
table.BorderWidth = Unit.Pixel(0)
Dim stepItem As StepItem
For Each stepItem In StepItems
Dim tr As New TableRow()
Dim cell1 As New TableCell()
Dim img As New HtmlImage
img.Src = ""
img.Alt = ""
cell1.Controls.Add(img)
tr.Cells.Add(cell1)
Dim cell2 As New TableCell()
cell2.Text = stepItem.Name
tr.Cells.Add(cell2)
table.Rows.Add(tr)
Next stepItem
table.RenderControl(writer)
End Sub
End Class
'========================================
'THIS IS A "TYPE CONVERTER" - JUST A COSMETIC THING, NOT CAUSING TROUBLE...
Public Class StepItemConverter
Inherits TypeConverter
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
Dim obj As StepItem = DirectCast(value, StepItem)
Return obj.Name
End Function
End Class
End Namespace
You will want to implement your own Collection object to represent the list - otherwise the designer will not display it properly.
See the ICollection, IEnumerable, etc. interfaces.

Resources