[英]How to implement a web service in ASP.NET
我是一个相当新的程序员,并且是Web编程的新手,我需要一些ASP.NET修复。 我正在尝试以一种形式实现自动完成控件。 当我将搜索逻辑以代码形式放在后面时,一切正常。 但是,一旦将搜索逻辑移至Web服务(自动完成功能仍在表单代码中调用相同的方法),一切都会变得很麻烦。 我正在使用OleDb连接并尝试访问我的工作站上的测试数据库,但是出现一条错误消息,指出该服务器不可访问。
显然,我可以坚持使用aspx代码,但是不管我需要了解Web服务的基本工作原理。 有人可以帮助我了解我在哪里出错吗?
我所看到的是这样的:
...
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetContacts(string prefixText, int count)
{
AutoCompleteService autoCompleteService = new AutoCompleteService();
autoCompleteService.QueryString = "SELECT ...";
autoCompleteService.ConnectionString = UserContext.ConnectionString;
return autoCompleteService.GetResults(prefixText, count);
}
...
<%@ WebService Language="C#" CodeBehind="AutoComplete.Service.cs" Class="AutoCompleteService" %>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
using System.Data;
[WebService(Namespace = "http://tempuri.org/")]
[System.Web.Script.Services.ScriptService]
public class AutoCompleteService : System.Web.Services.WebService {
public string QueryString;
public string ConnectionString;
public AutoCompleteService()
{
QueryString = "";
ConnectionString = "";
}
[WebMethod]
public String[] GetResults(string prefixText, int count)
{
...
}
private String[] GetNameListFromDB()
{
List<string> resultList = new List<string>();
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open(); //Exception thrown here.
...
}
ServerVersion = 'connection.ServerVersion' threw an exception of type 'System.InvalidOperationException'
您调用CodeBehind,这只是实例化该类。 它不使用该服务。
使用您的客户端JavaScript库,您可以直接调用该服务。 没有codeBehind中的代码。 但这取决于您的客户端js库。
我建议您使用的是Lern MVC或asp.net MVC,因为它们的工作要简单得多,并且最终实现起来也更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.