分享

vs2013下的WCFRest 模板开发WCF

 昵称10504424 2014-02-27

在vs2013下使用wcfRestservice40 是安装不成功的,尝试多遍,都是这样。查看以前vs2012做的wcfrest,经过调教,终于在vs2013下也可以了!
 
1.新建wcf服务应用程序
 
2.调制配置文件
 

 

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </modules>
  </system.webServer>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!--
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
   
  </system.serviceModel>
</configuration>


3.添加全局应用程序类 Global.asax
 
  里面代码:
 

 

void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();
        }

        private void RegisterRoutes()
        {
            RouteTable.Routes.Add( new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
            //RouteTable.Routes.Add("test", new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
        }


4.新建Service1.cs类库 (以后作为服务的节点)
 

 

[ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service1
    {
        [WebGet(UriTemplate = "")]
        public List<SampleItem> GetCollection()
        {
            return  new List<SampleItem>(){new SampleItem(){Id = 1,StringValue = "Hello"}};
        }
    }
    public class SampleItem
    {
        public int Id { get; set; }
        public string StringValue { get; set; }
    }


5.运行调试,
 
刚运行的时候
 
运行服务,在当前地址后面添加上面的服务节点
 


可以再路径后面添加“help/”查看服务的各个方法。
 
到此结束!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多