C# webservice 的使用.關鍵就在一個遠程調用.好比一段語句調用的多了,當然要放在一個方法裏面,然後直接調用方法,這個webservice就在一個web上,實現了遠程的調用,數據庫,函數都可以,功能強大實用.話不多說進正題:
環境: VS2012
功能: 本地項目調用,其他IP暫時沒實現.
一. 創建
先創建一個空白網站應用(ASP.NET 空 Web 應用程序).
然後添加一個web服務(Web 服務 (.asmx))
添加完,然後就可以添加你想要實現的功能代碼了,這裏自帶一個helloworld方法.
添加完可以 Ctrl+F5 運行看看.我這裏添加了一個求和方法和求差方法.WebMethod[是否允許遠程調用] Description[描述]
這裏一個webservice就算製作完成, 接下來 發佈.
二. 發佈
三. 測試.
接下來可以測試一下,先建一個控制檯應用程序,然後”添加服務引用”–”高級”–”添加Web引用”–輸入URL.
然後點擊”添加引用”就好.
然後添加如下代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
localhost.testDemo test = new localhost.testDemo();
string a = test.HelloWorld();
int b = test.Add(4, 2);
Console.WriteLine(a + "\n" + "4+2= " + b);
Console.ReadKey();
}
}
}
F5或者 Ctrl+F5 運行結果如下:
成功運行.
寫這篇博客只是爲了我第一次寫博客的好奇與激動,哈哈 參考了好多資料,其中比較詳細的一篇大家可以去看看:
http://www.cnblogs.com/Brambling/p/6815565.html
四. 喫飯.