C# webservice初探

        转载请注明出处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

        


posted @ 2016-01-10 13:45  phlsheji  阅读(209)  评论(0编辑  收藏  举报