如何让 Silverlight 从 MySQL 获取数据 [英] How to have silverlight get its data from MySQL

查看:17
本文介绍了如何让 Silverlight 从 MySQL 获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Silverlight 中编写了一个小型的 hello world 测试应用程序,我想将其托管在 Linux/Apache2 服务器上.我希望数据来自 MySQL(或其他一些与 linux 兼容的数据库),以便我可以将数据绑定到数据库中的内容.

I've written a small hello world test app in Silverlight which i want to host on a Linux/Apache2 server. I want the data to come from MySQL (or some other linux compatible db) so that I can databind to things in the db.

我已经通过使用 MySQL Connector/.NET:

MySqlConnection conn = new MySqlConnection("Server=the.server.com;Database=theDb;User=myUser;Password=myPassword;");
conn.Open();
MySqlCommand command = new MySqlCommand("SELECT * FROM test;", conn);
using (MySqlDataReader reader = command.ExecuteReader())
{
     StringBuilder sb = new StringBuilder();
     while (reader.Read())
     {
         sb.AppendLine(reader.GetString("myColumn"));
     }
     this.txtResults.Text = sb.ToString();
}

如果我完全信任已发布的 ClickOnce 应用程序(或至少是 SocketPermission)并在本地运行它,这会很好地工作.

This works fine if I give the published ClickOnce app full trust (or at least SocketPermission) and run it locally.

我想让它在服务器上运行,但我无法让它工作,总是以权限异常结束(不允许使用 SocketPermission).

I want this to run on the server and I can't get it to work, always ending up with permission exception (SocketPermission is not allowed).

如果有任何不同,数据库与 Silverlight 应用程序托管在同一台服务器上.

The database is hosted on the same server as the silverlight app if that makes any difference.

编辑好的,我现在明白为什么在客户端应用程序中拥有数据库凭据是个坏主意(显然).那么人们如何做到这一点呢?您如何保护代理 Web 服务,以便它以安全的方式将数据转发到客户端/数据库或从客户端/数据库转发数据?网上有没有例子?

EDIT Ok, I now understand why it's a bad idea to have db credentials in the client app (obviously). How do people do this then? How do you secure the proxy web service so that it relays data to and from the client/db in a secure way? Are there any examples out there on the web?

当然,我不会是第一个想要使用数据库来支持 Silverlight 应用程序的人吗?

Surely, I cannot be the first person who'd like to use a database to power a silverlight application?

推荐答案

做您想做的事情的最简单方法(现在已阅读您的编辑 :))将公开可以使用的服务.Microsoft 现在真正推动的模式是公开 WCF 服务,但事实是您的 Silverlight 客户端可以使用 WCF 来使用许多不同类型的服务.

The easiest way to do what you want (having read through your edits now :)) will be to expose services that can be consumed. The pattern that Microsoft is REALLY pushing right now is to expose WCF services, but the truth is that your Silverlight client can use WCF to consume a lot of different types of services.

现在对您来说最简单的方法是在 Web 服务器上使用 .NET 服务或 PHP REST 服务,然后将您的 Silverlight 应用程序指向该服务.通过这样做,您不仅可以保护您的数据库免遭他人窥探,更重要的是,您可以限制人们对您的数据库的操作.如果您的数据应该是只读的,并且您的服务合同只允许读取操作,那么您就设置好了.或者,您的服务可能会使用凭据协商会话,再次通过 WCF 设置.

What may be easiest for you to do right now would be to use a .NET service on a web server or maybe a PHP REST service, and then point your Silverlight app at that service. By doing so, you're protecting your database not only from people snooping through it, but more importantly, you're restricting what people can do to your database. If your data is supposed to be read-only, and your service's contract only allows reading operations, you're set. Alternatively, your service may negotiate sessions with credentials, again, set up through WCF.

WCF 可以是仅客户端、仅服务器或客户端-服务器连接器平台.您选择的内容会影响您编写的代码,但这一切都将独立于您的数据库.您的代码可以结构化为一对一映射到您的数据库表,也可以更抽象(如果您愿意,您可以设置代表完整逻辑视图的类).

WCF can be a client-only, server-only, or client-server connector platform. What you choose will affect the code you write, but it's all going to be independent of your database. Your code can be structured such that it's a one-to-one mapping to your database table, or it can be far more abstract (you can set up classes that represent full logical views if you choose).

这篇关于如何让 Silverlight 从 MySQL 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆