ASP.NET MVC 到 Silverlight 通信

发布于 2025-01-07 17:30:30 字数 380 浏览 1 评论 0原文

因此,我有一个 ASP.NET MVC Web 应用程序,其中嵌入了 silverlight 应用程序,并且我希望 SL 客户端通过在 ASP 内部运行的支持 SL 的 WCF 服务与数据库进行通信。 NET MVC 应用程序。我没有运气让它工作,silverlight 应用程序很好地获得了对 WCF 服务的服务引用。当我运行 silverlight 客户端并调用 Web 服务时,它每次都会返回 NOT FOUND 异常。我可以通过调试看到 WCF 服务确实被调用,但 silverlight 出错并且没有任何信息传回。

我确实尝试启用

serviceDebug includeExceptionDetailInFaults="true"

但我仍然收到 NOT FOUND 异常。有什么想法吗?

So I have an ASP.NET MVC web application that has a silverlight application embedded in it and I'd like the SL client to communicate with the database through a SL enabled WCF service that runs inside the ASP.NET MVC application. I have had no luck in getting this to work, the silverlight application gets a service reference to the WCF service just fine. When I run the silverlight client and call the web service it returns a NOT FOUND exception every single time. I can see through debugging that the WCF service does get called, but the silverlight errors out and nothing gets passed back.

I did try enabling the

serviceDebug includeExceptionDetailInFaults="true"

But I still get the NOT FOUND exception. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

或十年 2025-01-14 17:30:30

第一个技巧是让您的 silverlight 与您的 WCF 正确通信。 NOT FOUND 在很多情况下都会发生。可能是您的 Web 服务未在 IIS 中运行,也可能是您尝试使用某种违反服务契约的对象进行通信。例如,您的对象有一个“对象”类型的属性,可以是任何东西。缩小此过程范围的第一步是安装 Fiddler2 以显示客户端和服务器之间的网络流量。

一旦完成此操作,即一旦您确定您的 silverlight 应用程序可以与网络服务器通信,那么您就可以采取在 asp.net 和 silverlight 应用程序之间进行通信的方法。现在,请记住,Silverlight 在客户端上运行,而 asp.net 在服务器上运行,因此,从页面后面的 aspx 代码到 silverlight light 对象的通信必须通过某种常见代理进行 - 该代理是 JavaScript。定义一个 javascript 方法,该方法将访问 silverlight/对象容器的桥并传递该方法。反之亦然,定义一个允许 silverlight 与 javascript 对话的方法。

例如,假设您有一个在 silverlight 中显示在 aspx/html 页面上的类别列表。最有可能的是,该类别列表存在于您的 asp.net 项目的 ClientBin 文件夹中的 XAP 文件中。
示例:

<div style="margin:auto; float:left; height:auto;">
    <div id="silverlightControlHost_Categoris" style="height:auto;">

        <object id="silverlightControl_Categories" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" style="height:100%">
        <param name="source" value="ClientBin/Categories.xap"/>
        <param name="onError" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="4.0.50826.0" />
        <param name="autoUpgrade" value="true" />
        <param name="ScaleMode" value="Stretch" />
        <param name="EnableAutoZoom" value="True" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">
            <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
        </a>
    </object><iframe id="Iframe2" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
    </div>
</div>

接下来,在 Silverlight 项目的加载事件中,将对象注册为可编写脚本:

using System.Windows.Browser;

namespace CategoryListing
{
[ScriptableType]
public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        categoryListing1.DataContext = new MyLibrary.ViewModel.TCategoryListingViewModel();
        //categoryListing1 is the name of the control whose data context is the view model
        HtmlPage.RegisterScriptableObject("categoriesBridge", (categoryListing1.DataContext as MyLibrary.ViewModel.TCategoryListingViewModel));
    }

}

}

定义 javascript 方法来与类别列表视图模型中的某些方法对话

function selectCategory(category_id) {
var plugin = document.getElementById("silverlightControl_Categories");
//note the reference to the categoriesBridge property that was registered as scriptable
if (plugin != null)
    plugin.content.categoriesBridge.SelectCategory(category_id);

}

定义 SelectCategory 方法:

[ScriptableMember]
    public void SelectCategory(string category_id)
    {
        //select the category here
    }

如果您想从另一个调用此方法页面上的 silverlight 对象:

try
                {
                    HtmlWindow window = HtmlPage.Window;
                    window.Invoke("SelectCategory", new object[] { "Category1" });
                }
                catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }

最后,您应该能够在页面上使用 html/aspx 标记来调用 javascript 方法来调用 silverlight 对象。

享受。

The first trick would be to get your silverlight to communicate with your WCF properly. NOT FOUND occurs on MANY scenarios. It could be that your Web Service is not running in IIS, it could be that you are trying to communicate using some sort of object that violates the service contract. E.g. Your object has a property of type "object" that can be anything. The first step to narrow down this process would be to install Fiddler2 to show you the network traffic between your client and your server.

Once this is done, i.e., once you are sure that your silverlight app can communicate with the webserver, then you can take the approach of communicating between your asp.net and silverlight application. Now, keep in mind that Silverlight runs on the client, and asp.net runs on the server, therefore, communicating from and aspx code behind page to a silverlight light object must happen through some sort of common proxy - that proxy being JavaScript. Define a javascript method that would access a bridge to the silverlight/object container and pass the method. And vice versa, define a method that would allow the silverlight to talk back to the javascript.

For example, let's say you have a category listing that is displayed in silverlight on your aspx/html page. Most likely, that category listing exists in a XAP file in you ClientBin folder for your asp.net project.
Example:

<div style="margin:auto; float:left; height:auto;">
    <div id="silverlightControlHost_Categoris" style="height:auto;">

        <object id="silverlightControl_Categories" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" style="height:100%">
        <param name="source" value="ClientBin/Categories.xap"/>
        <param name="onError" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="4.0.50826.0" />
        <param name="autoUpgrade" value="true" />
        <param name="ScaleMode" value="Stretch" />
        <param name="EnableAutoZoom" value="True" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">
            <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
        </a>
    </object><iframe id="Iframe2" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
    </div>
</div>

Next, in your loaded event for your Silverlight Project, register the object as scriptable:

using System.Windows.Browser;

namespace CategoryListing
{
[ScriptableType]
public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        categoryListing1.DataContext = new MyLibrary.ViewModel.TCategoryListingViewModel();
        //categoryListing1 is the name of the control whose data context is the view model
        HtmlPage.RegisterScriptableObject("categoriesBridge", (categoryListing1.DataContext as MyLibrary.ViewModel.TCategoryListingViewModel));
    }

}

}

Define the javascript method to talk to some method in the category listing view model

function selectCategory(category_id) {
var plugin = document.getElementById("silverlightControl_Categories");
//note the reference to the categoriesBridge property that was registered as scriptable
if (plugin != null)
    plugin.content.categoriesBridge.SelectCategory(category_id);

}

Define the SelectCategory Method:

[ScriptableMember]
    public void SelectCategory(string category_id)
    {
        //select the category here
    }

If you wanted to call this method from ANOTHER silverlight object on the page:

try
                {
                    HtmlWindow window = HtmlPage.Window;
                    window.Invoke("SelectCategory", new object[] { "Category1" });
                }
                catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }

At the end of it all, you should be able to use your html/aspx markup on your page to invoke the javascript method to call the silverlight object.

Enjoy.

甲如呢乙后呢 2025-01-14 17:30:30

我能够弄清楚这一点 - 我用作数据契约的对象没有被标记为这样,即使它们被 silverlight 应用程序“接受”并且由服务引用生成,它们始终会在 WCF 服务之后通过此接受回来了。

I was able to figure this out - the objects I was using as data contracts were not marked as such and even though they were "accepted" by the silverlight app and were generated by the service reference they would always through this acception after the WCF service returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文