WebServiceHost 类

 

一、构造函数

1、public WebServiceHost (Type serviceType, params Uri[] baseAddresses);

使用指定的服务类型和基址初始化 WebServiceHost 类的新实例。

参数

serviceType

Type

服务类型。

baseAddresses

Uri[]

服务的基址。

示例

Uri[] baseAddresses = { new Uri("http://localhost/one"), new Uri("http://localhost/two") };
WebServiceHost host = new WebServiceHost(typeof(CalcService), baseAddresses);

2、public WebServiceHost (object singletonInstance, params Uri[] baseAddresses);

使用指定的单一服务器实例和基址初始化 WebServiceHost 类的新实例。

参数

singletonInstance

Object

要用作单一实例的服务实例。

baseAddresses

Uri[]

服务的基址。

示例

Uri[] baseAddresses = { new Uri("http://localhost/one"), new Uri("http://localhost/two") };
object mySingleton = GetObject();
WebServiceHost host = new WebServiceHost(mySingleton, baseAddresses);

二、示例

下面的示例演示如何使用 WebServiceHost 类来承载利用 WCF REST 编程模型的服务。

[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    [WebInvoke(UriTemplate = "add?x={x}&y={y}")]
    long Add(long x, long y);

    [OperationContract]
    [WebInvoke(UriTemplate = "sub?x={x}&y={y}")]
    long Subtract(long x, long y);

    [OperationContract]
    [WebInvoke(UriTemplate = "mult?x={x}&y={y}")]
    long Multiply(long x, long y);

    [OperationContract]
    [WebInvoke(UriTemplate = "div?x={x}&y={y}")]
    long Divide(long x, long y);

    [OperationContract]
    [WebGet(UriTemplate = "hello?name={name}")]
    string SayHello(string name);
}

public class CalcService : ICalculator
{
    public long Add(long x, long y)
    {
        return x + y;
    }

    public long Subtract(long x, long y)
    {
        return x - y;
    }

    public long Multiply(long x, long y)
    {
        return x * y;
    }

    public long Divide(long x, long y)
    {
        return x / y;
    }

    public string SayHello(string name)
    {
        return "Hello " + name;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Uri baseAddress = new Uri("http://localhost:8000/");

        WebServiceHost svcHost = new WebServiceHost(typeof(CalcService), baseAddress);

        try
        {
            svcHost.Open();

            Console.WriteLine("Service is running");
            Console.WriteLine("Press enter to quit...");
            Console.ReadLine();

            svcHost.Close();
        }
        catch (CommunicationException cex)
        {
            Console.WriteLine("An exception occurred: {0}", cex.Message);
            svcHost.Abort();
        }
    }
}
[ServiceContract]
public interface IHomeService
{
     [OperationContract]
     [WebGet(UriTemplate = "Get/{id}", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
     string Get(string id);
 
     [OperationContract]
     [WebInvoke(Method = "POST", UriTemplate = "Add", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
     string Add(string stu);
}
 
public class HomeService : IHomeService
{
     public string Get(string id)
     {
         return "Get " + id;
     }
 
     public string Add(string id)
     {
         return "Add " + id;
     }
 }
 
 public class WebApi
 {
     private static WebServiceHost _host;
 
     /// <summary>
     /// 开启服务
     /// </summary>
     /// <param name="url">监听路径(http://127.0.0.1:3721/abc)</param>
     public static void Open(string url)
     {
         if (_host==null)
         {
             Uri baseAddress = new Uri(url);
             _host = new WebServiceHost(typeof(HomeService), baseAddress);
             _host.Open();
         }
     }
 }

注释

RequestFormat规定客户端必须是什么数据格式请求的(JSon或者XML),不设置默认为XML
ResponseFormat规定服务端返回给客户端是以是什么数据格返回的(JSon或者XML)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完整的服务端及客户端调用程序,在win7+ vs2015环境运行通过. 一、说明 1、创建winfrom应用程序;(或者是控制台项目) 2、在项目中添加一个WCF服务,并实现服务; 3、在需要启动WebService服务的地方启动该服务即可; 二、代码如下: 1、新建一个WCF服务——定义服务接口    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]     public interface ICalculator     {         [OperationContract]         double Add(double n1, double n2);     } 2、新建一个WCF服务——实现服务 public class CalculatorService : ICalculator     {         public double Add(double n1, double n2)         {             return n1 + n2;         }     } 3、添加完WcF服务后会在应用程序配置文件中有入下节点                             <!--TestServer.ICalculator服务定义的接口,根据自己定义进行修改-->                                                                   <baseAddresses> <!--这个是要发布的服务地址,可以进行修改-->                                   </baseAddresses>                   4、在要启动服务的地方启动服务监听   public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { try { //打开服务创建监听,并开始监听消息 ServiceHost serviceHost = new ServiceHost(typeof(Service1));//需要using System.ServiceModel; serviceHost.Open(); label1.Text = "服务启动正常"; } catch (Exception ex) { label1.Text = ex.Message; } } 5、下面可以在客户端通过上面的服务地址”http://xxx.xxx.xxx.xx:8733/test/Service1/“对服务进行调用 到这步就实现在控制台中实现webService的发布。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值