开发者

Migrate Web Services (VS2003) to WCF (VS2010)

Our site is maintained with VS2003. We converted our solution to VS2010--it is a Web Application Project. It has several Web Services files (.asmx file endings) that we hoped to convert to WCF in two stages, first preserve the function of the asmx files that write back XML documents via AJAX to the client..., and then learn to build AJAX-enabled WCF Services to do the same things.

We have researched the 'Could not create type xxx'-error and the advice varies. We're certain(!!) that having an App_Code folder is not the difference, and that we've named our namespace and class correctly, and that we've decorated the Web Service code correctly, and that we're able to use ASP.Net 2.0 to activate these services, and that we have the application level set correctly in IIS... and we're still not able to get past the error.

Here is our asmx code (we created a test page with ScriptManager control to try to hit this one Web Method, but we never got past the build for the service). You certainly don't need to bother with the actual database pulls, but I left them in anyway:

Imports System
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports System.ComponentModel
Imports System.Data.SqlClient
Imports System.Text
Imports System.Uri
Imports System.Xml
Imports System.IO

<WebService([Namespace]:="nsCarousel", Description:="Carousel Web Service Methods")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<System.Web.Script.Services.ScriptService()> _
Public Class Carousel
    Inherits WebService

    <WebMethod(Description:="Get Dealer Info")> _
    Public Function GetDealerInfo(ByVal DID%) As String
        Dim s$, sql$, sRequest$, sRequestPathQuery$, sDID$, sAJAX$, sReturn$, sIP$, sWebSite$, sErr$, sDealerSearch$
        Dim sXML$, sXMLDealerResults$, sXMLPath$
        Dim sUserName$, sGeneralDesc$, sStatusText$
        Dim bPost As Boolean
        Dim bHaveData As Boolean
        Dim ds As DataSet
        Dim dasql As SqlDataAdapter
        Dim sqldr As SqlDataReader
        Dim sqlConn As SqlConnection
        Dim sqlCmd As SqlCommand
        Dim xdoc As New XmlDocument
        Dim xnode As XmlNode
        Dim xrefnode As XmlNode
     开发者_运维问答   Dim xnewnode As XmlElement

        Try
            '/ need test of sRequest for reference and use in test just below
            bPost = False
            sDID = Convert.ToString(DID)
            sGeneralDesc = "Dealer Pull"

            sqlConn = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringLong"))
            sqlConn.Open()
            '/ new sp to preserve nodes, even when fields are null
            '/ get results for individual dealer
            sql = "insert into junk(entrydate, sql_stmts) select GetDate(), '" & sDID & "';"
            sqlCmd = New SqlCommand(sql, sqlConn)
            sqlCmd.ExecuteNonQuery()
            sqlCmd.Dispose()

            sql = "Select_DRPROByDID_xml " & sDID
            sqlCmd = New SqlCommand(sql, sqlConn)
            sqldr = sqlCmd.ExecuteReader()
            '/ quick test if have data
            bHaveData = False
            Do While sqldr.Read
                If Not (IsDBNull(sqldr("DNum"))) Then
                    bHaveData = True
                End If
                Exit Do
            Loop
            sqldr.Close()
            sqldr = Nothing
            sqlCmd.Dispose()
            sqlCmd = Nothing
            '/ s will include a node for NewDataSet -> remove and add nodes for msg based on status
            '/ and DealerError
            If bHaveData Then
                dasql = New SqlDataAdapter(sql, sqlConn)
                ds = New DataSet
                dasql.Fill(ds)
                s = ds.GetXml
                sXMLDealerResults = s
                dasql.Dispose()
                ds.Clear()
                dasql = Nothing
                ds = Nothing
                '/ query the XML doc
                xdoc.LoadXml(s)
                xnode = xdoc.SelectSingleNode("/NewDataSet/Table")
                If (xnode.HasChildNodes) Then
                    If (sDID.Length > 0) Then
                        sStatusText = xdoc.GetElementsByTagName("DRStat").Item(0).InnerText
                        Select Case sStatusText
                            Case "A"
                                '/ test next when opportunity exists
                                '/ set current node at Table
                                'sXMLPath = "/NewDataSet/Table"
                                '/ set Table as the current node
                                'xrefnode = xdoc.SelectSingleNode(sXMLPath)
                                'xnewnode = xdoc.CreateElement("DealerMsg")
                                'sStatusText = "Active dealer found."
                                'xdoc.InsertAfter(xnewnode, xrefnode)
                                sXMLDealerResults = sXMLDealerResults.Replace("<Table>", "<Table><DealerMsg>Active dealer found.</DealerMsg>")
                            Case "C"
                                sXMLDealerResults = sXMLDealerResults.Replace("<Table>", "<Table><DealerMsg>Dealer has been cancelled.</DealerMsg>")
                            Case "H"
                                sXMLDealerResults = sXMLDealerResults.Replace("<Table>", "<Table><DealerMsg>Dealer is on hold.</DealerMsg>")
                            Case Else
                                sXMLDealerResults = sXMLDealerResults.Replace("<Table>", "<Table><DealerMsg>Dealer status is invalid.</DealerMsg>")
                        End Select
                        sXMLDealerResults = sXMLDealerResults.Replace("<Table>", "<Table><DealerError>1</DealerError>")
                    Else
                        '/ do nothing
                    End If
                Else
                    sXMLDealerResults = sXMLDealerResults.Replace("<Table>", "<Table><DealerError>0</DealerError><DealerMsg>Dealer not found.</DealerMsg>")
                End If
            Else
                sXMLDealerResults = "<Table><DealerError>0</DealerError><DealerMsg>Dealer not found.</DealerMsg></Table>"
            End If
            sXMLDealerResults = sXMLDealerResults.Replace("<NewDataSet>", "")
            sXMLDealerResults = sXMLDealerResults.Replace("</NewDataSet>", "")
            If (sDID.Length > 0) Then
                sXMLDealerResults = sXMLDealerResults.Replace("<Table>", "")
                sXMLDealerResults = sXMLDealerResults.Replace("</Table>", "")
            Else
                sXMLDealerResults = sXMLDealerResults.Replace("<Table>", "<Dealer>")
                sXMLDealerResults = sXMLDealerResults.Replace("</Table>", "</Dealer>")
            End If
            sXML = "<?xml version=""1.0"" ?><DealerFetch>" + sXMLDealerResults.Trim + "</DealerFetch>"
            'If sReturn.Trim.Length = 0 Then
            '    Response.ContentType = "text/xml"
            '    Response.Write(sXML)
            'End If
            sqlConn.Close()
            sqlConn = Nothing

            Return sXML

        Catch exp As Exception
            sErr = sErr & " " & exp.Message.ToString
        End Try
    End Function
End Class

Any help would be appreciated. Maybe it's something obvious that will appear to someone here quickly, but it sure is making us fret!!! If you need more info or code, please let us know.


There are tons of good samples and demos how to achieve this.......

Just a few a quick Google search turned up:

  • Convert existing asmx .net web service to WCF service in .net 3.0/3.5
  • Convert .asmx file to WCF .svc file to create simple API web service
  • Migrating a Versionable ASMX Web Service to WCF

I would just search Google for your keywords (ASMX, WCF, Migrate) and you should get tons of links and useful resources.....

As for resources to learn the basics of WCF: there's the MSDN WCF Developer Center which has everything from beginner's tutorials to articles and sample code.

Also, check out the screen cast library up on MSDN for some really useful, 10-15 minute chunks of information on just about any topic related to WCF you might be interested in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜