轉載請註明出處Coder的不平凡:http://blog.csdn.net/pearyangyang/article/details/46348633
由於工作的終端以前是直接對數據庫進行操作,導致每次終端會卡死,嚴重影響業務進度。所以進行了技術調整,用webservice來作爲數據對接的一箇中間件,自己也部署了一下webservice環境和入門。總體來說分爲以下這幾個步驟:
1.部署IIS環境
2.創建webservice
3.編寫測試程序引用webservice
我們就開始一步一步來進行。 首先部署IIS環境,win7中打開控制面板--->程序--->打開或關閉Window功能
選中裏面的選項
這樣IIS環境就配置好了,我們可以在開始編寫webservice程序,在visual studio2008中建立一個“ASP.NET服務應用程序”,名字叫MathService
打開MathService.asmx文件,編寫如下代碼:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace MathService
{
/// <summary>
/// Service1 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的註釋。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public int Add(int a,int b)
{
return a + b;
}
[WebMethod]
public int Sub(int a, int b)
{
return (a - b);
}
[WebMethod]
public int Mul(int a, int b)
{
return a * b;
}
[WebMethod]
public int Div(int a, int b)
{
return a / b;
}
}
}
在“解決方案資源管理器”中,選中項目,點擊右鍵,選擇“生成”,然後發佈,(如果是部署到本地的話就是本地的一個目錄,我的是D:\net\webservice\).
然後回到IIS信息服務管理器中,在Default Web Site下面新建一個“虛擬目錄”,按照如下的方式進行設置:
然後我們回到visual studio2008,重新建立一個控制檯應用程序來測試webservice所提供的方法是否我們可以引用。當然建立以後,我們還需要添加web reference,如圖:
選中項目,點擊鼠標右鍵,選中“添加web引用”,點擊高級引用屬性,我們可以出現我們在IIS中配置好的webservice項目。
由於我是在本地電腦進行測試,調試用的,所以我選擇 “本地計算機的Web服務”,他就會出現我們在IIS中配置好的webservice。
把URL複製上去,點擊”前往就可以了“。然後下面是測試webservice連接程序的代碼,調用了自己編寫的webservice中的Add方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ForkWebService
{
class Program
{
static void Main(string[] args)
{
localhost.Service1 myMathService = new localhost.Service1();
Console.Write("2+4={0}", myMathService.Add(2, 4));
Console.ReadLine();
}
}
}
好了,一個簡單的webservice程序部署我們就這樣完成了~~~
另外我們在部署的時候還出現了一個問題(Win 7),那就是:
CS0016:
未能寫入輸出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\TemporaryASP.NET
Files\web\4b49f661\23a749fc\App_Web_default.aspx.cdcab7d2.zii776dc.dll”--“拒絕訪問。 ”
解決方法:
找到C:\Windows\Temp 目錄,在其屬性->安全->編輯->添加 IIS_IUSERS用戶 賦予"完全控制"權限
參考:
http://www.cnblogs.com/lonelyxmas/archive/2011/05/28/2061272.html
https://support.microsoft.com/en-us/kb/308359