在Visual Studio 2010中使用控制台创建Web服务

时间:2013-05-27 06:47:41

标签: c# visual-studio-2010 web-services

我的雇主要求我使用Microsoft visual studio c#2010 express中的“控制台模板”创建Web服务。他说我需要做的就是右击参考>添加参考>在.Net选项卡下添加System.ServiceModel和System.IdentityModel。

我很抱歉这个非常基本的问题,但我不知道该怎么做。

2 个答案:

答案 0 :(得分:1)

using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var wsh = new WebServiceHost(typeof(AService), new Uri("http://0.0.0.0/AService"));
            wsh.Open();
            Console.ReadLine();
        }

        [ServiceContract]
        public class AService
        {
            [OperationContract, WebGet]
            public int AMethod(int i,int j)
            {
                return i + j;
            }
        }
    }
}

并导航至浏览器中的http://localhost/AService/AMethod?i=1&j=2

答案 1 :(得分:0)

实际上完全遵循(几乎)所说的内容。

这项任务并不像听起来那么令人生畏。首先准备好服务网址(uri)。

然后打开VS,创建一个新的控制台应用程序 创建之后,在右侧的解决方案资源管理器窗口中,右键单击“引用”
在该菜单上,您可以选择“添加服务参考”,而不仅仅是参考 单击该对话框后,会出现一个对话框。输入服务URL 按照说明操作,很可能现在只需单击“确定”,即可为服务生成包装类。

换句话说,您将通过此步骤生成类,对于您可以开始调用方法的服务。

类似的东西:

MyServiceClient myService = new MyServiceClient();
myService.Method1();