用C#创建服务 [英] Creating a Service in C#
本文介绍了用C#创建服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个由Web服务器调用的服务.我是这部分的初学者.因此,请给我一些有关如何开始工作的提示或指导.
谢谢.
I want to create a service which is called by a web server. I am a beginner in this part. So please give me some hints or direction about how should I start my work.
Thank you.
推荐答案
转到从您的项目中添加新项目
添加WebService.asmx
在App_Code文件夹WebService.cs下添加了一个文件
转到该文件
在这里,您必须编写要创建的方法.在这里,我给您提供一个示例:
Goto to add new Item from your Project
Add WebService.asmx
one file is added under App_Code folder WebService.cs
Goto That file
Here you have to write which method you want to create..Here i am giving you an example:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
[WebMethod]//added these else code doesn't work
public List<City> GetNews()
{
List<City> list = new List<City>();
string constr = "Data Source=localhost;Initial Catalog=DBname;User Id=sa;Password=lock";
string query = "SELECT top 20 news_id,news_title FROM news order by news_id DESC";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
list = ds.Tables[0].AsEnumerable().Select(data => new City() { Id = (int)data["news_id"], title = (string)data["news_title"] }).ToList();
return list;
}
public class City
{
public int Id { get; set; }
public string title { get; set; }
}
现在,我已经从这样的另一个页面调用了这些Web服务
Now i have called these web service from another page like this way
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
(document).ready(function(){
(document).ready(function () {
.ajax({ 输入:"POST", 网址:"WebService.asmx/GetNews", 数据: "{}", contentType:"application/json; charset = utf-8", dataType:"json", 成功:功能(响应){ var cars = response.d;
.ajax({ type: "POST", url: "WebService.asmx/GetNews", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var cars = response.d;
这篇关于用C#创建服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文