Open Notepad from Silverlight 4 Business Application - silverlight

I have a Silvleright Business Application, Silverlight_BussApp.
It has the Silverlight_BussApp project and the Silverlight_BussApp.Web project.
I need to open Notepad after populating it in the code behind on a button click event.
I cannot make this an out of browser application.
So since Silverlight does not allow me to do it, I created a WCF Service in the Silverlight_BussApp.Web project
and invoked it through a proxy in the Silverlight Project. This works fine.
The code in the WCF Service is:
public void openFileWithNotepad(string filename)
{
try
{
if (File.Exists(filename))
{
Process.Start("notepad.exe", filename);
}
else
{
MessageBox.Show(
"I was unable to locate the following file: " + filename,
"File Not Found");
}
}
catch (Exception e)
{
throw e;
}
}
Now I need to use this openFile functionality in more than one Silverlight Project.
So I created a "WCF service Project" and created a WCF Service, hosted it on IIS, and created a proxy in the Silverlight project. To my dismay, the file does not open and it keeps giving me the "File Not Found" Error.
Can somebody help me figure out how I can make this a reusable functionality for all my Silverlight projects?
Why is it when I use the same code in a WCF Service hosted on IIS it does not work while it works perfectly when hosted in the same solution of the Silverlight project?

For starters, the code in the Silverlight_BussApp.Web project is going to be executed on the server, while the code in the Silverlight_BussApp project is going to be executed on the client. During development, the client and the server are ostensibly the same machine -- when you click "Run" in Visual Studio, a development web server starts up on your machine and hosts your .Web project, while the Silverlight application runs in your browser.
When your application is deployed, the client (i.e., your browser) will most likely be on a different physical machine than the server (i.e., IIS). So, ignoring any other security restrictions that may be imposed by IIS (which is probably why you're getting a "File Not Found" error), the call to Process.Start would start Notepad on an entirely different machine than the one you're using.
Try using a SaveFileDialog to save whatever you want to the client's machine, then tell them to open the resulting file.

Related

Lync 2010 Plugin - Silverlight WPF with elevated permissions

I'm developing a CWE (Conversation Extensibility Window) with WPF and Silverlight 4 on Visual Studio 2010, for Lync 2010.
My application reads a list of .xml from the root directory of the application and into a string array.
I've this code:
bool featureComplete = Application.Current.HasElevatedPermissions;
if (featureComplete)
{
List<string> files = new List<string>(Directory.EnumerateFiles(textBox1.Text, "*.*"));
mensajes.Content = files.Count;
}
else
{
mensajes.Content = "no trust";
}
In the event handler of a button event. With any path (my documents, root, application, etc) the application says "no trust".
I change the properties on the build to Out-of-browser settings and also check "require elevated trust" but nothing happens.
I tried and tried looking for an answer on google and msdn but I could not find a solution.
So there's a checklist or step list to make a trusted CWE on Lync2010 with silverlight? I forgot something?
Please remember: this is not a usual web application, its a lync 2010 app.
ALSO: I can do it with "debug mode" with special folders like this tutorial: http://www.silverlight.net/learn/graphics/file-and-local-data/local-file-access
and it works, but when I run it under lync 2010 it says "access denied" for that folder.
There is the code that works only on debug as an application, and not works like lync applet:
List<string> picsinfolder = new List<string>();
var pics = Directory.EnumerateFiles
(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
foreach (var item in pics)
{
picsinfolder.Add(item);
}
listBox1.ItemsSource = picsinfolder;
Thanks a lot in advance.
PD: Work-arounds that fix the issue can be accepted as an answer
PD2: No, signing the xap doesn't work.
The work around I'm using and works is open an XML, reading, copy into Isolated Storage, read it again from there. Everytime I need to change the file, I read it and copy again on Isolated Storage

Windows Authentication in silverlight Vs 2010

I want to implement windows Authentication in silver light, How to do that ?
There is a workaround if you are hosting your Silverlight application in an ASP.NET page.
Make sure that your website (hosting the Silverlight .xap and ASPX page) has Windows Integration security enabled, and anonymous access disabled.
Add the following to your list:
<param name="initParams" value="myCustomParam1=Foo,userId=<% System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal; Response.Write(p.Identity.Name); %>" />
This will pass your username pulled from ASP.NET into your Silverlight application.
Add this line to your App.xaml.cs page, in the Application_Startup method:
// Take parameters and store them in application resources
if (e.InitParams != null)
{
foreach (var data in e.InitParams)
{
this.Resources.Add(data.Key, data.Value);
}
}
Once you have the above steps in place, you can access your value from page code behind using the following:
App.Current.Resources["userId"].ToString();
Also, as an alternative, if you run your application on an Intranet, and run it in out-of-browser mode with elevated security, this is all much easier. You can access the Windows API using this:
if (Application.Current.HasElevatedPermissions)
{
using (dynamic wshNetwork = AutomationFactory.CreateObject("WScript.Network"))
{
return (wshNetwork.UserName);
}
}
As far as I know it's not possible to use Windows Authentication "directly" in Silverlight (at least with a self-hosted WCF service -maybe with IIS there is some support?).
An acceptable way to accomplish this in my opinion is to pass username/password to your server and there you query the Active Directory using an LDAP library. Make sure to use SSL for your service calls otherwise the credentials will travel in clear over the wire.

Can silverlight detect or communicate across browser instances?

User starts up a silverlight application in their browser by navigating to a given URL.
User then opens another browser and starts up the same silverlight application by navigating to the same URL.
Can the second instance of the application detect that there is already an instance running on the same computer?
Can it detect itself if both applications are running within the same browser instance?
I would expect the answer to be 'no' but thought that i would ask it anyway. Otherwise i believe that i will have to setup a webservice and have each instance register itself and send requests to other instances from the same IP. does that sound reasonable?
I think you may be looking for LocalMessageSender and LocalMessageReceiver. I believe these are new classes in Silverlight 3 allowing two Silverlight applications running on the same local computer to communicate.
More detail: Communication Between Local Silverlight-Based Applications (msdn)
This will work, I've done it myself. This code from the Microsoft site demonstrates how you set up a LocalMessage 'receiver". If it throws an error, it is because another instance of the Silverlight app is already running.
public Receiver()
{
InitializeComponent();
LocalMessageReceiver messageReceiver =
new LocalMessageReceiver("receiver",
ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
messageReceiver.MessageReceived += messageReceiver_MessageReceived;
try
{
messageReceiver.Listen();
}
catch (ListenFailedException)
{
output.Text = "Cannot receive messages." + Environment.NewLine +
"There is already a receiver with the name 'receiver'.";
}
}
I think you're right you can't do it cross-application, but you can do it within a single browser instance using cookies or Isolated Storage.

Deployed silverlight enabled WCF Service Stops Working when development server is stopped

I have a silverlight business aplication that gets data from silverlight enabled webservice.
When I run the application in dev environment, it works fine.
when i deploy the application and the Asp.net web development server is working, then to the application works fine.
But when I stop the development server, the application can't access a service.
My questions are:
When I deploy a silverlight business application, doesn't the service get deployed and get started.
The endpoint address in my ServiceReferences.Clientconfig file is endpoint address="http://localhost:9702/MyWebservice.scv. Do I need to change this?
The enpoint address in the web.config is blank.
Appreciate your help
Because the WCF client code is declared as a "partial" class, what I've been doing to this point is creating another c# partial class file to host a GetClient() method on it. You'll notice that the code is taking into account the port that the service is on... in a few of the environments that I've posted or will be posting to, as well as the development environment, the application is not always on port 80.
Namespace Project.Service
{
public partial class ServiceClient
{
public static ServiceClient GetClient()
{
return new ServiceClient("CustomBinding_Service",
new System.ServiceModel.EndpointAddress(new Uri(string.Format("{0}://{1}:{2}/Services/Service.svc",
Application.Current.Host.Source.Scheme, Application.Current.Host.Source.Host, Application.Current.Host.Source.Port), UriKind.Absolute)));
}
}
}
Hope this helps someone!
Yes you are going to want to change your endpoint address. I recommend doing it in the silverlight code when creating the connection to the WCF service. The service itself lives on the web server, whereas the silverlight application lives on the clients computer. If the web server stops, the web service stops but the silverlight app can keep running.
edit:
To do this in code, as long as the path is always in the same domain as the app you can use do like so:
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None) //Use whatever security you need here
{
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue
};
Client client = new Client(binding, new EndpointAddress(new Uri(Application.Current.Host.Source, "../MyService.svc")));
Thanks so much for your help. I tried your approach to create the client code but that didn't work. And that's because the problem seems to be somewhere else.
So I installed fiddler to see the traffic.
Fiddler showed that the service was being accessed but the http response code was 302 showing that there was some redirection involved.
The address of my application is like this http:///Silverlightapp/(S(oirppxrwzhlf2a2vbia1ui45))/Default.aspx#/Home and it is hosted on IIS 6.
So I had to employ a workaround by installing the service on machine with IIS7 (and there was no session id involved like in the above url).I still kept the silverlight application hosted on IIS 6.
Anyway, in summary, to anyone who reads the thread, I did the following to troubleshoot and solve issue(temporarily)
Changed the end point address in the ServiceReferences.ClientConfig file. When you add the service using discover option in VS, the endpoint address is of the localhost and this must be changed.
Registered the service model using ServiceModelReg -i command. (this solved my problem that my applicaiton was only working from development server and not IIS)
-Put the CrossDomain and ClientAccessPolicy files in c:]inetpub\wwwroot folders.
-Used fiddler to look at http response codes. I had to do no configuration in fiddler.
Changed the binarymessageEncoding to textMessageEncoding iin the web.config file of the silverlight web project that also hosted the ecf service. I did this becasue adding a silverlight enabled wcf service creates a custom binding configuration in the web.config file by default uses binary encoding. I needed text encoding to see errors in fidder. But this didn't help much becasue I only saw the name of the operation in the Inspector>xml tab in fiddler. This was the same even after my issues was resolved by workaround.
Thanks for the help
Don't do it in code. Otherwise you won't be able to change it later without recompiling the application (when the address will need to change, perhaps years down the road when you've lost the source code :)
Change the address in ServiceReferences.ClientConfig to where the service is actually hosted... e.g. http://example.com/myVdir/MyWebservice.svc
If later on you need to change the address without recompiling:
- Open the .xap file (it's just a zip file with a different extension)
- Find the .ClientConfig file and change the address
- Put it back together as a .zip file and rename to .xap
Also, I can't remember anymore whether the .ClientConfig supports relative addresses (e.g. just "MyWebService.svc"), but if it does it may be a good solution as well.

WPF to WCF Permission issue

I have created a WPF browser application that I wish to connect to a WCF service. The service is in the same solution as the WPF application. It is the default service that Visual Studio creates with a method called GetData (int)
I am attempting to call this service with the following code:
var client = new Service1.Service1Client();
client.GetData(10);
I get the following error on the above line (client.GetData(10);)
{"Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."}
Please help
This solution from Scott Lanford worked for me:
http://www.codeexperiment.com/post/Debugging-XBAPWCF-Applications.aspx
Quote:
The possible reasons for this error are numerous but in this case it was because the default Debug->Start Action for an XBAP project is "Start browser in URL", where the URL simply points to the .xbap file on disk (e.g. C:\projects\myproject\bin\debug\myproject.xbap).
For a partial trust XBAP application to communicate with a WCF service it must be deployed from the same domain and port as the WCF service. The way to solve this dilemma is to fake the XBAP URL by starting the XBAP application with PresentationHost.exe and supplying the XBAP URL (copied from the "Start browser with URL" value) via the debug parameter and the WCF service URL via the debugSecurityZoneURL parameter.
For example the debugging options for your XBAP project should look something like this:
Start Action -> Start external program = %windir%\system32\PresentationHost.exe
Start Options -> Command line arguments = -debug "c:\projects\myproject\bin\debug\MyProject.xbap" -debugSecurityZoneUrl "http://localhost:2022"
You need to setup a client access policy on the server. Running in the browser comes with a bunch of security limitations.
I think this article should get you the information you need, http://www.dotnetcurry.com/ShowArticle.aspx?ID=208&AspxAutoDetectCookieSupport=1.

Resources