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);
}
}
以沫浅夏----奔跑的孩子
个人博客地址:http://www.blog.liyang.love
个人博客地址:http://www.blog.liyang.love