Calling a WCF service using SOAP in unit test or with VB Script on a classic ASP page

I created a WCF service in .Net and this was fine for .Net customers who use the Add Service Reference wizard easily enough.
However one customer was on classic ASP and he found it difficult to connect. So when we had succeeded together it was worth writing down. This is the result.
Please ignore the property naming. I don’t use strPropertyName. This isn’t working code, just an idea of what is required.

Solved.

Tips:

  • Inspect the .wsdl file
  • Note the soapAction setting
  • Note the namespace. The WCF namespace will include the interface name

Unit test written in VB.Net for SOAP

Public Sub SOAPClientHelloWorldTestMethod()

strURLSubFolder = “ServiceName.svc”
strInterfaceName = “IServiceName”
strFunction = “HelloWorldMethod”
Dim parameter1 = “parameter1”

strRequest2FunctionNameOpen = “&lt ns1:HelloWorldMethod &gt”
strRequest3Parameters = “&lt” + parameter1Value + “&gt”
strRequest4FunctionNameClose += “&lt ns1:HelloWorldMethod &gt”

CallSOAP()

End Sub

Then the SOAP call method

Private Sub CallSOAP()

‘URL to SOAP namespace and connection URL
Dim strURLBase = “http://serviceaddress.net/”
Dim strURL = strURLBase + strURLSubFolder
Dim strNamespace = “http://tempuri.org/”

strRequest1SOAPPart1 = “<?xml version=””1.0″” encoding=””UTF-8″”?>” & _

“<SOAP-ENV:Envelope ” & _

“xmlns:SOAP-ENV=””http://schemas.xmlsoap.org/soap/envelope/”&#8221; ” & _

“xmlns:ns1=””” & strNamespace & “””>” & _

“<SOAP-ENV:Body>”

strRequest5SOAPEnd = “</SOAP-ENV:Body></SOAP-ENV:Envelope>”

Dim strRequest = strRequest1SOAPPart1 + strRequest2FunctionNameOpen + strRequest3Parameters + strRequest4FunctionNameClose + strRequest5SOAPEnd
TestContext.WriteLine(“URL = ” + strURL)
TestContext.WriteLine(“Request = ” + strRequest)
Dim soapAction As String
soapAction = strNamespace & strInterfaceName & “/” & strFunction
TestContext.WriteLine(“SOAPAction = ” + soapAction)

Dim objectName As String
‘objectName = “Msxml2.XMLHTTP.3.0”
‘objectName = “MSXML2.DOMDocument.6.0”
objectName = “MSXML2.ServerXMLHTTP”

Dim soap_request As Object
soap_request = CreateObject(objectName)

Dim contentType As String
contentType = “text/xml; charset=utf-8”
‘contentType = “application/x-www-form-urlencoded”

soap_request.open(“POST”, strURL, False)
soap_request.setRequestHeader(“Content-Type”, contentType)
soap_request.setRequestHeader(“Content-Length”, Len(strRequest))
soap_request.setRequestHeader(“SOAPAction”, soapAction)

‘send the request and capture the result
soap_request.send(strRequest)
Dim strResult = soap_request.responseText
soap_request = Nothing

‘display the XML
‘response.write (strResult)
‘TestContext.WriteLine(strResult)
TestContext.WriteLine(“”)
Dim xml = XDocument.Parse(strResult)
TestContext.WriteLine(xml.ToString())

End Sub

Classic ASP

Title
<%
Dim strRequest, strResult, strElement, strURL, strNamespace,interfacename,soap_request,parameter1

‘ Define services
strURL = “http://serviceaddress.svc&#8221; ‘ Service URL
strNamespace = “http://tempuri.org/&#8221; ‘ Name space
interfacename=”IServiceName/” ‘ interface name
strElement = “GetMethod” ‘ Element

set soap_request = Server.CreateObject(“Msxml2.XMLHTTP.3.0″)
strRequest =”<?xml version=””1.0″” encoding=””UTF-8″”?>”

strRequest=strRequest& “<SOAP-ENV:Envelope xmlns:SOAP-ENV=””http://schemas.xmlsoap.org/soap/envelope/”&#8221; xmlns:ns1=”””&strNamespace&”””>”

strRequest=strRequest& “<SOAP-ENV:Body>”

strRequest=strRequest& “<ns1:HelloWorldMethod>”

strRequest=strRequest& “<ns1:parameter1>”&soap_user&”</ns1:parameter1>”

strRequest=strRequest& “</ns1:HelloWorldMethod>”

strRequest=strRequest& “</SOAP-ENV:Body>”

strRequest=strRequest& “</SOAP-ENV:Envelope>”

soap_request.open “post”, “”&strURL&””, False
soap_request.setRequestHeader “Content-Type”, “text/xml; charset=utf-8”
soap_request.setRequestHeader “SOAPAction”,strNamespace&interfacename&strElement
soap_request.setRequestHeader “Content-Length”,Len(strRequest)

Call soap_request.send(strRequest) ‘ Send the soap request and capture the result
strResult = soap_request.responseText

‘display the XML
if strResult”” then
Set ResponseXML = Nothing
end if
Set soap_request = Nothing

end if

%>

Advertisements

ASP.Net How do I call a code behind method from an image

Following was html

<a href="CatalogueByList.aspx?VehicleTypeID=3" target="_self">
<img id="CatalogueCVImage" style="border-style: none; height: 94px; width: 172px; margin: 20px;" alt="Automotive Catalogue" src="Resources/ebc-catcommercial-button.jpg" />
</a>

Following is code behind call to a method from an image

<asp:ImageButton
runat="server"
id="CatalogueCar"
ImageUrl="Resources/home-auto.jpg"
AlternateText="Automotive Catalogue"
CssClass="myImage"
style="border-style:none; height: 94px; width: 172px;margin: 20px"
OnClick="OpenCatalogueAutomotive"
/>

Then code behind as:

Protected Sub OpenCatalogueAutomotive(ByVal sender As Object, ByVal e As EventArgs)
ViewState("VehicleTypeID") = 1
Response.Redirect("CatalogueByList.aspx?VehicleTypeID=1")
End Sub

End

Publish ASP.Net to Azure ‘freezes’

Steps to reproduce freeze when publishing a website to Azure

This assumes you have previously published a site

  1. Visual Studio
  2. Right-click on ASP.Net project that you wish to publish
  3. Publish
  4. Preview
  5. Start Preview
  6. Publish
  7. Freezes after build

How to resolve

If your solution has multiple projects, make sure that the ASP.Net project is the “Start up project”

ASP.Net style css etc.

How to change width of out of box Visual Studio ASP.Net WebForms application

Took a while to work this out. In the Site.Master there is:

<body>
<form runat="server">
<div class="page">...
</div>

If you go to the class=”page” definition on the Site.css you may see a width. This seems to have no effect. I do not know why.

  1. Instead use split view and see on the bottom the different selections e.g.
  2. then with properties showing select
  3. then use the style property
  4. potion
  5. width e.g. 90 %

This may also be achieved in the source on the div tag:

<div class=”page” style=”width: 90%”>

Solved

How to copy an ASP.Net web form .aspx

The original page hook up can break

  1. Copy main file
  2. Rename file to remove “Copy of “
  3. View code
  4. Change Public Class name to the new name
  5. Show All Files
  6. Open the <file>.aspx.designer.vb
  7. Rename the class to the new name
    This may clear all errors. If not then continue
  8. If there is code behind then the controls will say “is already declared as..” or “Handle clause requires a WithEvents
  9. View Markup
  10. Change AutoEventWireup=”false” to “true”

 

ASP.Net publish troubleshooting

Was trying to publish to Azure Websites, but it was hanging on “Publish Started”

The Start-up project was another project, so switched it to the website and then published ok. Deliberately switched it back to see if the problem was this and the error returned.

Forms-Based Authentication with SQL Azure

 

Following is quite useful

http://msdn.microsoft.com/en-us/library/windowsazure/hh508981.aspx

I was able to get this to work without a Windows Azure Project

Worked on site. However the role manager did not wok

http://stackoverflow.com/questions/5541868/problem-with-your-selected-data-store-on-the-asp-net-website-administration-tool

Changed following and it worked
<roleManager enabled=”true”>

 

 

Deleting an ASP.Net azure hosted website using Entity Framework can potentially delete the linked database

I had a database on Azure that was being updated using Windows Forms. I then created an ASP.Net MVC application using EF to work against the same database. However I following a tutorial which used Code First

http://www.windowsazure.com/en-us/develop/net/tutorials/web-site-with-sql-database/

This would not publish until I used Nu-Get to:

enable-migrations

However I think I then used code first migrations on publish. However I published the website with high credentials and wanted to change the credentials to lower. Could not see how to do this in Azure, so deleted the website. At this point the database disappeared without warning.

This is why the last posts have been about SQL Azure backup. However recovered using on-site data and re-created database.

Microsoft support helped with the ASP.Net so:

enable-migrations

Check the file created, but do not check the box on publish.

Tested by deleting the Website – This then asked if you wanted to delete the linked database. Answer: No. Database stays ok.

ASP.Net publish to Azure with Database and control of Start Page

Followed

https://www.windowsazure.com/en-us/develop/net/tutorials/web-site-with-sql-database/

There was a warning on publish page 3, which mentioned code-first migrations must be enabled. After web-search I realised I needed to upgrade to a higher version of EntityFramework which has System.Data.Entity.Migrations

Used Visual Studio > Tools > Library Package Manager > Package Manager Console

PM> install-package EntityFramework -Pre

PM> enable-migrations

will then publish. Although now up, the sites page is “This web site has been successfully created. There’s nothing here yet, but Windows Azure makes it simple to publish content with GIT, TFS, FTS or your favorite development tool”

If go directly to a page I know exists it does take me there, (or in my case redirects to login)

http://xamlgeek.net/2010/11/19/start-page-in-azure/

During development this of course wasn’t an issue – I just right clicked the page I wanted as startpage as selected “Set as Start Page”.

When I deployed to Azure the start page wasn’t recognized. To set the start page in Azure it is necessary to do one of two things: 1) Implement a start page called “Default.aspx” or 2) specify the start page in the web.config file:

2010-11-19_002433

Setting the defaultDocument value in the web.config fixed my issue.

 

 

ASP.Net Forms Authorization

Use

C:\%windir%\Microsoft.NET\Framework\<versionNumber>\aspnet_regsql.exe

to add authorisation schema to an existing database

This will not work with SQL Azure. See

http://support.microsoft.com/kb/2006191

which links to

http://archive.msdn.microsoft.com/KB2006191