欢迎投稿

今日深度:

用SQL Server和XML学习Silverlight 2(1)(8)

构造一个WCF Web Service

我们选择将顾客的设计保存到SQL Server的机制是一个由web服务器托管的WCF Web Service,构造WCF Service模型是Jesse Liberty另一篇优秀指南的主题http://www.devx.com/RIA/link/38156)。

按照确认的最佳实践,我们的Service Contract包含一个界面中,Operation Contracts包括一个保存设计参数、通过UserID和设计IDAutoID)检索设计信息的方法。

[ServiceContract]
public interface ICustomerDesigns
{
    [OperationContract]
    List GetDesignsByUserId(string UserId);

    [OperationContract]
    int InsertNewCustomerWeddingImprintDesign
     (CustomerWeddingImprintDesign cstIDesign);

    [OperationContract]
    List GetDesignByAutoId(int AutoID);
}


虽然创建一个WCF Web Service的过程看起来让人生畏,并且代码也是及其冗长、复杂,但其实大部分代码都是由Visual Studio自动生成的,你只需要自己编写Service Contract部分的代码就可以了。

#region ICustomerDesigns Members


public List GetDesignsByUserId(string UserId)
{
    try
    {
        DBAccessDataContext dc = new DBAccessDataContext();
 var matchingDesigns = from CustomerWeddingImprintDesigns 
in dc.CustomerWeddingImprintDesigns
                 where CustomerWeddingImprint
                 Designs.UserID.Equals(UserId) 
                select CustomerWeddingImprintDesigns;
        return matchingDesigns.ToList();
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
        return null;
    }
}

public List 
GetDesignByAutoId(int AutoID)
{
   DBAccessDataContext dc = new 
DBAccessDataContext();
    var matchingDesigns = from CustomerWeddingImprintDesigns 
in dc.CustomerWeddingImprintDesigns
                          where 
CustomerWeddingImprintDesigns.AutoID.Equals(AutoID)
                          select CustomerWeddingImprintDesigns;
    return matchingDesigns.ToList();
}

public int InsertNewCustomerWeddingImprintDesign
(CustomerWeddingImprintDesign custIDesign)
{
    try
    {
        DBAccessDataContext dc = new 
DBAccessDataContext(); 
        dc.CustomerWeddingImprintDesigns.
InsertOnSubmit(custIDesign);                
        // Insert count of changes to be made to the database
        System.Data.Linq.ChangeSet changes = dc.GetChangeSet();
        dc.SubmitChanges();
        return changes.Inserts.Count;
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
        return 0;
    }
}

#endregion}

在这个示例中,简单到只需要写保存数据和检索数据的LINQ查询语句,因为.NET数据类型和SQL Server数据类型是一致的,在使用LINQ到SQL转换的过程中,会话结尾的数据类型保持兼容是最关键点饿,此外,由于Beta 2目前在调用SetValue方法时,要求所有参数的数据类型都必须精确正确允许事先进行无疑问的转换),必须遵守这个要求。

当运行Silverlight应用程序的服务器同时也是一个WCF service时,不需要Cross-Domain策略文件,然而,如果你是在本地运行和调试一个Silverlight应用程序,并引用了远程的WCF service时,或Silverlight应用程序是托管在有多个基础地址的服务器上时,是需要Cross-Domain策略文件的,Beta 1和Beta 2之间对Cross-Domain策略文件的格式要求是不同的,因此,如果你是从互联网上导出拷贝而来的代码,你需要注意选择合适的版本,更多关于Cross-Domain策略文件的信息你可以在www.wpflearningexperience.com上阅读小节标题为“配置Cross-Domain策略文件clientaccesspolicy.xml)”的内容。

实现问题

Joe已经在他的博客上写了一篇文章

http://www.wpflearningexperience.com/?p=55)集合了在我们网站开发过程中遇到的所有实现问题,因为与这些问题有关的具体解释在其他地方已经有了,这里我只列出与我们这个项目有关的一部分问题。
IIS托管的WCF Web Services有一个限制就是每个Service只能有一个基础地址,不幸的是,在有多基础地址的托管配置中,会造成WCF混乱,一个多重身份的例子就是domain.com和www.domain.com,如果托管配置的修改不是一个可选的,一个相对简单的工作区可以欺骗WCF进入一个看起来单一的基础地址,这个工作区包括创建一个自定义的service factory,并覆盖CreateServiceHost()方法,这样就会只返回IIS报告的第一个基础地址,Rob Zelthttp://www.devx.com/RIA/link/38158)和Rob Reynoldshttp://www.devx.com/RIA/link/38159)的博客文章提供了更详细的信息。


Silverlight 2创建一个自定义service factory

protected override ServiceHost CreateServiceHost(Type serviceType, Uri 
[] baseAddresses)
{ 
// If more than one base address exists then return the second 
// address, otherwise return the first address
 (protected override ServiceHost CreateServiceHost
(Type serviceType, Uri[] baseAddresses)
{                       
    // If more than one base address exists then return the second 
    // address, otherwise return the first address
    //(You might be wondering why we're picking the second address here 
    //rather than the first.
    //For us, the second address holds the correct address for our 
deployment domain
    //www.silverlightblend.com. You may need to modify this code to return 
    //a different/custom base address depending on your IIS server configuration. 
    if (baseAddresses.Length > 1)
    {
        return new ServiceHost(serviceType, baseAddresses[1]);
    }
    else
    {
        return new ServiceHost(serviceType, baseAddresses[0]);
    }            
}


将这段代码放在ServiceName.svc.cs文件中,与你的Service Contract同级,另一个要做的小小改动是将"Factory="YourNamespaceName.CustomHostFactory"添加到你的ServiceName.svc文件中,如:

<% @ ServiceHost 
Language="C#" Debug
="true" Service="DMSL2WeddingImprints_Web.CustomerDesigns" 
Factory="DMSL2WeddingImprints_Web.CustomHostFactory" 
CodeBehind="CustomerDesigns.svc.cs" %>


www.htsjk.Com true http://www.htsjk.com/shujukujc/19157.html NewsArticle 构造一个WCF Web Service 我们选择将顾客的设计保存到SQL Server的机制是一个由web服务器托管的WCF Web Service,构造WCF Service模型是Jesse Liberty另一篇优秀指南的主...
评论暂时关闭