Welcome to MSDN Blogs Sign in | Join | Help

Attach default code directory SQL mdf file to SQL expresss on Windows 2003

When following method of attaching sqlexpress mdf file from http://msdn.microsoft.com/en-us/library/ms165673.aspx , I meet an error in win2k3.  The procedure is:

 

1. Create a ASP web application in default location

2. Create a database inside app_data directory

3. Close the VS solution

4. In command line: sqlcmd -E -S .\sqlexpress

5. Do the following , and will see error:

 

1> USE [master]

2> GO

Changed database context to 'master'.

1> CREATE DATABASE [database1] ON

2> ( FILENAME = N'C:\Documents and Settings\username\My Documents\Visual Studio 2008\Projects\WebApplication1\WebApplication1\App_Data\database1.mdf' ),

3> ( FILENAME = N'C:\Documents and Settings\username\My Documents\Visual Studio 2008\Projects\WebApplication1\WebApplication1\App_Data\database1_log.ldf' )

4>  FOR ATTACH ;

5> GO

Msg 5133, Level 16, State 1, Server XINYANGQIU04-V1\SQLEXPRESS, Line 1

Directory lookup for the file "C:\Documents and Settings\username\My Documents\Visual Studio 2008\Projects\WebApplication1\WebApplication1\App_Data\database1.mdf

" failed with the operating system error 5(Access is denied.).

1>

 

Note, if  creating the WAP in a non-default directory, such as c:\others, we are fine.

 1> CREATE DATABASE [database_name] ON

2> ( FILENAME = N'C:\others\WebApplication1\WebApplication1\App_Data\database1.mdf' ),

3> ( FILENAME = N'C:\ others\WebApplication1\WebApplication1\App_Data\database1_log.ldf' )

4>  FOR ATTACH ;

5> GO

  -->No error.  And database is attached.

 

To workaround this,  add "[localComputerName]\Users" with read/Execute security permission to the default project location app_data folder

 

For information, the default security setting is as following

   Administrators

   username (current login user, admin)

   SYSTEM

       Each with full control

 

 In non-default location C:\others\WebApplication1\WebApplication1\App_Data, the security setting is:

   Administrastors (full control)

   Creator Owner (special permissions)

   SYSTEM (full control)

   Users (Read&Execute,List,Read, special permission)

Need to make service project web.config file Debug=True when debugging project with file system based web service.in VS2008

When debugging a web page which uses a file system based web service, I met an error message in Visual Studio 2008 which troubles me for a while.  Here are the steps and the solution.

1. Create a new C# file system based web site (website1)
2. Add a file system based web service web site into the solution, by file->Add->New web site. Name it webservice1.
3. In website1, add service reference to webservice1
4. In website1 default.aspx.cs file, add following to the page_load function
        ServiceReference1.ServiceSoapClient client = new ServiceReference1.ServiceSoapClient();
        Response.Write(client.HelloWorld());

5. In win2k3, add break point in service.cs HelloWorld function. In Vista, this is not a requirement.
6. F5 to debug default.aspx file, answer yes to change the website1’s web.config’s Debug=True.

7. An error message pops up:

Unable to automatically debug 'D:\...\WebSite1\'. The remote procedure could not be debugged. This usually indicates that debugging has not been enabled on the server.  See help for more information.

 

The problem is that Visual Studio didn’t change the webservice1’s web.config’s Debug=True during step 6.  I think the reason behind this is to prevent downgrading the security without proper warning.  So in order to debug it, you need to change the webservice1’s web.config debug setting yourself.

 

And don’t forget to change it back when release it.

 

VS2008调试使用了基于文件的网络服务的网页时,需将网络服务web.config中的Debug设为True

 

当我用VS2008调试一个使用了基于文件的网络服务的网页时,我遇到了一个让我花了一些时间的问题。下面是步骤和解决方法。(我是用的英文版,中文的翻译和实际VisualStudio上显示的会有出入。)

 

1. 新建一个新的C#基于文件系统的网站(website1)
2. 在解决方案中加入基于文件系统的网页服务,通过文件->添加->新网站 (file->Add->New web site)。叫它webservice1
3. website1中,加入webservice1的引用
4. website1default.aspx.cs的文件中,在page_load函数中加入
        ServiceReference1.ServiceSoapClient client = new ServiceReference1.ServiceSoapClient();
        Response.Write(client.HelloWorld());

5. 如果使用win2k3的操作系统,在service.cs文件中的HelloWorld函数加上断点。在Vista中,不需要这样做。
6. F5 来调试default.aspx 文件,当跳出将website1web.config文件改成Debug=True的问题框时,的选择

7. 遇到下面的错误信息:

Unable to automatically debug 'D:\...\WebSite1\'. The remote procedure could not be debugged. This usually indicates that debugging has not been enabled on the server.  See help for more information.

 

问题是Visual Studio在第6步时没有改变webservice1web.config文件Debug值为True。我想原因是为了安全考虑,不在用户不知情的情况下改变web.config的安全设置。所以,需要手工改变webservice1文件web.configdebug值。

 

不要忘了在发布时把debug设回成False

Posted by xinqiu | 1 Comments

Silverlight 2.0 Stock Quotation Demo Part 4/4: Cache the previous quoted symbols

The steps are:

1.       Adding the following functions to the Page.xaml.cs

 

        private string m_filePath = "TestHistoryStocks.txt";

        #region "Local Cache storage"

        /// <summary>

        /// Append the current cacheString to the local storage file, will remove all the duplicates to the original content

        /// </summary>

        /// <param name="cacheString"></param>

        private void StoreToLocalCache(string cacheString)

        {

            string existingSymbols = ReadFromLocalCache();

            string[] existingSymbolsSplit = existingSymbols.Split(m_separators, System.StringSplitOptions.RemoveEmptyEntries);

            string[] cacheStringSplit = cacheString.Split(m_separators, System.StringSplitOptions.RemoveEmptyEntries);

 

            foreach (string cacheSymbol in cacheStringSplit)

            {

                if (!existingSymbolsSplit.Contains(cacheSymbol))

                {

                    if (string.IsNullOrEmpty(existingSymbols))

                    {

                        existingSymbols += cacheSymbol;

                    }

                    else

                    {

                        existingSymbols += "," + cacheSymbol;

                    }

                }

            }

 

            try

            {

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())

                {

                    // Use a StringBuilder to construct output.

                    StringBuilder sb = new StringBuilder();

 

                    // Create three directories in the root.

                    store.CreateDirectory("TestStockQuotes");

 

                    // Create a file in the root.

                    IsolatedStorageFileStream rootFile = store.CreateFile(m_filePath);

                    rootFile.Close();

 

                    if (store.FileExists(m_filePath))

                    {

                        try

                        {

                            using (StreamWriter sw =

                                new StreamWriter(store.OpenFile(m_filePath,

                                    FileMode.Open, FileAccess.Write)))

                            {

                                sw.WriteLine(existingSymbols);

                            }

                        }

                        catch (IsolatedStorageException ex)

                        {

                            sb.AppendLine(ex.Message);

                        }

                    }

 

                    // remove the store

                    //store.Remove();

                }

            }

            catch (IsolatedStorageException)

            {

                // TODO: Handle that store was unable to be accessed.

            }

        }

 

        /// <summary>

        /// Read the text content from the localCache, will remove the end of line characters

        /// </summary>

        /// <returns></returns>

        private string ReadFromLocalCache()

        {

 

            try

            {

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())

                {

                    // Use a StringBuilder to construct output.

                    StringBuilder sb = new StringBuilder();

 

                    // Create a file in the root.

                    string contents = "";

 

                    // Read the contents of the file: MyApp1\SubDir1\MyApp1A.txt

                    try

                    {

                        using (StreamReader reader =

                            new StreamReader(store.OpenFile(m_filePath,

                                FileMode.Open, FileAccess.Read)))

                        {

                            contents = reader.ReadToEnd();

                        }

                    }

                    catch (IsolatedStorageException ex)

                    {

                        sb.AppendLine(ex.Message);

                    }

 

                    // remove the store

                    //store.Remove();

                    return contents.Trim().TrimEnd(new char[] { '\r', '\n' });

                }

            }

            catch (IsolatedStorageException)

            {

                // TODO: Handle that store was unable to be accessed.

            }

            return "";

        }

 

        /// <summary>

        /// Delete the local cache file

        /// </summary>

        private void DeleteLocalCache()

        {

            try

            {

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())

                {

                    // Use a StringBuilder to construct output.

                    StringBuilder sb = new StringBuilder();

 

                    // Delete a file.

                    try

                    {

                        if (store.FileExists(m_filePath))

                        {

                            store.DeleteFile(m_filePath);

                        }

                    }

                    catch (IsolatedStorageException ex)

                    {

                        sb.AppendLine(ex.Message);

                    }

 

                    // remove the store

                    store.Remove();

                }

            }

            catch (IsolatedStorageException)

            {

                // TODO: Handle that store was unable to be accessed.

            }

        }

#endregion

 

2.       Add functions and insert codes to call these functions:

 

In page.xaml:

        <StackPanel Orientation="Horizontal" >

            <Button Name="ButtonGetQuote" Background="Brown" Content="GetQuote" Click="ButtonGetQuote_Click" ></Button>

            <Button Name="ButtonClearCache" Margin="10,0,0,0" Background="Brown" Content="Clear Stored Cache" Click="ButtonClearCache_Click"></Button>

        </StackPanel>

 

        In page.xaml.cs:

        public Page()

        {

            InitializeComponent();

            TextBoxQuoteInput.Text = ReadFromLocalCache();

            if (!string.IsNullOrEmpty(TextBoxQuoteInput.Text))

            {

                RunQuery();

                StartRefresh();

            }

            TextBoxQuoteInput.Focus();

        }

 

        private void ButtonClearCache_Click(object sender, RoutedEventArgs e)

        {

            DeleteLocalCache();

        }

 

        private void BindItemSource()

        {

            if (m_count >= m_maxCount)

            {

                int x = dataGridStock.SelectedIndex;

                dataGridStock.ItemsSource = null;

                dataGridStock.ItemsSource = m_AllStocks.Values;

                dataGridStock.SelectedIndex = x;

 

                //remove the extra columns in the table

                while (dataGridStock.Columns.Count > 7)

                {

                    dataGridStock.Columns.RemoveAt(7);

                }

 

                StoreToLocalCache(TextBoxQuoteInput.Text);

            }

        }

 

3.       Ctrl-F5 a few times and quotes some symbols each time.  We should see the previous quoted symbols get cached each time we load the program.

 

This is what the final version should looks like:

 

4. As ASP.NET MVP Ken pointed out after I first published the blog, we need to add IsReadOnly="True" for data:DataGrid to make not throwing exception when editing the grid.
        <data:DataGrid x:Name="dataGridStock"
        Margin="0,5,0,10"
        RowHeight="44" 
        Height="500"
        VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" AlternatingRowBackground="LemonChiffon" IsReadOnly="True" >. I'll update the attached file as well.

Posted by xinqiu | 4 Comments
Filed under:

Silverlight 2.0 Stock Quotation Demo Part 3/4: Show different color for up and down quotes

The steps are:

1.       Change the column XAML for “Trade” to use cell template:

                <data:DataGridTemplateColumn Header="Trade" Width="80">

                    <data:DataGridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <TextBlock TextAlignment="Right" VerticalAlignment="Center"  FontSize="12" Text="{Binding last}" Foreground ="{Binding lastForegroundColor}"></TextBlock>

                        </DataTemplate>

                    </data:DataGridTemplateColumn.CellTemplate>

         </data:DataGridTemplateColumn>

 

2.       Adding a function to determine the color of row:

 

        private String GetLastForegroundColor(string last, string previousClose)

        {

            if (Convert.ToDouble(last) > Convert.ToDouble(previousClose))

            {

                return "Green";

            }

            else if (Convert.ToDouble(last) < Convert.ToDouble(previousClose))

            {

                return "Red";

            }

            else

            {

                return "Black";

            }

        }

      

        Adding the following line into the DisplayInDataGrid function and new OneStock section.

lastForegroundColor = GetLastForegroundColor(g.Element("Last").Value, g.Element("PreviousClose").Value)

 

3.       Ctrl-F5 and we should see the “Trade” column show either green, red depending on the current stock price.

 

Posted by xinqiu | 1 Comments
Filed under:

Silverlight 2.0 Stock Quotation Demo Part 2/4: Basic stock quotation

The steps are:

1.       New Project, select C#/Silverlight/SilverilghtApplication.  Click OK to “Add Silverlight Application” dialog, which defaults to “Add a new ASP.NET Web project to the solution to host Silverlight”.  I named the project as StockQuotesForBlog.

2.       Change page.xaml’s width and height to 800x600, and add the graphical interface into the XAML:

 

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="StockQuotesForBlog.Page"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="800" Height="600">

 

    <StackPanel Margin="10,10,10,10">

 

        <TextBlock Name="TextBlockQuotes" Text="Please enter a symbol, or symbols seperated by comma"></TextBlock>

        <TextBox x:Name="TextBoxQuoteInput" Text="MSFT,GOOG,YHOO" KeyDown="TextBoxQuoteInput_KeyDown"></TextBox>

        <StackPanel Orientation="Horizontal" >

            <Button Name="ButtonGetQuote" Background="Brown" Content="GetQuote" Click="ButtonGetQuote_Click" ></Button>

        </StackPanel>

 

        <data:DataGrid x:Name="dataGridStock"

        Margin="0,5,0,10"

        RowHeight="44" 

        Height="500"

        VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" AlternatingRowBackground="LemonChiffon" >

            <data:DataGrid.Columns>

                <data:DataGridTextColumn

                    Header="Symbol"

                    Width="80"

                    Binding ="{Binding symbol}"

                    FontSize="12" />

                <data:DataGridTextColumn

                    Header="Company"

                    Width="120"

                    Binding="{Binding name}"

                    FontSize="12" />

                <data:DataGridTextColumn

                    Header="Time"

                    Width="80"

                    Binding="{Binding time}"

                    FontSize="12" />

                <data:DataGridTextColumn

                    Header="Trade"

                    Width="80"

                    Binding="{Binding last}"

                    FontSize="12" />

                <data:DataGridTextColumn

                    Header="Change"

                    Width="80"

                    Binding="{Binding change}"

                    FontSize="12" />

                <data:DataGridTextColumn

                    Header="%Chg"

                    Width="80"

                    Binding="{Binding percentageChange}"

                    FontSize="12" />

                <data:DataGridTextColumn

                    Header="Volume"

                    Width="80"

                    Binding="{Binding volume}"

                    FontSize="12" />

            </data:DataGrid.Columns>

        </data:DataGrid>

    </StackPanel>

</UserControl>

 

3.       Add a code file, Stock service.cs

 

namespace StockQuotesForBlog

{

    public class OneStock

    {

        public string symbol { get; set; }

        public string last { get; set; }

        public string date { get; set; }

        public string time { get; set; }

        public string change { get; set; }

        public string open { get; set; }

        public string high { get; set; }

        public string low { get; set; }

        public string volume { get; set; }

        public string mktCap { get; set; }

        public string previousClose { get; set; }

        public string percentageChange { get; set; }

        public string annRange { get; set; }

        public string earns { get; set; }

        public string pe { get; set; }

        public string name { get; set; }

 

        private string _lastForegroundColor = "black";

 

        public string lastForegroundColor

        {

            get

            {

                return _lastForegroundColor;

            }

            set

            {

                _lastForegroundColor = value;

            }

        }

    }

}

 

 

4.       Add service reference of System.XML.Linq

5.       In Page.xaml.cs, we add the following code for the basic functionalities:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

using System.Xml;

using System.Xml.Linq;

using System.IO;

using System.IO.IsolatedStorage;

using System.Text;

using System.Collections;

 

namespace StockQuotesForBlog

{

    public partial class Page : UserControl

    {

 

        private Char[] m_separators = new Char[] { ' ', ',', ';' };

 

        private Storyboard m_sb = null;

        private Dictionary<string, OneStock> m_AllStocks = new Dictionary<string, OneStock>();

        private int m_count = 0;

        private int m_maxCount = 0;

 

        public Page()

        {

            InitializeComponent();

            TextBoxQuoteInput.Focus();

        }

 

 

        private void RunQuery()

        {

            string symbols = TextBoxQuoteInput.Text.Trim();

 

            string[] split = symbols.Split(m_separators, System.StringSplitOptions.RemoveEmptyEntries);

 

            m_count = 0;

            m_maxCount = split.Count();

 

            foreach (string symbol in split)

            {

                string url = "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=" + symbol;

 

                // Initiate Async Network call to Digg

                WebClient stockQuoteService = new WebClient();

                stockQuoteService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(stockQuoteService_DownloadStringCompleted);

                stockQuoteService.DownloadStringAsync(new Uri(url));

            }

        }

 

        void stockQuoteService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

        {

            if (e.Error == null)

                DisplayInDataGrid(e.Result);

        }

 

        private void DisplayInDataGrid(string xmlString)

        {

            XDocument xd1 = XDocument.Parse(xmlString);

            xmlString = xd1.Root.Value;

 

            XDocument xmlStories = XDocument.Parse(xmlString);

 

            var stocks = from g in xmlStories.Descendants("Stock")

                         select new OneStock

                         {

                             symbol = g.Element("Symbol").Value,

                             last = g.Element("Last").Value,

                             date = g.Element("Date").Value,

                             time = g.Element("Time").Value,

                             change = g.Element("Change").Value,

                             open = g.Element("Open").Value,

                             high = g.Element("High").Value,

                             low = g.Element("Low").Value,

                             volume = g.Element("Volume").Value,

                             mktCap = g.Element("MktCap").Value,

                             previousClose = g.Element("PreviousClose").Value,

                             percentageChange = g.Element("PercentageChange").Value,

                             annRange = g.Element("AnnRange").Value,

                             earns = g.Element("Earns").Value,

                             pe = g.Element("P-E").Value,

                             name = g.Element("Name").Value,

                         };

 

            m_count++;

 

            foreach (OneStock stock in stocks)

            {

                if (m_AllStocks.ContainsKey(stock.symbol))

                {

                    m_AllStocks[stock.symbol].last = stock.last;

                    m_AllStocks[stock.symbol].date = stock.date;

                    m_AllStocks[stock.symbol].time = stock.time;

                    m_AllStocks[stock.symbol].change = stock.change;

                    m_AllStocks[stock.symbol].open = stock.open;

                    m_AllStocks[stock.symbol].high = stock.high;

                    m_AllStocks[stock.symbol].low = stock.low;

                    m_AllStocks[stock.symbol].volume = stock.volume;

                    m_AllStocks[stock.symbol].mktCap = stock.mktCap;

                    m_AllStocks[stock.symbol].previousClose = stock.previousClose;

                    m_AllStocks[stock.symbol].percentageChange = stock.percentageChange;

                    m_AllStocks[stock.symbol].annRange = stock.annRange;

                    m_AllStocks[stock.symbol].earns = stock.earns;

                    m_AllStocks[stock.symbol].pe = stock.pe;

 

                    m_AllStocks[stock.symbol].lastForegroundColor = stock.lastForegroundColor;

                }

                else

                {

                    m_AllStocks.Add(stock.symbol, stock);

                }

            }

            BindItemSource();

        }

 

        private void BindItemSource()

        {

            if (m_count >= m_maxCount)

            {

                int x = dataGridStock.SelectedIndex;

                dataGridStock.ItemsSource = null;

                dataGridStock.ItemsSource = m_AllStocks.Values;

                dataGridStock.SelectedIndex = x;

 

                //remove the extra columns in the table

                while (dataGridStock.Columns.Count > 7)

                {

                    dataGridStock.Columns.RemoveAt(7);

                }

            }

        }

 

        private void ButtonGetQuote_Click(object sender, RoutedEventArgs e)

        {

            RunQuery();

            StartRefresh();

        }

 

        private void TextBoxQuoteInput_KeyDown(object sender, KeyEventArgs e)

        {

            switch (e.Key)

            {

                case Key.Enter:

                    RunQuery();

                    StartRefresh();

                    e.Handled = true;

                    break;

                default:

                    e.Handled = false;

                    break;

            }

        }

 

        private void StartRefresh()

        {

            if (m_sb == null)

            {

                m_sb = new Storyboard();

                m_sb.Completed += Storyboard_Completed;

                Duration duration = new Duration(TimeSpan.FromSeconds(10));

                m_sb.Duration = duration;

            }

            m_sb.Begin();

        }

 

        private void Storyboard_Completed(object sender, EventArgs e)

        {

            RunQuery();

            m_sb.Begin();

        }

    }

}

 

6.       Ctrl F5, and click GetQuote Button, to see the basic query in action.

Posted by xinqiu | 1 Comments
Filed under:

Silverlight 2.0 Stock Quotation Demo Part 1/4: Basic Design

Here is a Silverlight demo for getting stock quotes using data grid.  It also demos how to use stored cache to save all the queried symbol before.

 

The design would like:

 

I used Visual Studio 2008 SP1 with Silverlight 2.0 RC.  For stock quotation service, I chose http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=SYMBOLNAME for the demo.

 

The service returned the following which contains interesting double layered XML string.

<?xml version="1.0" encoding="utf-8" ?>

  <string xmlns="http://www.webserviceX.NET/"><StockQuotes><Stock><Symbol>MSFT</Symbol><Last>25.50</Last><Date>10/13/2008</Date><Time>4:00pm</Time><Change>+4.00</Change><Open>N/A</Open><High>25.50</High><Low>22.63</Low><Volume>143039616</Volume><MktCap>232.8B</MktCap><PreviousClose>21.50</PreviousClose><PercentageChange>+18.60%</PercentageChange><AnnRange>20.65 - 37.50</AnnRange><Earns>1.867</Earns><P-E>11.52</P-E><Name>MICROSOFT CP</Name></Stock></StockQuotes></string>

 

The complete project can be downloaded from the attached file to this blog.  The current update added IsReadOnly="True"  to data:DataGrid in page.xaml, by Ken's suggestion.

Posted by xinqiu | 1 Comments
Filed under:

Attachment(s): StockQuotesForBlog.zip

Siverlight 2 Beta 2 Balloon shooting tutorial Part 4/4, Create Silverlight project, modify the main control Page.xaml

Step 5. Replace the Page.xaml control with the following XAML:

 

<UserControl x:Class="ShootBalloonForBlog.Page"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="800" Height="600">

    <Canvas x:Name="LayoutRoot" Background="White">

        <MediaElement x:Name="BackMusic" Source="Symphony_No_3.wma" AutoPlay="False"></MediaElement>

        <Button Name ="ButtonPlay"  Canvas.Left="650" Canvas.Top="50" Height="20" Width="50" Content="Play" Click="Button_Click_PLay"></Button>

        <Button Canvas.Left="650" Canvas.Top="100" Height="20" Width="50" Content="Stop" Click="Button_MouseLeftButtonDown_StopTest"></Button>

 

        <TextBlock Canvas.Left="650" Canvas.Top="200" Height="20" Width="30" Text="Level" FontSize="11"></TextBlock>

        <TextBox Name="Textlevel" Canvas.Left="690" Canvas.Top="200" Height="20" Width="30" Text="1" FontSize="11"></TextBox>

 

        <TextBlock Canvas.Left="650" Canvas.Top="250" Height="20" Width="30" Text="Score:" FontSize="11"></TextBlock>

        <TextBlock Name="TextScore" Canvas.Left="690" Canvas.Top="250" Height="20" Width="200" Text="0" FontSize="11"></TextBlock>

 

    </Canvas>

</UserControl>

 

Step 6. Replace the Page.xaml.cs with the following code:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace ShootBalloonForBlog

{

    public partial class Page : UserControl

    {

        public Page()

        {

            InitializeComponent();

        }

 

        private Storyboard m_sb = null;

        private int m_score = 0;

 

        private int m_currentLevel = 1;

        private int DisplayLevel

        {

            get

            {

                int level;

                try

                {

                    level = Convert.ToInt32(Textlevel.Text);

                }

                catch

                {

                    level = 1;

                }

                Textlevel.Text = level.ToString();

                return level;

            }

        }

 

        private void AnimateBallonTest()

        {

            m_score = 0;

            TextScore.Text = m_score.ToString();

 

            m_sb = new Storyboard();

            m_sb.Completed += Storyboard_Completed;

 

            RepeatBehavior repeatOnce = new RepeatBehavior(1);

 

            m_currentLevel = DisplayLevel;

 

            //Generate random ballons

            Random random = new Random();

 

            for (int i = 0; i < 60 * m_currentLevel; i++)

            {

                double scaleFactor = (double)(random.Next(100, 1000)) / 1000;    // controls the size of the balloon .1 to 1

               

                // calculate and set our balloon color using an extension method that help keep the saturation up

                Color myColor = Color.FromArgb((byte)random.Next(100, 255),

                    (byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));

                myColor.A = (byte)(255 - (byte)(100 * scaleFactor));

               

                // create a new balloon

                Balloon balloon1 = new Balloon(scaleFactor, myColor);

 

                LayoutRoot.Children.Add(balloon1);

                int startingTop = random.Next(500, 2000);

                int startingLeft = random.Next(1, 10) * 55;

                int travelTime = random.Next(10, 20);   //the maximum is 20 seconds

                Duration duration = new Duration(TimeSpan.FromSeconds(travelTime));

                Duration duration1Sec = new Duration(TimeSpan.FromSeconds(1));

 

                SetPropertyDoubleAnimToStoryBoardBeta2(m_sb, duration, repeatOnce, balloon1, new PropertyPath("Opacity"), 1, 0.5);

                SetPropertyDoubleAnimToStoryBoardBeta2(m_sb, duration, repeatOnce, balloon1, new PropertyPath("(Canvas.Left)"), startingLeft, startingLeft);

                SetPropertyDoubleAnimToStoryBoardBeta2(m_sb, duration, repeatOnce, balloon1, new PropertyPath("(Canvas.Top)"), startingTop, -120); //max height of balloon is 120

 

                balloon1.Text.Text = BalloonAction.GetRandomAction(random);

 

                balloon1.MouseLeftButtonDown += ClickOnBalloon;

            }

            // Make the Storyboard a resource.

            if (!LayoutRoot.Resources.Contains("ResourceStoryBoard1"))

            {

                LayoutRoot.Resources.Add("ResourceStoryBoard1", m_sb);

            }

 

            // Begin the animation.

            m_sb.Begin();

 

            BackMusic.Play();

        }

 

 

        private void Storyboard_Completed(object sender, EventArgs e)

        {

            TextScore.Text += " Final";

            ButtonPlay.IsEnabled = true;

            BackMusic.Stop();

            clearBalloons();

 

            LayoutRoot.Resources.Remove("ResourceStoryBoard1");

        }

 

        private void ClickOnBalloon(object sender, MouseButtonEventArgs e)

        {

            Balloon ballon = (Balloon)sender;

            m_score = BalloonAction.EvaluateAciton(ballon.Text.Text, m_score);

            TextScore.Text = m_score.ToString();

            ballon.Visibility = Visibility.Collapsed;

        }

 

        private void clearBalloons()

        {

            int i = 0;

            while (i < LayoutRoot.Children.Count)

            {

                UIElement element = LayoutRoot.Children[i];

                if (element.GetType() == Type.GetType("ShootBalloonForBlog.Balloon"))

                {

                    LayoutRoot.Children.Remove(element);

                    continue;

                }

                i++;

            }

        }

 

        private void Button_MouseLeftButtonDown_StopTest(object sender, RoutedEventArgs e)

        {

            if (m_sb != null)

            {

                clearBalloons();

                m_sb.Stop();

 

                BackMusic.Stop();

                //LayoutRoot.Children.RemoveAt(2);

            }

 

            ButtonPlay.IsEnabled = true;

        }

 

        private void SetPropertyDoubleAnimToStoryBoardBeta2(Storyboard sb, Duration duration,

                    RepeatBehavior repeateBehavior, DependencyObject target,

                    PropertyPath property, double fromValue, double toValue)

        {

            DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();

            myDoubleAnimation1.Duration = duration;

            sb.Children.Add(myDoubleAnimation1);

            myDoubleAnimation1.RepeatBehavior = repeateBehavior;

            Storyboard.SetTarget(myDoubleAnimation1, target);

 

            Storyboard.SetTargetProperty(myDoubleAnimation1, property);

            myDoubleAnimation1.From = fromValue;

            myDoubleAnimation1.To = toValue;

        }

 

        private void Button_Click_PLay(object sender, RoutedEventArgs e)

        {

            ButtonPlay.IsEnabled = false;

            AnimateBallonTest();

        }

    }

}

 

Step 7. Now you should be able to run this application inside Silverlight 2 Beta 2.  Enjoy

 

Updated 10/03/2008: Thanks for the comments.  I uploaded the project file here:

 http://xqiu.members.winisp.net/download/ShootBalloonForBlog.zip

Posted by xinqiu | 5 Comments
Filed under:

Siverlight 2 Beta 2 Balloon shooting tutorial Part 3/4, Create Silverlight project, add BalloonAction.cs

Step 4 . Add BalloonActions.cs class file with the following code:

 

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace ShootBalloonForBlog

{

    public class BalloonAction

    {

        private static string[] nameAction =

            {

             "","","","","",

             "","","","","",

             "","","","","",

             "","","","","",

 

             "+2",

             "+3",

             "+4",

             "+5",

 

             "-1",

             "-2",

             "-1",

             "-2",

 

             "*2",

             "*3",

             "*4",

             "*5",

 

             "/2",

             "*0",

 

             "??",  //has to be the last!

            };

 

        private static string[] nameActionQuestionMark =

            {

             "*4",

             "*5",

             "/2",

             "*0",

            };

 

        public static string GetRandomAction(Random random)

        {

            return nameAction[random.Next(nameAction.Length)];

        }

 

        public static int EvaluateAciton(string action, int oriValue)

        {

            int retValue = oriValue;

            if (action.Length == 0)

            {

                return oriValue + 1;

            }

            try

            {

                switch (action[0])

                {

                    case '+':

                        retValue = oriValue + Convert.ToInt32(action.Substring(1));

                        break;

                    case '-':

                        retValue = oriValue - Convert.ToInt32(action.Substring(1));

                        break;

                    case '/':

                        retValue = oriValue / Convert.ToInt32(action.Substring(1));

                        break;

                    case '*':

                        retValue = oriValue * Convert.ToInt32(action.Substring(1));

                        break;

                    case '?':

                        Random random = new Random();

                        string randomExpression = nameActionQuestionMark[random.Next(nameActionQuestionMark.Length)];

                        retValue = EvaluateAciton(randomExpression, oriValue);

                        break;

                }

            }

            catch

            {

            }

 

            if (retValue < 0) retValue = 0;

 

            return retValue;

        }

    }

}

Posted by xinqiu | 2 Comments
Filed under:

Siverlight 2 Beta 2 Balloon shooting tutorial Part 2/4, Create Silverlight project, add balloon user control

I’m using Visual Studio 2008 SP1 with Silverlight 2 Beta 2 SDK.

 

1.       New Project, select C#/Silverlight/SilverilghtApplication, if there is any dialog asking options, select OK for default.  I named the project as ShootBalloonForBlog.

2.       Add a new user control file Balloon.xaml , by right click silverlight project item, Add-> New Item …-> Silverlight user control.

3.       Replace the Balloon.xaml user control with the following XAML:

 

(The balloon shape and XAML is referenced from code: http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2007/07/27/Silverlight-Balloons-Version-3.aspx)

 

<UserControl x:Class="ShootBalloonForBlog.Balloon"

    xmlns="http://schemas.microsoft.com/client/2007"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="100" Height="120">

    <Canvas x:Name="LayoutRoot" Background="Transparent">

        <Canvas.RenderTransform>

            <ScaleTransform x:Name="BalloonScaleTransform" ScaleX="1" ScaleY="1" />

        </Canvas.RenderTransform>

 

        <Path x:Name="Path" Width="101" Height="116.623" Canvas.Left="-0.5" Canvas.Top="-0.5" Stretch="Fill" Data="F1 M 50,0C 77.6142,0 100,22.3858 100,50C 100,71.6885 81.9116,86.6289 66.7874,99.7709C 62.5278,103.472 55.8424,108.306 49.9166,108.306C 44.2735,108.306 38.0278,103.639 33.7729,100.067C 17.8186,86.6724 0,71.9711 0,50C 0,22.3858 22.3858,0 50,0 Z M 51.2289,115.623L 43.8216,113.843L 49.0668,108.319L 51.2289,115.623 Z " StrokeLineJoin="Round">

            <Path.Stroke>

                <LinearGradientBrush StartPoint="0.276127,0.226989" EndPoint="0.930833,0.226989">

                    <LinearGradientBrush.RelativeTransform>

                        <TransformGroup>

                            <SkewTransform CenterX="0.276127" CenterY="0.226989" AngleX="-7.27327" AngleY="0"/>

                            <RotateTransform CenterX="0.276127" CenterY="0.226989" Angle="55.1587"/>

                        </TransformGroup>

                    </LinearGradientBrush.RelativeTransform>

                    <LinearGradientBrush.GradientStops>

                        <GradientStop Color="#FFFFFFFF" Offset="0.118644"/>

                        <GradientStop x:Name="StrokeColorStop" Color="#FFFF0000" Offset="1"/>

                    </LinearGradientBrush.GradientStops>

                </LinearGradientBrush>

            </Path.Stroke>

            <Path.Fill>

                <RadialGradientBrush RadiusX="0.654706" RadiusY="0.694299" Center="0.276127,0.226989" GradientOrigin="0.276127,0.226989">

                    <RadialGradientBrush.GradientStops>

                        <GradientStop Color="#FFFFFFFF" Offset="0.118644"/>

                        <GradientStop x:Name="FillColorStop" Color="#FFFF0000" Offset="1"/>

                    </RadialGradientBrush.GradientStops>

                    <RadialGradientBrush.RelativeTransform>

                        <TransformGroup>

                            <SkewTransform CenterX="0.276127" CenterY="0.226989" AngleX="-7.27327" AngleY="0"/>

                            <RotateTransform CenterX="0.276127" CenterY="0.226989" Angle="55.1587"/>

                        </TransformGroup>

                    </RadialGradientBrush.RelativeTransform>

                </RadialGradientBrush>

            </Path.Fill>

        </Path>

        <TextBlock Name="Text" Canvas.Left="40" Canvas.Top="40" Text="??" FontSize="16" FontWeight="700"></TextBlock>

    </Canvas>

</UserControl>

 

In Balloon.xaml.cs file: (The missing part from the 1st time blog 2 weeks ago.)

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace ShootBalloonForBlog

{

    public partial class Balloon : UserControl

    {

        private Color _color;

        private double _scale;

 

        public Balloon(double scale, Color color)

        {

            InitializeComponent();

 

            _color = color;

            _scale = scale;

 

            Loaded += new RoutedEventHandler(Balloon_Loaded);

        }

 

        void Balloon_Loaded(object sender, RoutedEventArgs e)

        {

 

            // set stroke and fill colors

            FillColorStop.Color = _color;

            StrokeColorStop.Color = _color;

 

            // scale to the size specified

            BalloonScaleTransform.ScaleX = _scale;

            BalloonScaleTransform.ScaleY = _scale;

 

        }

    }

}

Posted by xinqiu | 2 Comments
Filed under:

Siverlight 2 Beta 2 Balloon shooting tutorial Part 1/4, Design

Here’s the demo of how to write a simple silverlight game:

 

Design:

I played a computer game Monopoly before.  There is a fun balloon shooting game which I’d like to design myself.  The player clicks on the flying balloons and get the marks corresponding to the balloon face value.  Here are some face values, empty value means “+1” to the current score, “+3” means “+3” to the current score, “*5” means multiplying the current score with 5.  Face values, such as “/2”, “-3”,”*0” are certainly bad ones.  Face value “?” is the random chosen face values which can be good or bad.

 

The current scores should be updated immediately.

 

There should be a start/stop button.

 

There should be a level control how many balloons appearing in the background.  The default should be 1.

 

Updated 10/03/2008: Thanks for the comments.  I uploaded the project file here:

 http://xqiu.members.winisp.net/download/ShootBalloonForBlog.zip

 

Posted by xinqiu | 2 Comments
Filed under:

A simple function to get target file property names

I wrote a simple function to get all property name from a msbuild target file.  Such as visual studio’s Microsoft.WebApplication.targets file in direcotry of “%ProgramFiles%\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications\”.  It checks for possible property name case mismatch as well.  The properNames in the target file is in format of $(xxx).  So the function here is really simple.

 

        private string GetTargetsFromFile(string filePath)

        {

            if (!System.IO.File.Exists(filePath))

            {

                MessageBox.Show(String.Format("{0} does not exist!", filePath));

                return;

            }

            string content = System.IO.File.ReadAllText(filePath);

            string properties = "";

 

            Dictionary<string, string> PropertyDict = new Dictionary<string, string>();

            Dictionary<string, string> NoCasePropertyDict = new Dictionary<string, string>();

 

            //find all of the $(propertyName)

            Regex r = new Regex(@"\$\((.*?)\)");

 

            MatchCollection mc;

 

            mc = r.Matches(content);

            // Loop through the match collection to retrieve all

            // matches and positions.

            for (int i = 0; i < mc.Count; i++)

            {

                string value = mc[i].Value.Substring(2, mc[i].Value.Length - 3);

                // Add the match string to the string array.

                if (!PropertyDict.Keys.Contains(value))

                {

                    if (NoCasePropertyDict.Keys.Contains(value.ToUpperInvariant()))

                    {

                        properties += "Warning, non-matching name (already defind before) found in character location of " + mc[i].Index.ToString() + ", its substring is \r\n";

                        properties += content.Substring(mc[i].Index, 200) + "\r\n\r\n";

                    }

                    else

                    {

                        NoCasePropertyDict.Add(value.ToUpperInvariant(), "");

                        PropertyDict.Add(value, "");

                    }

                }

                // Record the character position where the match was found.

                //matchposition[i] = mc[i].Index;  

            }

 

            foreach (string value in PropertyDict.Keys)

            {

                properties += "        Private m_" + value + " As String = \"" + value + "\"\r\n";

            }

 

            Return properties;

        }

    }

 

我写了一个简单的函数,可以将msbuild的目标文件(target)的所有属性名称提出。如在“%ProgramFiles%\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications\”目录下的visual studio文件 Microsoft.WebApplication.targets。函数检查可能的属性名称大小写不一致的问题。在目标文件中,属性名称的格式是$(xxx),所以相关的函数很简单。 

Posted by xinqiu | 1 Comments
Filed under: ,

Service referencing ADO.Net Data Service

Visual Studio 2008 SP1 provides an easy way to reference the ADO.Net Data Service (formally code named as Astoria). 

(  Visual Studio 2008 SP1 and Visual Web Developer 2008 SP1 are now available for download. )

 

The following example uses the Astoria service we created in:

http://blogs.msdn.com/xqiu/archive/2008/05/13/ado-net-data-service-reflection-class-creation-in-vs2008-sp1-beta-a-breaking-change-with-ctp-time.aspx

 

1.       In the same solution, add a new C# website project

2.       Right click the website name, and choose Add Service Reference … menu item

3.       Click Discover button, and click the + sign of the found service provider under “Services” tree, you should get a dialog similar to the following dialog.  Note, this is the same Add Service Reference dialog that we used to add WCF service.

Add web service reference

 

4.       Click OK.
Note:
a) You don’t need to remember “
http://localhost:Port#/websiteDSReflectionProviderCSAddNS/WebDataService1.svc” address, as you can find it later in the auto-generated datasvcmap file under App_WebReferences folder.
b) I left the Namespace as default, you need to generate your own Namespace.
c) ADO.Net Data Service is simpler than WCF service, so there is no use for “Advanced …” button

5.       Add the following code to the page_load function, and run it to see the result for yourself:

 

   protected void Page_Load(object sender, EventArgs e)

    {

        try

        {

            //Note: this Uri port number is changed randomly in website

            String urlstr = "http://localhost:65110/websiteDSReflectionProviderCSAddNS/WebDataService1.svc";

            ServiceReference1.CellPhoneContainer ctx = new ServiceReference1.CellPhoneContainer(new Uri(urlstr));

 

            //Most complexed query

            System.Data.Services.Client.DataServiceQuery<ServiceReference1.CellPhone> cellPhones =

                ctx.CreateQuery<ServiceReference1.CellPhone>("/CellPhones");

            foreach (ServiceReference1.CellPhone c in cellPhones)

            {

                Response.Write(c.ModelName + "," + c.Price + "," + c.Bluetooth + "<BR>");

            }

 

            //URI

            //Should not use CreateQuery method with parameter ?$, use Execute instead

            var resultsSorted1 = ctx.Execute<ServiceReference1.CellPhone>(new Uri(urlstr + "/CellPhones?$orderby=Price"));

 

            //linq

            var resultsSorted2 = from table1 in ctx.CellPhones orderby table1.Price select table1;

 

            //lamnda

            var resultsSorted3 = ctx.CellPhones.OrderBy(table1 => table1.Price);

 

            foreach (ServiceReference1.CellPhone c in resultsSorted1)

            {

                Response.Write("URI query: " + c.ModelName + "," + c.Price + "," + c.Bluetooth + "<BR>");

            }

 

            foreach (ServiceReference1.CellPhone c in resultsSorted2)

            {

                Response.Write("LINQ query: " + c.ModelName + "," + c.Price + "," + c.Bluetooth + "<BR>");

            }

 

            foreach (ServiceReference1.CellPhone c in resultsSorted3)

            {

                Response.Write("lamnda query: " + c.ModelName + "," + c.Price + "," + c.Bluetooth + "<BR>");

            }

        }

        catch (Exception ex)

        {

            Response.Write(ex.Message);

        }

 

    }

 

For more about ADO.Net Data service, please check “Expose And Consume Data in A Web Services World” in http://msdn.microsoft.com/en-us/magazine/cc748663.aspx

 

===============

中文:

Visual Studio 2008 SP1 提供了一个简单的引用ADO.Net 数据服务(以前代号 Astoria) 的方式。

 

例子使用了我在下面的链接里提供的数据服务:

http://blogs.msdn.com/xqiu/archive/2008/05/13/ado-net-data-service-reflection-class-creation-in-vs2008-sp1-beta-a-breaking-change-with-ctp-time.aspx

 

1.       在同一个解决方案里,加入一个新的C#网页项目

2.       右击网页项目名称,选择“添加服务引用”的菜单

3.       在打开的对话框中点击发现按钮,然后点击在服务列表中发现的服务旁的加号。这个对话框和加入WCF服务的是一样的。

4.       点击确定。
注意:
a) 需要记住 “
http://localhost:Port#/websiteDSReflectionProviderCSAddNS/WebDataService1.svc” 的地址,或者以后可以在引用目录下的自动生成的datasvcmap文件中找到。
b) 对话框中有命名空间的缺省值,你可能需要改成自己需要的。
c) ADO.Net 数据服务比普通的WCF 服务要简单一些,所以不需要使用高级按钮。

5.       将上面的程序加入到page_load函数,看看运行的结果。

One way of handling <iframe> redirect using a asp.net button.

Here is a simple way to use iframe in asp.net.  In responding to a Chinese asp.net forum question, I write the following code to direct a new page to the iframe when clicking a button.

 

Here’s the sample code I write with VS2008.

 

<%@ Page Language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script type="text/javascript">

   function Buton1ClientClick()

   {

      document.forms["form2"].submit();

   }

</script>

 

<script runat="server">

 

   protected void Button1_Click(object sender, EventArgs e)

   {

      Response.Write("adsf");

   }

 

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

       <asp:ScriptManager ID="ScriptManager1" runat="server">

       </asp:ScriptManager>

       <asp:UpdatePanel ID="UpdatePanel1" runat="server">

          <ContentTemplate>

             <asp:Button ID="Button1" runat="server" Text="Button"

          OnClientClick="Buton1ClientClick();" />

          </ContentTemplate>

       </asp:UpdatePanel>

    </div>

    </form>

    <form id="form2" action="webForm2.aspx" target="test">

    <div>

   

       <iframe name="test" src="webForm1.aspx"></iframe>

   

    </div>

    </form>

</body>

</html>

 

This code only demonstrated one of the simple ways to handle <iframe> post in a button.  I don’t personally recommend this way.  I recommend using of AJAX directly without <iframe>, or use http://www.codeplex.com/UFrame .

 

===

中文

这是在asp.net中一个使用iframe的简单方式。我在回答一个中文asp.net论坛问题时,写了上面的简单页面,点击按钮来改变iframe里的页面显示。页面在VS2008中生成。 这段程序只是显示了一个简单的方式,我个人并不推荐使用。我推荐直接使用AJAX,或者使用 http://www.codeplex.com/UFrame .

Posted by xinqiu | 2 Comments

ADO.net Data Service reflection class creation in VS2008 SP1 Beta, a breaking change with CTP time

Visual Studio 2008 SP1 Beta is now available!  You can download it here.  

 

ADO.net Data Service is included in the SP1 beta, and there is a bit difference from CTP time.  Let’s follow the following procedure to create a C# website, which provides ADO.net data service on a class with IQueryable interface.

 

1.       Create a  new C# website

2.       Add a class file, e.g. class.cs under App_Code directory

3.       Add reference to System.Data.Services.Client

4.       Use the following code for the class1.cs , note, we need a namespace added here (e.g. CellPhoneNS)

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.Services.Common;

 

namespace CellPhoneNS

{

    /// <summary>

    /// Summary description for Class1

    /// </summary>

    [DataServiceKey("ModelName")]

    public class CellPhone

    {

        public string ModelName { get; set; }

 

        public float Price { get; set; }

 

        public bool Bluetooth { get; set; }

 

        public CellPhone(string modelName, float price, bool bluetooth)

        {

            ModelName = modelName;

            Price = price;

            Bluetooth = bluetooth;

        }

    }

 

    public class CellPhoneContainer

    {

        private List<CellPhone> _cellPhones;

 

        public CellPhoneContainer()

        {

            _cellPhones = new List<CellPhone>

                {   new CellPhone("Razor", 234, true),

                    new CellPhone("Treo 700wx", 550, true),

                    new CellPhone("Iphone", 145, true)

                };

        }

 

        public IQueryable<CellPhone> CellPhones

        {

            get

            {

                return _cellPhones.AsQueryable();

            }

        }

    }

}

 

5.       Add new item ADO.NET Data Service, e.g. WebDataService.svc

6.       Add namespace to the new WebDataService.cs file

7.       Change the svc file to include the namespace, i.e. Service=”CellPhoneNS.WebDataService”

8.       Change WebDataService.cs file as following:

 

using System;

using System.Data.Services;

using System.Collections.Generic;

using System.Linq;

using System.ServiceModel.Web;

 

namespace CellPhoneNS

{

    public class WebDataService : DataService<CellPhoneContainer>

    {

        // This method is called only once to initialize service-wide policies.

        public static void InitializeService(IDataServiceConfiguration config)

        {

            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

            // For testing purposes use "*" to indicate all entity sets/service operations.

            // "*" should NOT be used in production systems.

 

            // Example for entity sets (this example uses "AllRead" which allows reads but not writes)

            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

 

            // Example for service operations

            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);

        }

 

        //// Query interceptors, change interceptors and service operations go here

        //[WebGet]

        //public int someMethod() {return 1;}

    }

}

 

9.       View svc file in the browser to see the ADO.net service for your class

 

The  obvious breaking changes from the original CTP is the following:

1.       Use     [DataServiceKey("ModelName")] above the class, instead of the [DataWebKey] on method name.

2.       The reference for the above attribute is System.Data.Services.Common from assembly System.Data.Services.Client.dll, instead of the System.Data.Services

 

Note, you need namespace in the website to help the ADO.net service consumers in the future, so they won’t get an empty namespace…

 

Hope this helps.

 

Reference:

http://lostintangent.com/training-series/adonet-data-services-training-series/

 

======

中文               

 

Visual Studio 2008 SP1 Beta中提供了ADO.net 数据服务,和CTP(社区技术预览版)时的数据服务稍有不同。下面的例子新建一个C#网页,并通过一个带有IQueryable 接口的类来提供ADO.net 数据服务。 

1.       新建一个C#网页

2.       加入一个类文件,如 在App_Code目录中加入class.cs文件

3.       加入对System.Data.Services.Client的引用

4.       U在class1.cs中加入下面的程序(参照上面英文版本第4步)。注意,我们加入了一个命名空间(CellPhoneNS)

5.       加入新的ADO.NET 数据服务项目, 如 WebDataService.svc

6.       在WebDataService.cs 文件中加入命名空间

7.       更改svc 文件,加入命名空间,如:Service=”CellPhoneNS.WebDataService”

8.       更改WebDataService.cs (参照上面英文版本第8步)。

9.       V在浏览器中查看svc文件,来看你生成的ADO.NET 数据服务

 

和CTP不一样的地方在:

1.       在类的上面定义[DataServiceKey("ModelName")] ,而不是在函数上方定义[DataWebKey] 。

2.       上面的属性是在 System.Data.Services.Common 中定义,要引用System.Data.Services.Client.dll 文件,而不是System.Data.Services

 

注意,在网页中,你需要定义命名空间,来帮助将来的ADO.net数据服务的客户端,这样他们不会拿到一个空的命名空间。 

 

引用: 

http://lostintangent.com/training-series/adonet-data-services-training-series/

Problems debugging VS2008 RTM web site and applications on IE8 beta1

I recently have to delete one of my blog talking about VS2008 and IE8 beta, since I made some comment that may be easy to misunderstand.  I will be careful this time. J

 

If IE8 beta1 is installed on the OS, when F5, Ctrl-F5, view in browser is fired for a web page with VS2008 RTM, IE8 beta1 may get popped up, then you will notice VS2008 RTM will freeze for about 30 seconds.  The page loaded on IE8 beta1 is an empty page.  One may have to close it to get the proper page running.  This seems happen for both normal mode and IE7 emulated mode.

 

There is a workaround, if IE8 beta1 is the preferred way to debug.  Install Microsoft Silverlight Tools Beta 1 for Visual Studio 2008, which contains the fix.  Otherwise, we’ve to wait for the next VS service pack for the fix.

 

An update note for this on 9-6-2008: IE8 Beta2 is out, and works fine with VS2008 SP1. 

 

如果系统里安装了IE8Beta1版,当使用VS2008 RTM的F5/Ctrl-F5时,IE8Beta1可能会显现出空的文件,VS2008 RTM也会停止响应30秒钟左右。可能需要关掉出现空文件的IE8Beta1来继续。这在IE8Beta1的正常浏览模式和IE7的模拟浏览模式下都会出现。

 

解决方法是安装VS2008 SP1。

Posted by xinqiu | 0 Comments
More Posts Next page »
 
Page view tracker