Linux学习日记-WCF RestFul的部署(三)

一、关于WCF 的部署

    默认的wshttp风格的wcf是很容易部署上去的,但是这里给个建议尽量不要使用WCF的配置文件去部署尽管

我们都已经很熟悉了,在使用配置文件你会发现各种蛋疼的问题。

二、WCF Restful的部署

以下是简单的目录:

   

最主要的是主机的代码:

      注: 一定要用代码,而不用配置文件 否则帮助页、默认返回格式什么的以配置就报异常

接口IService 类
using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace Services
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract,WebGet(UriTemplate="test/{name}")]
        string GetData (string name);
    }
}

服务Service 类
using System;

namespace Services
{
    public class Service:IService
    {
        public string GetData(string name)
        {
            return name;        }
    }
}

主机启动服务的方法:

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using Services;

namespace Hosting
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            using (WebServiceHost host = new WebServiceHost (typeof(Services.Service))) {
                //host.AddServiceEndpoint(typeof(ICalculator), new WebHttpBinding(), "http://127.0.0.1:9999/");

                ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(Services.IService), new WebHttpBinding(), "http://127.0.0.1:9999/");
                if (host.Description.Behaviors.Find<WebHttpBehavior> () == null) {
                    WebHttpBehavior httpBehavior = new WebHttpBehavior ();
                    httpBehavior.HelpEnabled = true; //打开帮助页
                    httpBehavior.DefaultOutgoingResponseFormat = WebMessageFormat.Json;//指定返回格式为“Json”
                    httpBehavior.DefaultBodyStyle = WebMessageBodyStyle.Bare; //正文消息样式
                    httpBehavior.AutomaticFormatSelectionEnabled = false; //是否自动返回格式
                    endpoint.Behaviors.Add (httpBehavior);//添加终结点
                }
                host.Opened += delegate {
                    Console.WriteLine ("服务已启动!");
                };
                host.Open();
                Console.ReadKey();
            }
        }
    }
}

 

 

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace Services
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract,WebGet(UriTemplate="test/{name}")]
        string GetData (string name);
    }
}

转载于:https://www.cnblogs.com/liyangLife/p/4179778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值