将Web服务(VS2003)迁移到WCF(VS2010)

时间:2011-10-09 02:34:05

标签: ajax wcf visual-studio-2010 asmx

我们的网站使用VS2003进行维护。我们将我们的解决方案转换为VS2010 - 它是一个Web应用程序项目。它有几个Web服务文件(.asmx文件结尾),我们希望分两个阶段转换为WCF,首先保留通过AJAX将XML文档写回客户端的asmx文件的功能......然后学习构建支持AJAX的WCF服务也可以做同样的事情。

我们研究了“无法创建类型xxx”错误,建议各不相同。我们确定(!!)有一个App_Code文件夹没有区别,我们已经正确地命名了我们的命名空间和类,并且我们正确地修饰了Web服务代码,并且我们能够使用ASP.Net 2.0激活这些服务,我们在IIS中正确设置了应用程序级别...我们仍然无法通过错误。

这是我们的asmx代码(我们创建了一个带有ScriptManager控件的测试页面,试图点击这个Web方法,但我们从未超越该服务的构建)。你当然不需要为实际的数据库拉动而烦恼,但无论如何我都把它们留下了:

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

任何帮助将不胜感激。也许它很明显会很快出现在这里,但肯定会让我们烦恼!如果您需要更多信息或代码,请告诉我们。

1 个答案:

答案 0 :(得分:1)

的优秀样本和演示如何实现这一目标.......

只需进行一些快速的Google搜索:

我只会在Google上搜索您的关键字(ASMX,WCF,Migrate),您应该获得大量的链接和有用的资源......

至于学习WCF基础知识的资源:MSDN WCF Developer Center包含从初学者的教程到文章和示例代码的所有内容。

另外,请查看screen cast library up on MSDN,了解一些非常有用的10-15分钟信息块,其中包含与您可能感兴趣的WCF相关的任何主题。