如何在 WPF 项目中公开一个安静的服务 [英] how to expose a restful service inside a WPF project

查看:33
本文介绍了如何在 WPF 项目中公开一个安静的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是个新手.我将 Visual Studio 2010 用于桌面应用程序,我需要将一些数据从那里公开到我局域网中的网站,但我不知道如何去做.

I'm a bit of a newbie. I'm using Visual Studio 2010 for a desktop application, and I need to expose some data from there to websites in my LAN, but I have no idea how to.

我创建了一个 MVC2 项目,我创建了一个新控制器,我可以公开一个简单的helloWorld"服务,如下所示:

I create an MVC2 project, I created a new controller and I can expose a simple "helloWorld" service like this:

[HttpGet]
    public JsonResult HolaMundo()
    {
        List<object> movies = new List<object>();

        movies.Add(new { Title = "Ghostbusters", Genre = "Comedy", Year = 1984 });
        movies.Add(new { Title = "Gone with Wind", Genre = "Drama", Year = 1939 });
        movies.Add(new { Title = "Star Wars", Genre = "Science Fiction", Year = 1977 });

        return Json(movies, JsonRequestBehavior.AllowGet);
    }

它有效:

除了在 wpf 项目中,我该怎么做?(或者我如何使用 wpf 项目中的这个 mvc 项目来获得相同的结果?)

How can I do it but in a wpf project? (Or how can I use this mvc project from the wpf project to get the same results?)

我只是迷失在信息的海洋中,我什至不知道如何为我的问题寻找解决方案.

I'm just lost in an sea of information, I don't even know how to search for a solution for my problem.

感谢您的耐心等待,祝您有美好的一天.

Thanks for your patience, have a nice day.

推荐答案

您通常不会公开来自 WPF 应用程序的 RESTful 服务.WPF 应用程序是一个客户端桌面应用程序,应该在最终用户的客户端计算机上运行.RESTful 服务应该从多个客户端可以连接到的服务器(通常是 Web 服务器)公开.

You typically don't expose a RESTful service from a WPF application. A WPF application is a client desktop application that is supposed to be run on an end-users client machine. A RESTful service is supposed to be exposed from a server that several clients can connect to - typically a web server.

您确实可以使用 WCF 创建 REST 服务:http://www.infoworld.com/article/3061973/application-development/how-to-create-a-restful-service-in-wcf.html

You could indeed use WCF to create a REST service: http://www.infoworld.com/article/3061973/application-development/how-to-create-a-restful-service-in-wcf.html

...并在包括 WPF 应用程序在内的任何 .NET 应用程序中托管 WCF 服务:https://msdn.microsoft.com/en-us/library/ms731758(v=vs.110).aspx

...and host the WCF service in any .NET application including a WPF application: https://msdn.microsoft.com/en-us/library/ms731758(v=vs.110).aspx

但问题是仍然没有好的方法来公开您的 WPF 客户端应用程序,以便其他客户端可以连接到它,因为它是客户端应用程序而不是服务器应用程序.因此,如果您想创建 RESTful 服务,我建议您查看 WCF 或 ASP.NET Web API:https://www.asp.net/web-api/overview/older-versions/build-restful-apis-with-aspnet-web-api

But the problem is that there is still no good way of exposing your WPF client application so that other clients can connect to it as it is a client application rather than a server application. So if you want to create a RESTful service I recommend you to take a look at either WCF or ASP.NET Web API: https://www.asp.net/web-api/overview/older-versions/build-restful-apis-with-aspnet-web-api

然后,您可以使用 WPF 应用程序中的 REST 服务,例如使用 HttpClient 客户端.此处提供了如何执行此操作的示例:https://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client.从 WPF 应用程序和任何其他托管 (.NET) 应用程序使用 REST 服务实际上没有区别.

You can then consume a REST service in a WPF application using the HttpClient client for example. There is an example of how to do this available here: https://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client. There is really no difference between consuming a REST service from a WPF application and any other managed (.NET) application.

这篇关于如何在 WPF 项目中公开一个安静的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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