Friday, March 11, 2011

.NET: WCF Vs Web Services

These are the consolidated points from various web links to understand and remember to understand the main difference between WCF service and SOAP Web Service.

 

1.       WCF: Support multiple protocols (HTTP,TCP, Peer Network, IPC, MSMQ) whereas traditional webservices are limited to HTTP only.

2.        WCF: messages are independent of the underlying transport protocol.

3.       WCF: maintains the same programming model for the local and remote cases, thus it allows you to switch locations of the service without affecting the client.

4.       WCF: services can communicate with non-WCF clients and non-WCF clients can communicate with WCF services.

5.       WCF: Supports Multiple Bindings like HTTP, TCP, MSMQ etc  whereas web services uses only HTTP

6.       WCF: can maintain transaction like com+.

7.       WCF: can maintain state.

8.       WCF: can be hosted on IIS, Selfhosting, Windows Servies (Windows Application Service) .

9.       We Service: The development of webservice with ASP.net relies on defining data and relies on the XMLSerializer to transform data to or from a service. Key Issues of XML Serailizer to serialize .net Types with XML.

10.   Web service: Only Public fields and Properties of .net types can be translated to XML

11.   Web service: Only the classes which implement IEnumerable Interface can be translated to XML.

12.   Web service:  Classes implementing IDictionary Interface such as Hash table cannot be serialized.

13.   WCF Uses DataContract Attribute and DataMember Attribute to translate .net types to XMl.

14.   DataContract Attribute can be used for classes and structures-DataMember Attribute to Field or Property.

15.   WCF also supports many of the advanced WS-* Web service standards, which can provide rich Web service communication across platforms.

16.   WCF can be extended to use protocols other than SOAP to communicate with Web services, for example, Rich Site Summary (RSS). It is interoperable with WSE 3.0, System.Messaging, .NET Enterprise Services, and ASMX Web services. This means that, with little or no change to code, existing applications that are built with these technologies can interoperate with WCF services. WCF contracts define the behavior of WCF services. They are created in code by service developers, and are exposed to clients in the service metadata. The five types of contracts:

a.       Service Contracts

b.      Operation Contracts

c.       Data Contracts

d.      Message Contracts

e.      Fault Contracts

Diference between DataContractSerailizer and XML Serializer is

17.   DCS has better performance over XML Serialization

18.   XML Serialization doesn't indicate which field or property of the type is serialized

19.   DCS Explicitly shows which field or property is serialized

20.   DCS can serialize classes implementing IDictionary interface(Hash Table)

Exception Handling

21.   Web services: Unhandled exceptions are returned to the client as SOAP faults.

22.   WCF:  unhandled exceptions are not returned to clients as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.

1) One way methods are "fire and forget". The client which sends this message does not need to, or want to, receive a reply (at all). While you can _functionally_ achieve the same affect by using more traditional request/reply patterns (or by just making your callback empty as you suggest) -- you are still wasting CPU and network resources sending/receiving what ends up being a null message.

Also, in WCF 'void' methods are actually still Request/Reply (they are synchronous). This means that even though nothing will be returned in the reply, the client will still wait until it has received that reply before continuing on. This is still the case if you apply the Async pattern to the 'void' methods as well -- when you execute your End* method, it will block until it is finished executing etc.

This may be explained better in the conceptual docs for this topic: http://msdn2.microsoft.com/en-us/library/ms730149.aspx

2) Again, there is no reply message at all with one way messages. One would only need to have a CallbackContract IFF your service intends to send information back to a client.

3) If the service host is not alive, that call will still throw with an EndpointNotFound exception for instance or other similiar faults. How does this work? With HTTP when we send the request, even if it's a one-way operation, we still get back a HTTP 200 response (very small, very negligable) -- hence, the client still knows something is wrong if we do not see that HTTP 200 response.

4) WS-RM is primarily targeted at non-point-to-point communication exchanges OR for networks where connections are flakey; i.e. if you have a SOAP router involved you'll probably want WS-RM OR if you want your client to automatically reconnect to the service when that connection suddently dies. Also, WS-RM is not durable, like queues are. So in general, no, you wouldn't want to enable ReliableSessions (WS-RM) on your bindings if you are going to be using Queues -- unless there are intermediaries involved or other network conditions that plain TCP cannot handle.

5) Yes, the various MSDTC's involved (or WebSpheres or Axis's) all need to talk to each other -- and they each have their own port. By default when you configure WS-AT we will use port 443 (the common HTTPS port that is also usually open on server machines).

6) In general WCF does not offer such things. We do have several instancing options and timeouts that can be tweaked to help design for this, but in general, the only deadlock avoidance technique that exists is transaction timeouts.

What is .NET Remoting?

.NET Remoting is an enabler for application communication. It is a generic system for different applications to use to communicate with one another. .NET objects are exposed to remote processes, thus allowing interprocess communication. The applications can be located on the same computer, different computers on the same network, or even computers across separate networks.

.NET Remoting versus Distributed COM

In the past interprocess communication between applications was handled through Distributed COM, or DCOM. DCOM works well and the performance is adequate when applications exist on computers of similar type on the same network. However, DCOM has its drawbacks in the Internet connected world. DCOM relies on a proprietary binary protocol that not all object models support, which hinders interoperability across platforms. In addition, have you tried to get DCOM to work through a firewall? DCOM wants to communicate over a range of ports that are typically blocked by firewalls. There are a ways to get it to work, but they either decrease the effectiveness of the firewall (why bother to even have the firewall if you open up a ton of ports on it), or require you to get a firewall that allows support for binary traffic over port 80.

.NET Remoting eliminates the difficulties of DCOM by supporting different transport protocol formats and communication protocols. This allows .NET Remoting to be adaptable to the network environment in which it is being used.

.NET: Remove Empty XML Elements from a XML Document

When you are calling your Oracle Stored procedure using ADO.NET Command object and each of the Parameter is mapped to Dataset’s source column and the XML contains empty elements
You will end up seeing the Exceptions. To avoid this kind of exception the simple way is to Remove empty XML elements from your XML document.
Here is piece of code that saved my life during XML Parsing when creating a Dataset that was mapped to my Façade layer and collaborated with OracleDataAdapter

Protected Function RemoveEmptyXMLElements(ByVal XMLFile As String) As String Dim sXML As String = String.Empty
Try
         Dim XmlDoc As XmlDocument = New XmlDocument      
     XmlDoc.Load(XMLFile)      
     Dim emptyXmlNodeList As XmlNodeList = XmlDoc.SelectNodes("//*[not
                                 (descendant::text()[normalize-space()])]")      
     Dim emptyXmlNode As XmlNode      
    
     For Each emptyXmlNode In emptyXmlNodeList             
         emptyXmlNode.ParentNode.RemoveChild(emptyXmlNode)      
     Next      
     sXML = XmlDoc.InnerXml.ToString()       'XmlDoc.Save(XMLFile)
Catch ex As Exception
   Throw ex
End Try
Return sXML.ToString
End Function

Thursday, March 3, 2011

Oracle: External Tables

External Tables - Oracle

 

 

Source : http://www.orafaq.com/wiki/External_table

From Oracle FAQ

Jump to: navigation, search

An external table is a table that is NOT stored within the Oracle database. Data is loaded from a file via an access driver (normally ORACLE_LOADER) when the table is accessed. One can think of an external table as a view that allows running SQL queries against files on a filesystem without the need to first loaded the data into the database.

Example

Prepare test data. For our example we need to create a file called report.csv with the following data:

1, Yes

Create a database directory to match your already existing OS directory and grant your Oracle user READ and WRITE access to it:

SQL> CREATE OR REPLACE DIRECTORY my_data_dir as '/my/data/dir/';

Directory created.

 

SQL> GRANT read, write ON DIRECTORY my_data_dir TO scott;

Grant succeeded.

Create the external table definition:

CREATE TABLE t1

( c1 NUMBER,

  c2 VARCHAR2(30)

)

ORGANIZATION EXTERNAL

( default directory my_data_dir

  access parameters

  ( records delimited by newline

    fields terminated by ','

  )

  location ('report.csv') 

);

Select from the external table will invoke a load of the data on filesystem:

SQL> select * from s1;

     C1 C2

------- ----------

      1 Yes

 

Wednesday, December 29, 2010

Oracle : DML Error Logging in Oracle 10g

Source : http://www.oracle-base.com/articles/10g/DmlErrorLogging_10gR2.php

One of the nice feature of Oracle 10G

DML Error Logging in Oracle 10g Database Release 2

By default, when a DML statement fails the whole statement is rolled back, regardless of how many rows were processed successfully before the error was detected. In the past, the only way around this problem was to process each row individually, preferably with a bulk operation using a FORALL loop with the SAVE EXCEPTIONS clause. In Oracle 10g Database Release 2, the DML error logging feature has been introduced to solve this problem. Adding the appropriate LOG ERRORS clause on to most INSERT, UPDATE, MERGE and DELETE statements enables the operations to complete, regardless of errors. This article presents an overview of the DML error logging functionality, with examples of each type of DML statement.

Syntax

The syntax for the error logging clause is the same for INSERT, UPDATE, MERGE and DELETE statements.

LOG ERRORS [INTO [schema.]table] [('simple_expression')] [REJECT LIMIT integer|UNLIMITED]

The optional INTO clause allows you to specify the name of the error logging table. If you omit this clause, the the first 25 characters of the base table name are used along with the "ERR$_" prefix.

The
simple_expression is used to specify a tag that makes the errors easier to identify. This might be a string or any function whose result is converted to a string.

The
REJECT LIMIT is used to specify the maximum number of errors before the statement fails. The default value is 0 and the maximum values is the keyword UNLIMITED. For parallel DML operations, the reject limit is applied to each parallel server.

Restrictions

The DML error logging functionality is not invoked when:

  • Deferred constraints are violated.
  • Direct-path INSERT or MERGE operations raise unique constraint or index violations.
  • UPDATE or MERGE operations raise a unique constraint or index violation.

In addition, the tracking of errors in LONG, LOB and object types is not supported, although a table containing these columns can be the target of error logging.

Sample Schema

This following code creates and populates the tables necessary to run the example code in this article.

-- Create and populate a source table.

CREATE TABLE source (

id NUMBER(10) NOT NULL,

code VARCHAR2(10),

description VARCHAR2(50),

CONSTRAINT source_pk PRIMARY KEY (id)

);


 

DECLARE

TYPE t_tab IS TABLE OF source%ROWTYPE;

l_tab t_tab := t_tab();

BEGIN

FOR i IN 1 .. 100000 LOOP

l_tab.extend;

l_tab(l_tab.last).id := i;

l_tab(l_tab.last).code := TO_CHAR(i);

l_tab(l_tab.last).description := 'Description for ' || TO_CHAR(i);

END LOOP;


 

-- For a possible error condition.

l_tab(1000).code := NULL;

l_tab(10000).code := NULL;


 

FORALL i IN l_tab.first .. l_tab.last

INSERT INTO source VALUES l_tab(i);


 

COMMIT;

END;

/


 

EXEC DBMS_STATS.gather_table_stats(USER, 'source', cascade => TRUE);


 

-- Create a destination table.

CREATE TABLE dest (

id NUMBER(10) NOT NULL,

code VARCHAR2(10) NOT NULL,

description VARCHAR2(50),

CONSTRAINT dest_pk PRIMARY KEY (id)

);


 

-- Create a dependant of the destination table.

CREATE TABLE dest_child (

id NUMBER,

dest_id NUMBER,

CONSTRAINT child_pk PRIMARY KEY (id),

CONSTRAINT dest_child_dest_fk FOREIGN KEY (dest_id) REFERENCES dest(id)

);

Notice that the CODE column is optional in the SOURCE table and mandatory in the DEST table.

Once the basic tables are in place we can create a table to hold the DML error logs for the
DEST. A log table must be created for every base table that requires the DML error logging functionality. This can be done manually or with the CREATE_ERROR_LOG procedure in the DBMS_ERRLOG package, as shown below.

-- Create the error logging table.

BEGIN

DBMS_ERRLOG.create_error_log (dml_table_name => 'dest');

END;

/


 

PL/SQL procedure successfully completed.


 

SQL>

The owner, name and tablespace of the log table can be specified, but by default it is created in the current schema, in the default tablespace with a name that matches the first 25 characters of the base table with the "ERR$_" prefix.

SELECT owner, table_name, tablespace_name

FROM all_tables

WHERE owner = 'TEST';


 

OWNER TABLE_NAME TABLESPACE_NAME

------------------------------ ------------------------------ ------------------------------

TEST DEST USERS

TEST DEST_CHILD USERS

TEST ERR$_DEST USERS

TEST SOURCE USERS


 

4 rows selected.


 

SQL>

The structure of the log table includes maximum length and datatype independent versions of all available columns from the base table, as seen below.

SQL> DESC err$_dest

Name Null? Type

--------------------------------- -------- --------------

ORA_ERR_NUMBER$ NUMBER

ORA_ERR_MESG$ VARCHAR2(2000)

ORA_ERR_ROWID$ ROWID

ORA_ERR_OPTYP$ VARCHAR2(2)

ORA_ERR_TAG$ VARCHAR2(2000)

ID VARCHAR2(4000)

CODE VARCHAR2(4000)

DESCRIPTION VARCHAR2(4000)


 

SQL>

Insert

When we built the sample schema we noted that the CODE column is optional in the SOURCE table, but mandatory in th DEST table. When we populated the SOURCE table we set the code to NULL for two of the rows. If we try to copy the data from the SOURCE table to the DEST table we get the following result.

INSERT INTO dest

SELECT *

FROM source;


 

SELECT *

*

ERROR at line 2:

ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")


 


 

SQL>

The failure causes the whole insert to roll back, regardless of how many rows were inserted successfully. Adding the DML error logging clause allows us to complete the insert of the valid rows.

INSERT INTO dest

SELECT *

FROM source

LOG ERRORS INTO err$_dest ('INSERT') REJECT LIMIT UNLIMITED;


 

99998 rows created.


 

SQL>

The rows that failed during the insert are stored in the ERR$_DEST table, along with the reason for the failure.

COLUMN ora_err_mesg$ FORMAT A70

SELECT ora_err_number$, ora_err_mesg$

FROM err$_dest

WHERE ora_err_tag$ = 'INSERT';


 

ORA_ERR_NUMBER$ ORA_ERR_MESG$

--------------- ---------------------------------------------------------

1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")

1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")


 

2 rows selected.


 

SQL>

Update

The following code attempts to update the CODE column for 10 rows, setting it to itself for 8 rows and to the value NULL for 2 rows.

UPDATE dest

SET code = DECODE(id, 9, NULL, 10, NULL, code)

WHERE id BETWEEN 1 AND 10;

*

ERROR at line 2:

ORA-01407: cannot update ("TEST"."DEST"."CODE") to NULL


 


 

SQL>

As expected, the statement fails because the CODE column is mandatory. Adding the DML error logging clause allows us to complete the update of the valid rows.

UPDATE dest

SET code = DECODE(id, 9, NULL, 10, NULL, code)

WHERE id BETWEEN 1 AND 10

LOG ERRORS INTO err$_dest ('UPDATE') REJECT LIMIT UNLIMITED;


 

8 rows updated.


 

SQL>

The rows that failed during the update are stored in the ERR$_DEST table, along with the reason for the failure.

COLUMN ora_err_mesg$ FORMAT A70

SELECT ora_err_number$, ora_err_mesg$

FROM err$_dest

WHERE ora_err_tag$ = 'UPDATE';


 

ORA_ERR_NUMBER$ ORA_ERR_MESG$

--------------- ---------------------------------------------------------

1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")

1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")


 

2 rows selected.


 

SQL>

Merge

The following code deletes some of the rows from the DEST table, then attempts to merge the data from the SOURCE table into the DEST table.

DELETE FROM dest

WHERE id > 50000;


 

MERGE INTO dest a

USING source b

ON (a.id = b.id)

WHEN MATCHED THEN

UPDATE SET a.code = b.code,

a.description = b.description

WHEN NOT MATCHED THEN

INSERT (id, code, description)

VALUES (b.id, b.code, b.description);

*

ERROR at line 9:

ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")


 


 

SQL>

As expected, the merge operation fails and rolls back. Adding the DML error logging clause allows the merge operation to complete.

MERGE INTO dest a

USING source b

ON (a.id = b.id)

WHEN MATCHED THEN

UPDATE SET a.code = b.code,

a.description = b.description

WHEN NOT MATCHED THEN

INSERT (id, code, description)

VALUES (b.id, b.code, b.description)

LOG ERRORS INTO err$_dest ('MERGE') REJECT LIMIT UNLIMITED;


 

99998 rows merged.


 

SQL>

The rows that failed during the update are stored in the ERR$_DEST table, along with the reason for the failure.

COLUMN ora_err_mesg$ FORMAT A70

SELECT ora_err_number$, ora_err_mesg$

FROM err$_dest

WHERE ora_err_tag$ = 'MERGE';


 

ORA_ERR_NUMBER$ ORA_ERR_MESG$

--------------- ---------------------------------------------------------

1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")

1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")


 

2 rows selected.


 

SQL>

Delete

The DEST_CHILD table has a foreign key to the DEST table, so if we add some data to it would would expect an error if we tried to delete the parent rows from the DEST table.

INSERT INTO dest_child (id, dest_id) VALUES (1, 100);

INSERT INTO dest_child (id, dest_id) VALUES (2, 101);

With the child data in place we ca attempt to delete th data from the DEST table.

DELETE FROM dest;

*

ERROR at line 1:

ORA-02292: integrity constraint (TEST.DEST_CHILD_DEST_FK) violated - child record found


 


 

SQL>

As expected, the delete operation fails. Adding the DML error logging clause allows the delete operation to complete.

DELETE FROM dest

LOG ERRORS INTO err$_dest ('DELETE') REJECT LIMIT UNLIMITED;


 

99996 rows deleted.


 

SQL>

The rows that failed during the delete operation are stored in the ERR$_DEST table, along with the reason for the failure.

COLUMN ora_err_mesg$ FORMAT A69

SELECT ora_err_number$, ora_err_mesg$

FROM err$_dest

WHERE ora_err_tag$ = 'DELETE';


 

ORA_ERR_NUMBER$ ORA_ERR_MESG$

--------------- ---------------------------------------------------------------------

2292 ORA-02292: integrity constraint (TEST.DEST_CHILD_DEST_FK) violated -

child record found


 

2292 ORA-02292: integrity constraint (TEST.DEST_CHILD_DEST_FK) violated -

child record found


 


 

2 rows selected.


 

SQL>

Tuesday, November 16, 2010

VBA :Macro to Download Email Attachments

Start Outlook à
Tools à Macro à Visual Basic Editor.

Add a new code module by choosing Insert > Module and add the following code. Compile the Macro and run.


 

Sub DownloadAttachments()


Dim ns As
NameSpace


Dim Inbox As MAPIFolder


Dim Subfolder As MAPIFolder


Dim Item As
Object


Dim Atmt As Attachment


Dim FileName As
String


Dim Localfolder As
String


Dim SubfolderName As
String


 


Dim i As
Integer


Dim j As
Integer


 


'***************************


' This Macro is coded to downlaod the files only from the folders under the INBOX.


' Apprently you could change ns.GetDefaultFolder(olFolderInbox) argument to any other folder and try.

' The NameSpace is the object that gives you access to all Outlook's folders.


' In Outlook there is only one and it is called "MAPI" which is an acronym for Messaging Application Programming

'    Interface.


'***************************


On
Error
GoTo GetAttachments_err


 

Localfolder = "C:\Email Attachments\"

SubfolderName = "AppWorx"
'Configure your own folder name which is under your INBOX.


 

ns = GetNamespace("MAPI")

Inbox = ns.GetDefaultFolder(olFolderInbox) ''Change the argument to any other folder.

Subfolder = Inbox.Folders(SubfolderName)

i = 0


 


If Subfolder.Items.Count = 0 Then

MsgBox("There are no messages in the folder." _

, vbInformation, "Nothing Found")


Exit
Sub


End
If


 


If Subfolder.Items.Count > 0 Then

j = j + 1


For
Each Item In Subfolder.Items


For
Each Atmt In Item.Attachments


If LCase(Right(Atmt.FileName, 4)) = ".csv"
Then 'change your filter logi here

FileName = Localfolder & Atmt.FileName

Atmt.SaveAsFile(FileName)

i = i + 1


End
If


Next Atmt


Next Item


End
If


 


 


If i > 0 Then

varResponse = MsgBox("Downloaded " & i & " files." _

& vbCrLf & "Saved @" & Localfolder & " folder." _

& vbCrLf & vbCrLf & "Would you like to view the files now?" _

, vbQuestion + vbYesNo, "Finished!")


If varResponse = vbYes Then

Shell("Explorer.exe /e, " & Localfolder, vbNormalFocus)


End
If


Else

MsgBox("I didn't find any attached files in your mail.", vbInformation, _


"Finished!")


End
If


 

GetAttachments_exit:

Atmt = Nothing

Item = Nothing

ns = Nothing


Exit
Sub


 

GetAttachments_err:

MsgBox("An unexpected error has occurred. @" & j & "Email record" & Err.Number _

& vbCrLf & "Error Description: " & Err.Description _

, vbCritical, "Error!")


Resume GetAttachments_exit

End
Sub