【问题标题】:WebService/ScriptService not found; System.Web.Extensions not recognized未找到 WebService/ScriptService; System.Web.Extensions 无法识别
【发布时间】:2012-02-19 21:37:10
【问题描述】:

我在我的 vb.net 4.0 Web 应用程序(或网站......不确定它是什么 - 有关系吗?)中创建了一个脚本服务,因为我想从客户端调用它。我收到客户端错误,说我的命名空间无法识别(下面代码中的 HomepageService)。我已经尝试使用在项目中配置为“根命名空间”的名称对其进行限定,但 js 说它也不识别该命名空间。

该应用程序较旧 - 我们最近将其从 dotnet 2.1 转换为 4.0。

我找到了以下相关主题,因为当我尝试在 HomepageServices.asmx.vb 中导入 System.Web.Extensions 时,Visual Studio 说即使我可以看到它,它也无法识别它,列在参考文献下的工作室。

[System.Web.Extensions 程序集无法解析][1]

我尝试发布该主题的后续问题,因为我尝试按照答案中的说明进行操作,但它们不起作用(我在项目 > 属性 > 应用程序中没有“目标框架”),但有人删除了是因为我想我不允许问后续问题吗?

我查阅了各种网站并按照说明进行操作,以下是一个示例: http://www.asp.net/ajax/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

这是位于我网站根文件夹中的 HomepageService.asmx 的内容:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Collections.Generic


<System.Web.Services.WebService(Namespace:="http://localhost/appname")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<System.Web.Script.Services.ScriptService()> _
Public Class HomepageService
    Inherits System.Web.Services.WebService

    Shared _rand As Random = New Random(Environment.TickCount)

    <WebMethod()> _
    Public Function Test(ByVal s As String) As Integer
        Return _rand.Next(0, 120)

    End Function


End Class

来自母版页的片段:

<body>
<form id="form1" runat="server">

    <asp:ScriptManager ID="Scriptmanager1" runat="server" EnablePageMethods="true">
        <Scripts>
            <asp:ScriptReference Path="CallWebServiceMethods.js" />
        </Scripts>

        <Services>
            <asp:ServiceReference Path="HomepageService.asmx" />
        </Services>
    </asp:ScriptManager>

在我的页面顶部,我导入了 js:

PageRequestManager.js:

HomepageService.set_defaultSucceededCallback(
                 OnLookupComplete);
HomepageService.set_defaultFailedCallback(
                 OnError);
function OnLookup() {
    HomepageService.Test(stb.value);
}
function OnLookupComplete(result, userContext) {
    // userContext contains symbol passed into method
    var res = document.getElementById("_resultLabel");
    res.innerHTML = userContext + " : <b>" + result + "</b>";
}
function OnError(result) {
    alert("Error: " + result.get_message());
}

我的 web.config 是一团糟,但我很乐意发布它...

【问题讨论】:

  • 我尝试了位于 msdn.microsoft.com/en-us/library/bb532367(v=vs.90).aspx 的 msdn 演练,它可以工作。主要区别似乎是他们的 webservice 类被命名空间包围,并且完全使用标记中的命名空间进行限定。但是我不能在我的网络服务周围放置一个命名空间,因为在 VB 中有一个默认命名空间,我不能按照其他地方的建议将其空白,因为应用程序中已经存在的代码将无法编译......有什么办法吗解决这个问题?

标签: web-services scriptservice


【解决方案1】:

终于搞定了。这就是我的做法。虽然不确定问题是什么.. 但按照下面的过程就可以了。

第 1 步。按照 http://msdn.microsoft.com/en-us/library/bb532367(v=vs.90).aspx 的演练,使用新的网站项目。
有用。我将其称为模型项目。

第 2 步。在我现有的 Web 应用程序中创建文件并从模型项目中复制所有代码: - 在我项目的根文件夹中创建了 WebService HelloWorld.asmx(studio 还给了我一个嵌套文件 Helloworld.asmx.vb)。这与模型项目不同,因为当我在该项目中创建 Web 服务时,它会将 .asmx 放在我的根文件夹中,但会将 .asmx.vb 放在 App_Code 文件夹中。不确定我是否应该手动移动它或其他什么?过去,当我尝试将 App_Code 或 App_data 文件夹与此 Web 应用程序一起使用时,一切都陷入了困境......

第 3 步。编辑 web 服务以使用我的 web 应用程序而不是网站: - 命名空间 Samples.Aspnet 被注释掉,没有被替换为不同的命名空间,因为我的应用有一个默认命名空间“Fubar”,与我的应用的项目名称相同。

这是我的网络应用根文件夹中 HelloWorld.asmx.vb 中的结果代码:

Imports System
Imports System.Web
Imports System.Collections
Imports System.Web.Services
Imports System.Web.Services.Protocols

'Namespace Samples.Aspnet
<WebService([Namespace]:="http://mycompany.org/"), _
WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1), _
System.Web.Script.Services.ScriptService()> _
Public Class HelloWorld
    Inherits System.Web.Services.WebService

    Public Sub New()

    End Sub 'New


    'Uncomment the following line if using designed components 
    'InitializeComponent(); 

    <WebMethod()> _
    Public Function Greetings() As String
        Dim serverTime As String = _
            String.Format("Current date and time: {0}.", DateTime.Now)
        Dim greet As String = "Hello World. <br/>" + serverTime
        Return greet

    End Function 'Greetings
End Class 'HelloWorld

'End Namespace

这是 HelloWorld.asmx 中的代码,也在根文件夹中:

<%@ WebService Language="vb" CodeBehind="HelloWorld.asmx.vb" Class="Fubar.HelloWorld" %>

步骤 4. 创建 js“HelloWorld.js”,像在模型项目中一样将其粘贴在根文件夹中,然后粘贴模型项目中的代码。进行一项编辑以匹配我的应用程序的命名空间。这是生成的代码:

var helloWorldProxy;

// Initializes global and proxy default variables.
function pageLoad() {
    // Instantiate the service proxy.
  //  helloWorldProxy = new Samples.Aspnet.HelloWorld();
    helloWorldProxy = new Fubar.HelloWorld();
    // Set the default call back functions.
    helloWorldProxy.set_defaultSucceededCallback(SucceededCallback);
    helloWorldProxy.set_defaultFailedCallback(FailedCallback);
}


// Processes the button click and calls
// the service Greetings method.  
function OnClickGreetings() {
    var greetings = helloWorldProxy.Greetings();
}

// Callback function that
// processes the service return value.
function SucceededCallback(result) {
    var RsltElem = document.getElementById("Results");
    RsltElem.innerHTML = result;
}

// Callback function invoked when a call to 
// the  service methods fails.
function FailedCallback(error, userContext, methodName) {
    if (error !== null) {
        var RsltElem = document.getElementById("Results");

        RsltElem.innerHTML = "An error occurred: " +
            error.get_message();
    }
}

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

第 5 步。将代码添加到我的母版页中的 ScriptManager 标记。这对应于 Default.aspx 中模型项目的代码; 下面的 ScriptManager 标签与模型项目中的标签相同:

<body>
    <form id="form1" runat="server">

        <asp:ScriptManager runat="server" ID="ScriptManager">
            <Services>
                <asp:ServiceReference path="~/HelloWorld.asmx" />
            </Services>
            <Scripts>
                <asp:ScriptReference Path="~/HelloWorld.js" />
            </Scripts>
        </asp:ScriptManager>

第 5 步。将控件添加到我的测试页面,就像它们在模型项目中的 Default.aspx 中一样:

   <div id="divTestWebServices">

         <button id="Button1" onclick="OnClickGreetings(); return false;">Greetings</button>
         <div>
            <span id="Results"></span>
        </div>   
    </div>

步骤 6. 运行 Fubar 并导航到测试页面,然后点击按钮。

有效!!!我不知道为什么它以前不起作用,但经过三天后,我现在可以继续前进了......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多