eFORCE
Blogs Home | Corporate Website

Thursday, August 14, 2008

Dynamic Data Part1 - Your First Dynamic Data Website

Your First Dynamic Data Site
Microsoft’s .Net 3.5 SP1 of is here and it brings with it some of the most exiting enhancements in .NET 3.5. ASP.Net - Dynamic Data is one of them.  Dynamic Data is a scaffolding framework. It generates CRUD operations from your database schema. What gives an edge to Dynamic Data over some of the existing scaffolding frameworks (like monorail) is the fact that it is fully template driven and highly customizable.

Install .Net SP1

Download and install the .Net 3.5 SP1. You will find two new templates as you try to create a new website. These templates namely, DynamicDataEntityFrameworkWebSite and  DynamicDataLinqToSqlWebSite allow you to quickly create a dynamic data site.
   
   
DynamicDataLinqToSqlWebSite Template: Creates a Dynamic Data web application using a LINQ to SQL data model to generate the scaffolds.
DynamicDataEntityFrameworkWebSite Template: Creates a Dynamic Data web application using Entity Data Model (ADO.NET Entity Framework) to generate the scaffolds.
We will use the DynamicDataLinqToSqlWebSite for this example. Here we go …
 
   
1.
Let us create a Dynamic Data Website by choosing DynamicDataLinqToSqlWebSite template and call it MyDynamicDataSite. You will find that the WebSite has a folder called DynamicData, which has all the template controls and pages which will be used to generate CRUD operation on the tables.
 
 

 

 

2.

 Let us create our Data model using LinqToSql Classes. I have used “Company” database in this example. Let us call our LINQ to SQL class Company.dbml.

 
 

 

 

3.

The Final step is to enable scaffolding. This is done by un-commenting the following lines of code in the global.asax:

 
 

 

 model.RegisterContext(typeof(YourDataContext), new ContextConfiguration() {

ScaffoldAllTables = false });



 

 

 
 
 

Replace the YourDataContext with “your” DataContext that is with CompanyDataContext and turn  ScaffoldAllTables to true:

 
 

 

 model.RegisterContext(typeof(CompanyDataContext), new ContextConfiguration() {

ScaffoldAllTables = true} );

 
 

 

And that’s all you have to do. Run the site and see the magic.

You will find the Dynamic Data has generated CRUD operation on all the tables in your Data Model without you needing to write a single line of code.

It’s Amazing.

 
     

Conclusion:

This was a very simple “hello world” kind of demonstration of how ASP.NET Dynamic Data can provide with basic CRUD operations on your site in matter of minutes.

In future posts we will try to dive deeper into some of the concepts that’s been overviewed here.

Till then, happy programming!

   

posted @ Thursday, August 14, 2008 3:37 AM | Feedback (0)

Tuesday, April 22, 2008

Handling binary data with Web Service Enhancements 3.0

Handling binary data with Web Service Enhancements 3.0
In one of my recent projects, I needed to expose a web service to upload some files to the server. Hence, decided to use array of bytes to transfer contents of these files over the wire. The problem, however, was the fact that these files were as huge (at times even ten’s of MBS). Sending and receiving these huge arrays of bytes over the web was a real pain in the neck. Thanks to Microsoft’s Web Service Enhancement (WSE) 3.0 which proved to be the perfect answer to our problem.
In this article we will examine how easy and efficient it is to send and receive large binary data using WSE 3.0. WSE 3.0 uses MTOM encoding and does not require any form of SOAP attachment. If you have not downloaded WSE 3.0 yet, you can do the same from here. We have used C# as the language and Visual studio 2005 as the IDE in the example that follows.
Create a Web service which Uploads/download large binary data
1.    Create a web service called FileUploadService using visual studio 2005.
2.    Add two methods called UploadFile and DownloadFile
 
 
[WebMethod]
    public void UploadFile(string fileName, byte[] fileData)
    {
        string filePath = Server.MapPath("Uploads");
        filePath = filePath + @"\";
        File.WriteAllBytes(filePath + fileName, fileData);
    }
 
 
[WebMethod]
    public byte[] DownloadFile(string fileName)
    {
        string filePath = Server.MapPath("Uploads");
        filePath = filePath + @"\";
        return File.ReadAllBytes(filePath + fileName);
    }
 
Make your service WSE enabled
1.    Install WSE 3.0.
2.    After the installation is complete, right click on the web service project and select WSE Settings 3.0 from the context menu.
3.    From the displayed dialog and on General tab select the Enable this project for Web Services Enhancements check box and Enable Microsoft Web Services Enhancement Soap Protocol Factory check box and click OK.
 
 
4.    In your web.config, you’ll notice that a new configuration section is added:  
 
<configuration>
 <configSections>
 <sectionname="microsoft.web.services3"
          type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration,  
              Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,   
              PublicKeyToken=31bf3856ad364e35" />
 </configSections>
 ...
</configuration>
 
A reference to the Microsoft.Web.Services3 assembly is added  
 
 
 
<assemblies>
      <addassembly="Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,  
        PublicKeyToken=31BF3856AD364E35" />
</assemblies>
... 
 
Under the system.web element, the <webServices> configuration element is defined. Here the Web service is configured to use the Microsoft Web Services Enhancement Soap Protocol factory WseProtocolFactory which is defined by the <soapServerProtocolFactory> configuration element under the <webServices> element. This is a must for a web service to be able to use WSE 3.0. ...  
 
 
 
<webServices>
      <soapExtensionImporterTypes>
            <addtype="Microsoft.Web.Services3.Description.WseExtensionImporter,   
        Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,   
        PublicKeyToken=31bf3856ad364e35" />
      </soapExtensionImporterTypes>
      <soapServerProtocolFactorytype="Microsoft.Web.Services3.WseProtocolFactory,  
          Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,   
          PublicKeyToken=31bf3856ad364e35" />
</webServices> 
 
Enable a Web Service to Send and Receive Large Amounts of Data
Select Messaging tab from the dialog. On MTOM Settings ensure that Server Mode is set to optional and click OK.
Following Server Mode option are available:
·         In optional mode the WSE processes the incoming SOAP messages whether or not they are MTOM encoded. With optional the client is the one who decide whether to use MTOM or not, if the client application request to use MTOM the web service will use MTOM.
·         In always mode all incoming and outgoing SOAP messages must be MTOM encoded. When a SOAP request is received that is not encoded using MTOM, an HTTP error 415: "Media unsupported" is returned to the sender.
·         In never mode all incoming SOAP messages must not be MTOM encoded. When a SOAP request is received that is encoded using MTOM, an HTTP error 415: "Media unsupported" is returned to the sender.
 
 
Again open the web.config file and review the changes made by the WSE 3.0 configuration tool. You’ll notice the following:
 
A <messaging> configuration element is defined under the <microsoft.web.services3> configuration. This is used to specify the MTOM options.
 
<microsoft.web.services3>
      <messaging>
            <mtomserverMode="optional" />
      </messaging>
</microsoft.web.services3>
 
 
Create a client Application
1.    Create an asp.net website.
2.    Add a fileUpload control and a button control to the default page.
 
 
 
<formid="form1"runat="server">
 <div>
   <table>
      <tr>
            <td>
                  Choose a file
            </td>
            <td>
                  <asp:FileUploadID="filUpload"runat="server"/>
            </td>
            <td>
                  <asp:ButtonID="btnUpload"
                  runat="server"Text="Upload"
                  OnClick="btnUpload_Click" />
            </td>
      </tr>
   </table>
 </div>
</form>
 
 
Make the client Application WSE-enabled
1.    Right click on the website ans select WSE 3.0 properties. From the displayed dialog and on General tab select the Enable this project for Web Services Enhancements check box and Enable Microsoft Web Services Enhancement Soap Protocol Factory check box and click OK.
 
 
2.    In the Messaging tab, specify client Mode as “On” and Server Mode as “always”. By spcifying client mode “on”, we indcate that the configuration is for the client and Server Mode “always” means that all incoming and outgoing SOAP messages must be MTOM encoded.
 
Call the Web service
The client now is ready to call the WSE-enabled FileUpload/FileDownload web-method. Drop in the following code:
 
private void UploadFile()
{
   FileServiceWse service = new FileServiceWse();
   service.UploadFile(filUpload.FileName, filUpload.FileBytes);
   Response.Write("File uploaded.");
}
 
 
 
Conclusion
Here was an attempt to share my first exciting encouter with Microsoft's WSE 3.0. The idea was to illustrate how easy and efficient it is to use the WSE 3.0 to handle large volume of binary data over the web. Hope you guys find this effort useful .
 
 

posted @ Tuesday, April 22, 2008 7:25 AM | Feedback (2)

Home
Contact
RSS 2.0 Feed
Login
August, 2008 (1)
April, 2008 (1)

Powered by: