Fast, simple test app for ADO.NET Data Serviceshttp://blogs.msdn.com/marcelolr/archive/2008/01/02/fast-simple-test-app-for-ado-net-data-services.aspx
Mike Taulty 的Webloghttp://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/12/19/10028.aspx
以及Christian Weyer的Webloghttp://blogs.thinktecture.com/cweyer/archive/2007/12/11/415057.aspx
都有讨论Astoria ServiceHost在一个控制台的应用中,因为目前版本的Astoria CTP只提供了WebHosting/ASP.NET(模式),当然这非常容易应用在Web开发的环境中,看了他们的Weblog,自己测试了一下,果然是可行的。
1.先创建一个控制台的应用(VS2008)2.增加一个ADO.NET Entity Data Model(或者是加入一个已经创建EDM Lib的项目引用) 创建一个ObjectContext,比如:BlogEntities3.创建SvcImp的cs文件,实现WebDataService (BlogSvcImp.cs)
using System;using System.Web;using System.Collections.Generic;using System.ServiceModel.Web;using System.Linq;using Microsoft.Data.Web;using BlogModel;
namespace SelfHostSvc{ public class BlogSvcSelfHost : WebDataService<BlogEntities> { public static void InitializeService(IWebDataServiceConfiguration config) { config.SetResourceContainerAccessRule("*", ResourceContainerRights.AllRead);
// Example for service operations // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All); }
}}
4.修改App.config
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <services> <service name="SelfHostSvc.BlogSvcSelfHost" behaviorConfiguration="webHttp_SelfHost"> <host> <baseAddresses> <add baseAddress="http://localhost:9909/DataServices/SelfHost/BlogSvc"/> </baseAddresses> </host> <endpoint address="" binding="webHttpBinding" contract="Microsoft.Data.Web.IRequestHandler" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="webHttp_SelfHost"> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
<connectionStrings> <add name="BlogEntities" connectionString="metadata=.\Blogs.csdl|.\Blogs.ssdl|.\Blogs.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\sqlexpress;Initial Catalog=Blog;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings></configuration>
5.控制台Main应用中增加下面的代码
static void Main(string[] args) {
WebServiceHost host = new WebServiceHost(typeof(BlogSvcSelfHost)); host.Open();
Console.WriteLine("Service starting....\nPress Enter to exit"); Console.ReadLine();
host.Close();
}
之后就可以启动这个控制台应用。在IE浏览器中访问下面的地址
http://localhost:9909/DataServices/SelfHost/BlogSvc
接着可以通过简单的REST命令简单访问服务http://localhost:9909/DataServices/SelfHost/BlogSvc/Blogs
用下面的链接获得Astoria服务的元数据信息http://localhost:9909/DataServices/SelfHost/BlogSvc/$metadata
从上面看出,通过控制台的Self-Hosting方式也可以实现Astoria Service,Astoria提供了一个通用的数据服务的架构和基础工具,借助.NET 3.5的WCF功能,你可以更加方便的开发自己的Internet数据服务。