Quote of the day

About Me

Wednesday, April 2, 2014

Could not resolve mscorlib for target framework '.NETFramework,Version=v4.0'. This can happen if the target framework is not installed or if the framework moniker is incorrectly formatted wcf update service reference issue

I am feeling little good about writing this blog as this might help many, as i searched for the suggestion on the net for couple of days but may be i didn't google it properly (I can not say google does not have solution :) ), but finally if ends well everything is well, i got the solution of this nightmare kinda issues



The Issue

I faced this issue and i saw on the net the plenty like me had the same issue, that one fine morning your service reference stopped updating, or you can't do anything with them, which were working fine earlier, You can't update service reference, you can't delete them, once you remove everything and give the reference again, the error fires back again and you can't repeat the same procedure of deleting and inserting the reference over and over



See the following screen shot



Above is the screen shot which i kept on getting and searched for the solution with best of my googling capabilities untill i got the solution which is below, Before that there were many solutions like the classe names which WCF (wsdl) generates were larger than 256 characters, But i could not use that solution for some reason, other solutions were re intalling some of the MS tools and re installing them, Below is the solution which worked for me and i am posting so other can also make use of it

Solution

Creating a batch file instead of manually adding service reference (WCF reference) will only generate the wsdl files which is required to access the data of it and functions inside it and via which the above error will be gone

@ECHO OFF

set UIProjectServiceRefPath =..\..\MyApplication\ServiceReferences

set SVCUTILPATHValue =C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools

ECHO Generating CommonService.CS
"%SVCUTILPATHValue %\svcutil.exe" /o:"%UIProjectServiceRefPath %\CommonService.cs" /reference:"c:\MyDomainService\bin\Debug\Domain.dll" /serializer:Auto /edb /ct:System.Collections.Generic.List`1 /ixt  /namespace:*,MyService.CommonService http://localhost/MyService.BLL/CommonService.svc?wsdl

set /p delBuild=press any key to exit:

  • Create a .bat file (batch file) like above, very first line after echo is where you want to have those class files to be created, this would be same path where your WCF service reference were getting created, so replace your path having that in mind
  • Second Echo statement is your actual service file which you want to generate, now if you have 3 services, you will these type 3 echo statements with their respective paths, here /reference is any reference DLLs your WCF might be using, so give those DLLs path there, you can have multiple /reference nodes followed by one another
  • /namespace is via which you will be referencing your classes to create their objects, and .svc is your hosted WCF service, the path which you were using to add service reference.


Now remove your existing "Service reference" folder which you have generated from previous WCF manual add service reference from windows explorere and also from your solution, and doucle click this file, it will generate "ServiceReference" folder at the smae place and inside that it will put above .CS files which is in .dat file



Your are good to go, go to your solution include this folder and build your project, if you have configured it properly it will build with no error, and now whenever you want to update the service reference, just double click this file to generate new WSDLs



Feel free to comment here in case of any doubt, I would like to mention here the valuable help from http://k-sharp-net.blogspot.in/ (Kamlesh samnani, very good colleague of mine)

Friday, March 28, 2014

401.1 Unauthorized with windows authentication error code 0xc000006d

How many of you have faced this hosting issue when you do everything what it takes to run the site with windows authentication but still you are getting the same error again and again?



If you think you also have faced the same issue and you tired of reading MSDN KBs for it and still have not found the issue (If KB has solved the issue, well and good, if not you can try this trick),Please Read below



Typical scenario


In typical hosting with IIS, i did every possible things like enabling windows authentication, changing it in web.config, configuring connection pool, authorization rules, it asks me for window authentication login and despite of entering correct credentials it always fails and keeps on asking for login, and when pressed cancel it gives 401.1 with 0xc000006d error code

Solution (Which worked for me at-least after trying for almost 6-9 hrs)


You need to change the Loop Back Check in registry so that it allows the host names which you are giving in url are allowed and authorized, Following are the steps



  • The solution is some registry hacking to avoid loopback check:
  • Using regedit, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
  • Add a new Multi-String Value to MSV1_0 and name it BackConnectionHostNames
  • Add the host names you wish to use. In my case, "test.mytestsite.com".
  • Restart the IIS.




I got this important information from http://falunbyday.blogspot.in/2013/02/windows-authentication-fails-on-local.html,It was life saver




Thanks, Enjoy

Thursday, March 13, 2014

File upload reference on client side for client side processing like crop image or anything

I have had a requirement were i needed to include the crop functionality in already developed file upload control, requirement was user should be able to crop the images he selects and later on when he finalize the selection, and that file should get saved on server so next it is available to user
Here our tradition approach is always like, we upload the file on server on some temporary location, so that it is available for cropping to user, and he can apply cropping on client side and send that to server with its crop selection and on server side we can process that image and crop on server side with already uploaded image and save so only cropped version will be saved and use that image
There is another approach which can save us from doing the upload first, crop and later on again save that image, the process is like following, Find the following code



Consider following is my upload control










Now,Consider following javascript registered which will take the file reference on client side and return the byte array of image and directly show in preview on the fly and very fast, as it is yet not uploaded on server, so process is very fast


function getImageFromClientSide(file) {

    var reader = new FileReader();
    var image  = new Image();
  
    reader.readAsDataURL(file);  
    reader.onload = function(_file) {
    image.src    = _file.target.result;              
    image.onload = function() {
           var w = this.width,
           h = this.height,
           t = file.type,                           
           n = file.name,
           s = ~~(file.size/1024) +'KB';
           $('#uploadedImagePreview').attr('src',this.src);
        };
    };
    
}
$("#imgSelector").change(function (e) {
    getImageFromClientSide(this);

});



As you can above, what it is using is FileReader method of DOM and reading a file from client's computer, and it will convert image into byte array of base 64, now you can treat this as a string and you can send this image to server side using ajax or anything as a string, and on server side (i am using asp.net c#, and you can use following


byte[] imageBytes = Convert.FromBase64String(imageByteArray); // Here imageByteArray is my string variable which contains the byte array string sent from client side 
   TypeConverter tc = TypeDescriptor.GetConverter(typeof(Image));
   Image OriginalImage = (Image)tc.ConvertFrom(imageBytes))



As this whole process is working on client side, this is very fast in performance, I got the reference of my search and r&d from http://jsbin.com/oTAtIpA/490/edit url and i implimented it from the same, i hope this will help some of you coders
Thanks you !!!

Friday, March 15, 2013

Same old file reference in project when 'Add reference' issue

Here is the "STUPID" scenario i came across,Following is the problem i had

Issue
I had one folder in my computer and it had two different version of dlls,My project already had reference of one of the dll,But now i wanted the reference of other dll,I simply deleted the old reference and tried to add new reference of the other file by selecting it,It was added but was showing old version when i tried to see its property ,I tried multiple times with multiple ways but no luck

Solution


First just check out the folder structure i had in my project,Here "Dlls" is the folder from which i wanted to add another file


You can see the "Dlls" folder is included in the project, And thanks to "Mr. Zakir Rana", One of project partner who insisted me to try excluding this folder and adding reference, It sounded foolish to me as its physical folder after all no matter if it is included or not,But i was surprised as soon as i excluded the folder,And it started taking new reference !!!

Wednesday, February 20, 2013

Ajax call inside for loop issue and solution


Hello people, This post is regarding real day to day issue in coding and its resolution, It sometimes looks small issue but when it starts eating your time you start getting feeling that its no more smaller one, Here goes the issue

As the title of the blog suggests, When you are making AJAX calls inside loop, It is finitely possible that inside loop when the call is made, It takes time to get the result back to client side and by that time loop has already ended and counter has reached to limit and you end up thinking how to keep track of the request, it should get the correct index in "Success" method of $.ajax call, Otherwise it will keep on giving you one count higher than your total count of loop, There can be many way as we know in our world but below is one of the good solution to it

Solutions
See below code for an example

for (var i = 0; i < groupCount.length; i++) 
{
                !function (i) {
                    $.ajax({
                        type: 'POST',
                        url: ,
                        data: data,
                        dataType: "json",
                        success: function (respons) {
                            // Do some action
                            
                        },
                        error: function (err, para1, parra2) {
                           // Do some action 
                        }
                    });
                } (i);

Here we can see we have wrapped the ajax call into !function(), It will make sure that your ajax call works, no matter what !!!, One more dirty fix is that we can change the property called async=false in $.ajax parameters but that does not serve the purpose as when we use ajax it has to be async.

Small but helpful !!!

Tuesday, January 8, 2013

Facebook Graph API calls for facebook integration

FACEBOOK the world of it's own,But it sounds good if you get its power in your site,Photos,Status,Like,Commenting,Facebook albums, In short you can simply go and create your app which does everything for you without even going on to facebook

Facebook has its very superb API which you can find on developer.facebook.com, You can download full binaries to be coded in C# but i will skip that and put that on readers to explore as and when you have time,Lets talk something short and sweet

Facebook Page
Here i am only going to talk about the get API calls,That means only things which only get the data from any public page,Say you have a page on facebook for your business and you want the data from your facebook page to be shown onto your website,You can use these calls,Fast,Easy and Fun,Lets get started

Say i have a photography page on my facebook or any other page,You can get its photo albums and its photos by following code,Some of the things required to make a call are following

  • Unique Id or name of the facebook page
  • Facebook Graph API url on which you will make a http request and wait for the response
  • Each call are different (i.e photos,albums etc,Depending on your requirement) so proper URL of those calles

The Calls
Lets say i want to get the basic details of my FB page,like it's description,cover photo,profile pic,so following is the url you would be using

NOTE: Here i am using my photography page to show,You can replace the page name with yours

Get the FACEBOOK page basic info
https://graph.facebook.com/147326062077207

Try hitting above url into browser and you will get an idea what you would be doing,As it returns well formed JSON string and you will make use of that JSON and display the things
Get the list of photo albums on your page
https://graph.facebook.com/147326062077207/albums

Get list of photos into specific album

From above url you will get list of albums with their album id,and in below url you will pass that album id to get photos inside it

https://graph.facebook.com/153930304750116/photos
Getting album cover photo to show

From above url you will get list of photos for particular album,and if you see its JSON string,The 'picture' property is thumbnail preview of the cover photo, So you can take that 'picture' property and show that image from FACEBOOK's CDN.

There are many other useful properties and functions with GET and POST request,I will be covering them in next blog post, Enjoy playing with it,And do provide feedback !!!

Monday, October 22, 2012

Importance of flushing memory

As we know in our day to day life how important 'Flush' is :),But that teaches us same thing in our programming life as well

Let me share a scenario where i faced the issue,In our day to day life of deadlines,Sometimes we forget to write the code which is actually needed there,You achieved the best with your code but you do not achieve the better sometimes

The Scenario :

I before few days created export to CSV and export functionality of my one of the list (Grid),It was importing the data very well,But on the close examination of data i found out that its inconsistent data,At some point of time it stopped writing the data and from that point onward there were no data in exported file.

Following is the code.


HSSFRow row = wsheet.GetRow(i);
for (short j = 0; j < row.LastCellNum; j++)
{

     HSSFCell cell = row.GetCell(j);
     string strValue = string.Format("\"{0}\",",   Convert.ToString(cell.RichStringCellValue.String));
       StreamWriter.Write(strValue);
       
 

}
StreamWriter.WriteLine();
StreamWriter.Flush();

If you see above code you do not find any issue,Same happened with me,I tried debugging it over and over,logging the operation but surprising thing was in log each and everything was coming,And i end up finding the issue was below

Issue The mistake i did was i was not clearing our StreamWriter from memory by flushing the memory and hence it was creating issue,One single statement introduced and woooo...issue got resolved like below


HSSFRow row = wsheet.GetRow(i);
for (short j = 0; j < row.LastCellNum; j++)
{

     HSSFCell cell = row.GetCell(j);
     string strValue = string.Format("\"{0}\",",   Convert.ToString(cell.RichStringCellValue.String));
       StreamWriter.Write(strValue);
       StreamWriter.Flush();
 

}
StreamWriter.WriteLine();
StreamWriter.Flush();

Everything started working as it should be,Simple but very important to follow the theories.