Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Monday, June 16, 2014

You need to install the latest Silverlight Developer runtime before creating a Silverlight project

After a long gap, I installed Silverlight in my development machine to look into a performance issue in an internal company tool. As per the policy the "internal projects don't have the luxury of testers" so the actual testing happens on production hence so many issues especially performance. Its a Silverlight 5 project and I installed the Silverlight 5 tools from the Microsoft site. When I tried to open the solution the first error I got was given below.

You need to install the latest Silverlight Developer run-time before creating a Silverlight project
Download runtime at <url>

I thought, the Silverlight tools version I installed may be 32 bit version where its expecting 64 bit version. So downloaded the version which is mentioned in the error dialog. But when I tried to install it, the next message came.

Newer version of Silverlight is already installed

Interesting. Now no other way remove all the items from control panel by searching the word Silverlight and install only Silverlight 5. This I had did so many times when in active development of Silverlight applications. But this time that too didn't help. So decided to google.

The solutions people provided in internet are different. Some says we need to follow the link provided in the visual studio error message some posted a link from where we should download the run time, another one suggest to install Visual Studio 2010 Service Pack 1 to get Silverlight 5 working.

Installing VS 2010 SP1 takes more time, that is one of the reason why I didn't install it. As there are no other option I had to install VS 2010 SP1 and it started working!!!

These are the occasions, we think about the error messages we coded which is absolutely useless for the users. Thanks Microsoft...

Tuesday, July 3, 2012

Net.TCP in Silverlight from IIS+WAS or ConsoleHost

Background
As usual a requirement came from onsite to one of the development team that they need to host WCF service using net.tcp and access the same from Silverlight 5. A developer spent couple of days but no luck. Then it came to me as I am serving as technical lead. First itself I raised the concerns on using net.tcp with Silverlight as Silverlight implementation lacks security features of net.tcp. Only advantage is the reduced payload size and speed. But there were no clear answer. So started debugging session to correct the sample.

There are so many articles and forum posts about the same with step by step explanation. But if we look closely at the comments, it says most of the people were not able to accomplish the task of setting net.tcp in IIS 7 and call the same from Silverlight. Also we were not able to find any good sample which does this. So planned to post the sample we used for net.tcp to Silverlight 5 communication which I think will help a lot of people.

Hosting net.tcp in IIS + WAS Host and accessing from Silverlight
I don't want to duplicate the contents here as you can get detailed steps with screen shots from the links given below. But would like to highlight the points which we noticed while doing the task.
  • Make sure you have enabled non-http activation in Add remove programs->Turn windows features on or off-> Microsoft .Net framework 3.5.1
  • Make sure the net tcp related listener and adapter services are running in Services.msc
  • The clientaccesspolicy.xml file should be present in the root of IIS with default port 80.
    • http://localhost/clientaccesspolicy.xml must be present regardless you are pointing to c:\inetpub\wwwroot\clientaccesspolicy.xml or some other path
    • The contents should be same as described in any of the links below. Main thing is the tcp port range 4502-4534.
  • The tcp port range which Silverlight supports for net.tcp communication is 4502-4534 .So your service need to use port number which comes in this range. Eg net.tcp://localhost:4503/
  • The port which you are using in the above step must be open in the firewall. Better switch off firewall if you are facing any issue.Once you get it running on firewall and add exception.
  • If you are hosting the services in "Default Web Site" of IIS make sure the edit site->bindings are pointed to the port numbers in the range 4502-4534. eg: 4504:*
  • If you are in Visual studio make sure you are using IIS for running WCF services. Create the IIS web application from Project properties->Web->Use  local IIS web server
  • Make sure the "Default Web Site" and web application in IIS has net.tcp in the enabled protocols section.
  • Turn off the security features of net.tcp in the web.config as Silverlight doesn't support the same .By default in .Net the tcp security will be on. Better make all the security related attribute in the netTcpBinding to "None".
  • A <clear/> tag is advised in the <Service> element in web.config before adding the end points.
  • Try to "Add Service Reference" using the net.tcp://site/service url itself. This will make sure that the server side is correct.
  • In ServiceReferences.ClientConfig use the CustomBinding and inside that binaryMessageEncoding & tcpTransport. There is no security related element in Silverlight config.
Hosting net.tcp  in ConsoleHost and accessing from Silverlight
This is another scenario which I tried along with hosting in IIS and though of sharing the same. Below are the points to note.
  • There is no need to run any additional exe as socket policy server which was there till Silverlight 4 beta.
  • While adding service reference you can use the mex url.eg:net.tcp://localhost:4503/mex
Challenges faced
  • We added the service reference with http:// url which worked fine.But when we tried to call using net.tcp url it failed. So always use net.tcp url to add service reference.
  • We tried to add some security related attributes in the ServiceReferences.clientconfig file which caused more errors. Leave it minimal.
  • There were some exceptions which we cannot catch in Visual Studio.Use the diagnostic trace listeners on System.ServiceModel so that debugging will be easier.For example the below one was never caught by VS
  • We were getting an exception at the server side (service svclog) that there is a protocol exception .We resolved it by making all the security related attributes of nettcpbinding to None. The exception was System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0.0.0."Stream Security is required at http://www.w3.org/2005/08/addressing/anonymous, but no security context was negotiated. This is likely caused by the remote endpoint missing a StreamSecurityBindingElement from its binding."
Running the attached sample

Download the sample and read the below points about the same.
  • WCFWASService project contains the WCF service .Make sure this web project is running using IIS.
  • ConsoleHost project contains a service which is exposed via net.tcp://localhost:4503/GetData .Goto the bin/debug folder of this project and double click on the ConsoleHost.exe to start the service.
  • SilverlightApplication1.Web is the web host for silverlight application.
  • SilverlightApplication1 is the Silverlight 4 app from which we are going to access the IIS hosted and Console hosted net.tcp services. The url to IIS hosted service is in the ServiceReferences.clientconfig file and the other is hard coded in the method Implementation.TestGetData()
  • If you want to add the service reference of ConsoleHost to any other app use the url net.tcp://localhost:4503/mex
Links which explains step by step
http://tomasz.janczuk.org/2009/11/pubsub-sample-with-wcf-nettcp-protocol.html
http://www.ditran.net/self-hosted-nettcp-wcf-silverlight-4
With screen shots - http://www.silverlightshow.net/items/WCF-NET.TCP-Protocol-in-Silverlight-4.aspx

Tuesday, February 14, 2012

Handling WCF FaultContract & FaultException in Silverlight

What is FaultContract in WCF and how to handle fault exception in a windows application has been posted 1 year ago in this same blog itself. Some time back we one of colleague was trying to implement the same in his Silverlight application and he was not able to leverage my post to accomplish his task as Silverlight uses async pattern to call the service and the binding is basicHttp. The basicHttpBinding uses the SOAP protocol and it returns the fault using the HTTP 500 series status code which cannot be understood by Silverlight. So we modify the WCF response using custom EndPointBehavior and a MessageInspector to return using HTTP 200 code.

All those details are specified in the MSDN itself. What I am doing here is to post a sample in Silverlight 4.0 :-)
http://msdn.microsoft.com/en-us/library/ee844556(v=vs.95).aspx

No its not just a sample. It describes how to write client side code which is not mentioned in the MSDN article.Mainly where to put try catch to catch the fault exception. For the server side code and configurations just look at the MSDN article. Client side code you can get from below section.


   1:  private void GetChar(String ipString, int position)
   2:  {
   3:          MyServiceReference.MyServiceClient client = new MyServiceReference.MyServiceClient();
   4:          client.GetCharCompleted += (sender, e) =>
   5:          {
   6:              try
   7:              {
   8:                  if (e.Error != null) throw e.Error;
   9:                  MessageBox.Show("Extracted character: " + e.Result.ToString());
  10:              }
  11:              catch (FaultException<MyServiceReference.MyException> ex)
  12:              {
  13:                  MessageBox.Show("FaultException occurred " + ex.Detail.ExMessage);
  14:              }
  15:              catch (FaultException ex)
  16:              {
  17:                  MessageBox.Show(ex.Message);
  18:              }
  19:          };
  20:          client.GetCharAsync(ipString, position);
  21:  }

The main difference is the position of try catch blocks .In the sync service calls it will be wrapping the service call itself.Here we have 2 ways as the exception comes through the e.Error property.Either we can check the type in the handler itself or throw to handle by the callers.

Monday, November 28, 2011

Installing SSL certificates to Windows Phone 7 emulator

The days are again getting tight due to new technologies like Windows Azure, Windows Phone 7 & iPhone development. It is always interesting to work in new technologies where we learn so many new things ,hacks etc…My recent project uses Windows Azure as server side technology and mobile as client side. For the time being we are using iPhone as client.

When we start with Azure we could see that we can simulate the https scenario even in the emulator which is more like the production environment. Really like it. But after some time, it became trouble. There were not enough MAC machines where we can test the iPhone application. So I moved to WP 7 as client for testing purpose. I integrated the Azure tool kit for WP 7 and when tried to access the Azure ACS, it started throwing errors .Our REST based services hosted using HTTPS in emulator are not working. ie simply says network error.

After a google we could see that it is due to lack of certificates. If it was some other .Net client I can live with ServicePointManager and its validation callback. But unfortunately there is no such ServicePointManager in Silverlight. So it is proved that WP7 never allows you to access the https url without proper certificate. But how to install certs in WP7 .When I asked my fellow developer who has Windows Phone, he told that he normally send the certificate file (.cer)through mail and the opening of attachment in phone will prompt for installation.

But where is the mail client in simulator. Tried to send to my gmail id. But no luck.Then I tried the next way. Export the certificate into .cer format, host the .cer file in IIS and access it from Phone’s browser.Yes it worked. I was able to install the certificate. But again I got error in getting data through REST service. That was due to certificate URL issue .I should have used the exact certificate which is created for the IIS. ie for the localhost/127.0.0.1.

Below are the steps to install the certificate in WP7 and get the Azure REST services working which is hosted in Azure emulator.
  • Export the certificate as .cer
    • The certificate should be for the exact web address. ie export the certificate for 127.0.0.1 from IIS.
  • Host this certificate .Even in your http://localhost/<cert Name>.cer
  • Make sure your IIS has the proper MIME type for the .cer
    • MIME type for .cer certificate file is application/x-x509-ca-cert
  • Browse to this certificate path from Windows Phone 7 emulator.
  • Install the certificate.
Now you are all set to access https service hosted Windows Azure emulator from Windows Phone 7 with debugging support.
Enjoy… production environment in development.

Wednesday, January 20, 2010

My First Look at EnterpriseLibrary.Logging

Last week I got opportunity to work with the logging module of our application.It was done by one of my colleague and he told that it is done using Logging module of .Net EnterpriseLibrary.
After looking around the code, I was surprised on the lines of code below which does the logging.Rest is all taken care by configuration entries in web.config.

using (new Tracer("My Tracer"))
{
LogEntry le = new LogEntry() { Message = message };
Logger.Write(le);
}



When I looked into the web.config I was able to see some entries in a section named loggingConfiguration




<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />



The above code should be inside configSectionsTag.The rest is a new section as follows added after the config sections.




<loggingConfiguration name="Logging Application Block" tracingEnabled="false"
defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add fileName="log.log"
header="-------------Log Message---------------------"
rollFileExistsBehavior="Increment"
rollInterval="None"
rollSizeKB="1024"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None"
filter="All"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="FlatFileListener" />
</listeners>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors &amp; Warnings">
<listeners>
<add name="FlatFileListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>



I hope the config is self explanatory.It uses a flat file writer which writes into the file log.log present in the root directory.

I usually afraid of these config entries which cannot be memorized by a developer.If you are also afraid of the config entries use the Enterprise Library Configuration Tool.


This is just a sample.There lot more things which we can do with the logging application block such as format the text which is writing into the file.More properties such as priority,category etc…,Variety of logging  locations such as database,email etc…If I get some more extra time I will surely blog about the same.More details can be found in the below links.



http://bloggingabout.net/blogs/dennis/archive/2009/07/21/quickstart-tutorial-into-enterprise-library-logging.aspx


http://www.codeguru.com/csharp/.net/net_framework/systemnamespace/article.php/c11281/


http://www.codeproject.com/KB/architecture/GetLoggingWithEntLib.aspx



NB : Don’t forget to download the CAB and add reference to the following dlls when  you start a new project.




  • Microsoft.Practices.EnterpriseLibrary.Common.dll


  • Microsoft.Practices.EnterpriseLibrary.Logging.dll


  • Microsoft.Practices.ObjectBuilder2.dll



I have uploaded a sample here.

Thursday, May 14, 2009

Comparison of WSHttpBinding and BasicHttpBinding in WCF

Criteria BasicHttpBinding WSHttpBinding
Messaging Soap 1.1 Soap 1.2
Security Very less More
Security level SSL @ Transport layer SSL @ Transport and MessageLevel
Speed High Low
Ordered delivery No Yes
Compatibility Like asmx.Older versions can Older .Net versions can’t since this implements more WS specs
Silverlight supports Yes Upto Silverlight 2 doesn’t support.

I will be adding more as and when I come across.

More links

http://tech2update.com/blog/?p=105
http://msdn.microsoft.com/hi-in/magazine/cc163570(en-us).aspx#S6