Wednesday, December 16, 2009

Consuming WCF service out of box

Consuming WCF service out of box: "

Hello All,

I found one issue while consuming WCF service from out of box, There are lots of threads I found for 'The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.' error, CommunicationObjectFaultedException exception thrown and 'The caller was not authenticated by the service.'

I spent lots of time in search, got bunch of solutions, some worked some does not, after doing lots of troubleshooting I got solution for my problem.

My case was, a web application running on different box is trying to consume WCF service which is hosted on different box, but some how on production it was not working. It always throws 'CommunicationObjectFaultedException'. Usually we are putting such calls in using blog, as its taking care of disposing object. But in case of WCF it may not work as per expected, the reason is; A CommunicationObjectFaultedException has been throws so you, the object is not yet initialized and you are using is trying to dispose method which again throw exception.

 

This is because using implicitly call Dispose at the closing brace, which may throw an exception because your object is not yet initialized as its in the Faulted state. To over come this a simple solution, use try catch and call Abort. More reading on this click Avoiding Problems with the Using Statement, good article form Microsoft including Sample application. During my search I found one good code delegate make you fell like you are using clause, but internally its handling such exception.

private class Util
{
public static void UsingWcf<T>(T client, Action<T> action)
where T : System.ServiceModel.ICommunicationObject
{

try
{
action(client);
client.Close();
}
catch (Exception)
{
client.Abort();
throw;
}
}
}
class Program
{
static void Main(string[] args)
{

Util.UsingWcf(new WcfClient(), c =>
{

});

/*using (WcfClient wcfClient = new WcfClient())
{
....
}// This will throw an error
*/
}
}

So we have caught CommunicationObjectFaultedException, but still we are not able to make call to service method. Now you can see one new error 'The caller was not authenticated by the service'. Cool we reach the service but its missing some credentials, as we are consuming service out of box.

Typical configuration section looks like following



Form error 'The caller was not authenticated by the service' we can guess that we need some credentials to call method. You can see the configuration; it says clientCredentialsType="Windows" and security mode is Message, generally with wsHttpBinding these are the typical settings.



As we are using Windows as credential type, we need to provide credentials to use WCF serverice out of box. Once this done you just need to pass user and password in ClientCredentials just like bellow

"

Tuesday, December 15, 2009

WCF Comparison with Web Services and .Net Remoting

WCF Comparison with Web Services and .Net Remoting: "This article is about differences between WCF with Web Services and .Net Remoting"

Saturday, November 21, 2009

Prize winner in Competition “Best ASP.NET article of Sept 2009″

Prize winner in Competition “Best ASP.NET article of Sept 2009″: "

Sorry to post it little bit later, I was out of station for few weeks and great to see that I have own the first prize in the article competition for my article Exploring IIS 6.0 With ASP.NET . This is fourth times in this year I have received the prizes from codeproject. Thanks to all of you who read my articles and vote for it.


Thanks again to all of you. Hope to publish many more articles for you !


Posted in General
"

Friday, November 13, 2009

Announcing the Microsoft SDK for Facebook (Facebook .NET SDK 3.0)

Announcing the Microsoft SDK for Facebook (Facebook .NET SDK 3.0): "This latest release of the Facebook .NET SDK supports the newest Facebook services delivered through the Facebook Open Stream API. Create applications for Facebook using any of your favorite Microsoft development platforms: Silverlight, WPF, ASP.NET, ASP.NET MVC, and Windows Forms."

Thursday, October 29, 2009

ASP.NET MVC Cheat Sheets

ASP.NET MVC Cheat Sheets: "Microsoft MVP Elijah Manor shares lists of quick reference notes for working with ASP.NET MVC that he has collected across the web."

Tuesday, October 27, 2009

Transfer Schema Objects MSSQL 2005

Some time restoring database from ".bak" file might be problematic,

especially the "The backup copy provided by web hosts" "schema mapping" might create issues.
To transfer object from one schema to another following command can be used

ALTER SCHEMA schema_name TRANSFER object_name


but if we want to transfer all objects following cursor will do the job.

this might not be good approach but it worked for me.

declare @oldSchemaName varchar(100)
declare @newSchemaName varchar(100)
declare @name varchar(500)
declare @ExecQuery varchar(1000)
set @oldSchemaName='guest'
set @newSchemaName='dbo'

DECLARE cur CURSOR FOR SELECT @oldSchemaName + '.'+ name FROM sys.objects

where schema_id in(
select top 1 schema_id from sys.schemas where [name] like @oldSchemaName)

OPEN cur FETCH NEXT FROM cur into @name
WHILE @@FETCH_STATUS = 0
begin
set @ExecQuery='ALTER SCHEMA ' + @newSchemaName + 'TRANSFER ' + @name
print (@ExecQuery)
exec(@ExecQuery)

FETCH NEXT FROM cur into @name
end
CLOSE cur
DEALLOCATE cur

Tuesday, October 20, 2009

78 jQuery Scenarios to Fall in Love

78 jQuery Scenarios to Fall in Love: "I am in love with jQuery, are you? If you are not then you may not have seen the productivity in web design that can happen by using jQuery. I have collected some of the most interesting jQuery scenarios around the web that can make you fall in love...


"