Wednesday, September 25, 2013

Consuming WCF service from sharepoint web part or asp.net project

Consuming WCF service from sharepoint web part or asp.net project

Step1.
Make sure that web part project/ dot net project has references to the following assemblies.
System.ServiceModel.dll
System.ServiceModel.Web.dll
System.ServiceModel.dll
System.Runtime.Serialization.dll

Step2
Import the following namespace in the code-behind of the web part/web page.
using System.ServiceModel;
using System.Runtime.Serialization;

Step3
The next step is to generate the client proxy for the WCF service. You can use ‘Add Service Reference’ option in Visual Studio 2008/10 or use the command line utility svcutil.exe.

Follow the below steps to create service reference using svcutil.exe.

Run command prompt with administrator privilege or take visual studio command prompt

Step 3.1:
Start > run > cmd.exe

Step 3.2: Deploy the service using basic http binding /net tcp binding and get the url of the service deployed

For example my service url is :
http://dt3138aaashjuae/asco/ASCO.ServiceLayer.MasterDataService.svc
It nettcpbindng is used it will be:
net.tcp://dt3138aaashjuae/ASCO.Services/ASCO.ServiceLayer.MasterDataService.svc/mex

Step 3.3: Change directory to the path of svcutil.exe

>cd C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin

Or

cd C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin

Step 3.4: Run the below command to create service reference class and configuration file for the client sharepoint project ASCO.SP(Namespace is ASCO.SP)

>svcutil /config:output.config /out:MasterDataService.cs /tcv:Version35 /n:*, ASCO.SP http://dt3138aaashjuae/asco/ASCO.ServiceLayer.MasterDataService.svc

If service is using nettcpbinding the above command should be:

> svcutil /config:output.config /out:MasterDataService.cs /tcv:Version35 /n:*, ASCO.SP net.tcp://dt3138aaashjuae/ASCO.Services/ASCO.ServiceLayer.MasterDataService.svc/mex

/config – specifies the name of the client configuration file to be generated
/out – specifies the name of the client proxy file (.cs file)
/tcv – specifies the .net framework version for the client proxy
/n:* – specifies the namespace for the client proxy. This is a significant factor that needs to be considered. Please make sure that the namespace of the proxy file matches with the namespace of visual web part project.



Text in the above command window are:
--------------------------------------------------------------------------------------------------------------
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
C:\Program Files\Microsoft Visual Studio 10.0\VC>
C:\Program Files\Microsoft Visual Studio 10.0\VC>svcutil /config:output.config
/out:MasterDataService.cs /tcv:Version35 /n:*,ASCO.Service http://dt3138aaashjuae/asco/ASCO.ServiceLayer.MasterDataService.svc
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1]
Copyright (c) Microsoft Corporation. All rights reserved.
Attempting to download metadata from 'http://dt3138aaashjuae/asco/ASCO.ServiceLayer.MasterDataService.svc' using WS-Metadata Exchange or DISCO.
Generating files...
C:\Program Files\Microsoft Visual Studio 10.0\VC\MasterDataService.cs
C:\Program Files\Microsoft Visual Studio 10.0\VC\output.config
C:\Program Files\Microsoft Visual Studio 10.0\VC>
-----------------------------------------------------------------------------------------------------------------

Now Svcutil had generated two files in the location ‘C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin’
1. output.config
2. MasterDataService.cs

Step 4
Copy the content of section ‘system.serviceModel’ from the configuration to the clients web.config section ‘system.serviceModel’. Generated configuration file with ‘nettcpbinding’ looks like as below:

<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<bindingname="MasterDataServiceEndpoint"closeTimeout="00:01:00"
openTimeout="00:01:00"receiveTimeout="00:10:00"sendTimeout="00:01:00"
transactionFlow="false"transferMode="Buffered"transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard"listenBacklog="10"
maxBufferPoolSize="524288"maxBufferSize="65536"maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotasmaxDepth="32"maxStringContentLength="8192"maxArrayLength="16384"
maxBytesPerRead="4096"maxNameTableCharCount="16384" />
<reliableSessionordered="true"inactivityTimeout="00:10:00"
enabled="false" />
<securitymode="Transport">
<transportclientCredentialType="Windows"protectionLevel="EncryptAndSign" />
<messageclientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpointaddress="net.tcp://dt3138aaashjuae/ASCO.Services/ASCO.ServiceLayer.MasterDataService.svc"
binding="netTcpBinding"bindingConfiguration="MasterDataServiceEndpoint"
contract="ASCO.Service.IMasterDataService"name="MasterDataServiceEndpoint">
<identity>
<dnsvalue="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

For Sharepoint site ASCO.SP the command should be:
svcutil /config:output.config /out:MasterDataService.cs /tcv:Version35 /n:*,ASCO.SP net.tcp://dt3138aaashjuae/ASCO.Services/ASCO.ServiceLayer.MasterDataService.svc/mex

For .Net Project ASCO.DotNet the command should be:
svcutil /config:output.config /out:MasterDataService.cs /tcv:Version35 /n:*,ASCO.DotNet net.tcp://dt3138aaashjuae/ASCO.Services/ASCO.ServiceLayer.MasterDataService.svc/mex

Step 5
Copy and paste the proxy class MasterDataService.cs from the path ‘C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin’ to the client project (Sharepoint/Asp.Net)which consume the wcf service

If you try to access the client proxy on the code-behind of the Visual Web Part, the intellisense will show that class now. For the intellisense to recognize this proxy class, the namespace of the generated proxy class has to match with the namespace of the visual web part project. If there is a mismatch in the namespace, the intellisense will not recognize this proxy class. It is always recommended to explicitly specify the namespace during the generation of client proxy using svcutil.

For Example: Consuming service method to bind a grid view

protected void grdMaster_NeedDataSource(object sender,Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
int pageSize =grdMaster.PageSize;
int pageIndex =grdMaster.CurrentPageIndex;

MasterDataServiceClient service = new MasterDataServiceClient();

long? status = 0;
long? errno = 0;
string errmsg = string.Empty;

PREAMBLE_MASTER[] preambles = service.GetPreambleMasterData(null, null, ref status, ref errno, ref errmsg);

this.grdMaster.DataSource = preambles.Skip(pageIndex * pageSize).Take(pageSize);
this.grdMaster.VirtualItemCount = preambles.Count();
}

Sunday, August 26, 2012

PL/SQL Developer : Initialization error Could not locate OCI dll

When try to run PL/SQL Developer to connect to oracle Database the below error is shown 

---------------------------
PL/SQL Developer
---------------------------
Initialization error
Could not locate OCI dll

OracleHomeKey: SOFTWARE\ORACLE\KEY_OraClient11g_home
OracleHomeDir: D:\app\aneesh.thilakan\product\11.2.0\client_1
---------------------------
OK  
---------------------------



Solution:
1. Copy oci.dll from folder c:\app\user\product\11.2.0\client_1

2. Paste it in the folder  c:\app\user\product\11.2.0\client_1\bin

You had done!

Now open your PL/SQL Developer.

Tuesday, August 14, 2012

System.Data.SqlClient.SqlException: Generating user instances in SQL Server is disabled. Use sp_configure 'user instances enabled' to generate user instances

I. When I try to run ASP.net application with SQL Server database an error shown as below:


Server Error in '/' Application.
--------------------------------------------------------------------------------
Generating user instances in SQL Server is disabled. Use sp_configure 'user instances enabled' to generate user instances.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Generating user instances in SQL Server is disabled. Use sp_configure 'user instances enabled' to generate user instances.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

II. When I try to connect to a SQL Server database from visual studio server explorer an error shown as below:  
---------------------------
Microsoft Visual Studio
---------------------------
Generating user instances in SQL Server is disabled. Use sp_configure 'user instances enabled' to generate user instances.
---------------------------
OK
---------------------------

Solution
This error is due to SQL Server Express has disabled user instance genereation. Simply apply the below steps to enable user instance generation.

1. Open SQL Server 2005 Management Studio Express.

2. Connect to the default instance of that server (eg: Computer Name\SQLEXPRESS)

3. Open a new query window and use master database

4. Execute sp_configure as below:
exec sp_configure 'user instances enabled', 1

--Configuration option 'user instances enabled' changed from 0 to 1. Run the RECONFIGURE statement to install.

5. Execute RECONFIGURE as:
RECONFIGURE

--Command(s) completed successfully.

6. Go to services and restart the SQL Server Instance currently running.



Note: Sample connection string format in web.config should be as below :

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=

|DataDirectory|\DBName.mdf;Integrated Security=True;User Instance=True;Initial Catalog=DBName;"

User Instances
User instance is a new feature supported by Microsoft SQL Server 2005 Express Edition, which is only available when using the .NET Framework Data Provider for SQL Server (SqlClient). A user instance is a separate instance of the SQL Server Express Database Engine that is generated by a parent instance. User instances allow users who are not administrators on their local computers to attach and connect to SQL Server Express databases. To generate user instances, a parent instance of SQL Server Express must be running. User instances are enabled by default when SQL Server Express is installed, and they can be explicitly enabled or disabled by a system administrator executing the sp_configure system stored procedure on the parent instance.

-- Enable user instances.
sp_configure 'user instances enabled', '1'

-- Disable user instances.
sp_configure 'user instances enabled', '0'

Sunday, August 12, 2012

Visual Studio Server Explorer Oracle Procedure Run Debug Error

When try to debug an oracle procedure/package  from visual studio server explorer an error shows to inform the user that he has insufficient privilege for debugging as below.

Visual Studio Server Explorer Oracle Procedure Run Debug Error
---------------------------
Microsoft Visual Studio
---------------------------
Server Explorer
An error occurred while executing the procedure/function: ORA-01031: insufficient privileges
ORA-06512: at "SYS.DBMS_DEBUG_JDWP", line 68
ORA-06512: at line 1
---------------------------
OK
---------------------------

Solution
The following privileges are required for debugger:

1. DEBUG CONNECT SESSION
Connect the current session to a debugger that uses the Java Debug Wire Protocol (JDWP).

2.DEBUG ANY PROCEDURE
Debug all PL/SQL and Java code in any database object. Granting this privilege is equivalent to granting the DEBUG object privilege on all applicable objects in the database.

Syntax
GRANT DEBUG CONNECT SESSION TO user_name ;
GRANT DEBUG ANY PROCEDURE TO user_name;