[译]在WebService如何使用XPO

Web Service

     在Global.asax中的Application_Start程序创建了一个特定的数据库连接提供商(这是一个例子的MS Access数据库的)

  [C#中]

void Application_Start ( object sender , EventArgs e )
{
   Application [ "provider" ] = DevExpress . Xpo . XpoDefault . GetConnectionProvider (
   DevExpress . Xpo . DB . AccessConnectionProvider . GetConnectionString ( Server . MapPath   ( "App_Data\\ContactManagement.mdb" ) ) ,
  DevExpress . Xpo . DB . AutoCreateOption . SchemaAlreadyExists ) ;
}
 

 

 

     该MyXpoService Web服务是实现在App_Code文件\ MyXpoService.cs。 It's a WebService descendant.这是一个WebService的后裔。 It must expose four methods: SelectData , ModifyData , UpdateSchema and GetAutoCreateOption .它必须揭露四种方法:SelectData,ModifyData,UpdateSchemaGetAutoCreateOption。

 [C#中] [ WebService ( Namespace = WebServiceAttribute . DefaultNamespace ) ]
[ WebServiceBinding ( ConformsTo = WsiProfiles . BasicProfile1_1 ) ]
public class MyXpoService : System . Web . Services . WebService {
public MyXpoService ( ) { }
[ WebMethod ]
public ModificationResult ModifyData ( params ModificationStatement [ ] dmlStatements ) {
IDataStore provider = ( IDataStore ) Application [ "provider" ] ;
return provider . ModifyData ( dmlStatements ) ;
}
[ WebMethod ]
public SelectedData SelectData ( params SelectStatement [ ] selects ) {
IDataStore provider = ( IDataStore ) Application [ "provider" ] ;
return provider . SelectData ( selects ) ;
}

[ WebMethod ]
public UpdateSchemaResult UpdateSchema ( bool dontCreateIfFirstTableNotExist , params DBTable [ ] tables ) {
// do nothing (do not allow DB schema updates via a public Web service)
return UpdateSchemaResult . SchemaExists ;
}
[ WebMethod ]
public AutoCreateOption GetAutoCreateOption ( ) {
return AutoCreateOption . SchemaAlreadyExists ;
}
}

 

 

  出于安全原因,UpdateSchema方法不执行任何操作,并GetAutoCreateOption总是返回SchemaAlreadyExists。 You should create a separate application, which creates and updates your database's schema.您应该创建一个单独的应用程序,它创建和更新您的数据库的架构。

在此之前您发布您的Web服务,你需要改变它的命名空间参数,以一个独特的字符串。 For example:例如:

[C#]

[ WebService ( Namespace = "http://my-company-site.com/webservices/" ) ]

[ WebServiceBinding ( ConformsTo = WsiProfiles . BasicProfile1_1 ) ]
public class MyXpoService : System . Web . Services . WebService {
public MyXpoService ( ) { }
. . .

 

客户端应用程序

请使用ContactManagement演示XPO随测试MyXpoService。 启动ContactManagement.exe作为命令行参数中指定的Web服务的URL:

ContactManagement.exe http://localhost:2224/XpoGate/MyXpoService.asmx

 在您发布您的Web服务,你必须改变它的命名空间为一个唯一的名称。当你这样做,你将无法再使用XpoDefault.GetDataLayer方法,因为它会创建一个具有默认名称空间WebServiceDataStore实例。 你应该继承WebServiceDataStore阶级属性和装饰WebServiceBinding您的后代。

 

[C#中] using System . Web . Services ;

[ WebServiceBinding ( Namespace = "http://my-company-site.com/webservices/" ) ]
class MyWebDataStore : DevExpress . Xpo . WebServiceDataStore {
public MyWebDataStore ( string url )
: base ( url , DevExpress . Xpo . DB . AutoCreateOption . SchemaAlreadyExists ) {
}
}
 

 

 

下面是代码以初始化一个客户端应用程序的入口点XPO数据层:

 

 [C#中] using DevExpress . Xpo ;

[ STAThread ]
static void Main ( ) {
InitDAL ( ) ;
Application . Run ( new Form1 ( ) ) ;
}

private static void InitDAL ( ) {
string serviceUrl = "http://localhost:2224/XpoGate/MyXpoService.asmx" ;
XpoDefault . DataLayer = new SimpleDataLayer ( new MyWebDataStore ( serviceUrl ) ) ;
}

 

 

Web服务是创建AutoCreateOption等于SchemaAlreadyExists。 Hence, a client of the Web service will not be able to update the database schema.因此,一个Web服务客户端将无法更新数据库模式。 An attempt to call the UpdateSchema method will throw a SchemaCorrectionNeededException .试图调用UpdateSchema方法将抛出一个SchemaCorrectionNeededException。

    为了使您的Web服务安全存储,请使用IIS功能。 请验证身份的访问打开您的Web服务。 要启用数据加密,请使用了SSL(HTTPS)的协议。.这两个选项是从你的虚拟文件夹的属性在互联网信息服务提供控制台对话框。

 

转载于:https://www.cnblogs.com/tanliang/archive/2010/04/21/1717370.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值